Shipping Cost and Checkout Smartware


SHOW PRODUCTS IN SUMMARY COLUMN 

SW-103756 - 1.12. CHECKOUT – SHOW PRODUCTS IN SUMMARY COLUMN (23)

_CheckoutCartSummary.cshtml 





SHIPPING COST IN SHOPPING CART SUMMARY

SW-103755 - 1.11. SB – SHIPPING COST IN SHOPPING CART SUMMARY (22), THE MOUSE-OVER CART (24), AND THE CHECKOUT RIGHT COLUMN SUMMARY (23)


ExtendedShippingCostStep > 

public override void Execute(BasketCalculationContext context)
        {
            if (context.SourceBasket.BasketLines.Count == 0)
                return;
 
            if (!CommerceFrameworkBase.ShippingMethods.AreShippingCostsEnabled())
            {
                if (context.SourceBasket.BasketLines.RemoveServiceLine(SalesLineTypes.ShippingCosts) || context.SourceBasket.ShippingMethodId.HasValue)
                {
                    context.SourceBasket.ShippingMethodId = null;
                    OnSourceBasketChanged(context);
                }
                return;
            }
 
            context.SourceBasket.TotalGrossWeight = CommerceFrameworkBase.Orders.CalculateTotalWeight(context.SourceBasket);
            context.SourceBasket.TotalPriceExcludingDiscount = CalculateTotalPriceExcludingDiscount(context.SourceBasket);
 
            var shipTo = CommerceFrameworkBase.Addresses.GetShippingAddress(context.SourceBasket, context.Settings.IsSalesAgentImpersonatingContact);
 
            // If User is not logged in to system, get selected country's shippinng cost
 
            #region Get login Customer
            var accountId = CommerceFrameworkBase.Context.AccountId;
            var accountType = ShopContext.GetCurrent()?.ShopAccount?.Type;
            ICustomerProfile customer = null;
 
            if (accountType != null)
            {
                if (accountType == ShopAccountType.Contact)
                    customer = CommerceFrameworkBase.Customers.GetCustomerByContact(accountId);
                else if (!String.IsNullOrEmpty(accountId) && ((accountType == ShopAccountType.Customer) || (accountType == ShopAccountType.SalesAgent && ShopContext.GetCurrent().IsImpersonating)))
                    customer = CommerceFrameworkBase.Customers.GetCustomer(accountId);
            }
            else
                customer = CommerceFrameworkBase.Customers.GetCustomer(accountId);
            #endregion
 
            //Get selected countryID and set 'shipTo' Address, for get the selected country's shippinng cost.
            if (accountType == null)
            {
                var countryIdFromCookies = CookiesState.GetValue("B2CCountryId");
 
                if (!countryIdFromCookies.IsEmptyString())
                {
                    shipTo = new CustomerAddress();
                    shipTo.CountryId = countryIdFromCookies;
                }
            }
 
            //Set Ship Address property
            context.SourceBasket.ShippingAddress = shipTo;
 
            var line = context.SourceBasket.BasketLines.FindServiceLine(SalesLineTypes.ShippingCosts);
            IShippingMethod shippingRate = null;
            bool isDefaultRate = false;
 
 
            //1. Get "Deault Shipping method" and set value to the "ShippingMethodId" 
 
            // Check if default shipping already set            
            if (!((Basket)context.SourceBasket).IsDefaultShipMethodChecked)
            {
                ExtendedShippingMethodManager extendedShippingMethodManager = new ExtendedShippingMethodManager();
                var shippingMethods = extendedShippingMethodManager.GetAllShippingMethods();
 
                if (shippingMethods.ToList().Count > 0)
                {
                    //Get Default Shopping method  and set values                                        
                    var defaultshipMethod = shippingMethods.Where(i => ((ShippingMethod)i).IsDefaultShippingMethod == true).FirstOrDefault();
 
                    if (defaultshipMethod != null)
                    {
                        context.SourceBasket.ShippingMethodId = defaultshipMethod.Id;
                        ((Basket)context.SourceBasket).IsDefaultShipMethodChecked = true;
                        ShopApi.Basket.ModifyCurrentBasket(basket =>
                        {
                            ((Basket)basket).IsDefaultShipMethodChecked = true;
                            return true;
                        });
                    }
                }
            }
 
            // If the customer is EX worker,Then Reset deails to EX work shipping details. 
            var customerShippingMethodCode = ((CustomerProfile)customer).ShippingMethodCode;
 
            if (customerShippingMethodCode == "EXW")
            {
                //Get the EXW Shipping Details and fill sourceLine , calculatedLine               
                var shippingMethods = new ExtendedShippingMethodManager().GetAllShippingMethods();
 
                if (shippingMethods.ToList().Count > 0)
                {
                    //Get EX Work enable shipping Cost                                       
                    var exWorkMethod = shippingMethods.Where(i => ((ShippingMethod)i).IsExWorx == true).FirstOrDefault();
 
                    if (exWorkMethod != null)
                        context.SourceBasket.ShippingMethodId = exWorkMethod.Id;
                }
            }
 
            if (context.SourceBasket.ShippingMethodId.HasValue)
            {
                shippingRate = CommerceFrameworkBase.ShippingMethods.CalculateShippingRate(context.SourceBasket.ShippingMethodId.Value, context.SourceBasket, shipTo);
                if (shippingRate == null)
                    HandleUnavailableShippingMethod(context.SourceBasket, context.SourceBasket.ShippingMethodId.Value, shipTo);
            }
            else
            {
                var shippingRates = CommerceFrameworkBase.ShippingMethods.CalculateShippingRates(context.SourceBasket, shipTo);
                shippingRate = GetDefaultShippingRate(shippingRates);
                isDefaultRate = true;
            }
 
            if (shippingRate == null)
            {
                if (line != null || context.SourceBasket.ShippingMethodId.HasValue)
                {
                    if (line != null)
                        context.SourceBasket.BasketLines.Remove(line);
                    context.SourceBasket.ShippingMethodId = null;
                    context.SourceBasket.IsSuggestedShippingMethod = true;
                    OnSourceBasketChanged(context);
                }
                return;
            }
 
            decimal shippingCost = shippingRate.Cost.HasValue ? shippingRate.Cost.Value : 0;
 
            if (context.SourceBasket.BasketLines.SetServiceLine(shippingRate.Id, shippingRate.Name, shippingCost, SalesLineTypes.ShippingCosts))
            {
                if (shippingRate.Id != Guid.Empty)
                    context.SourceBasket.ShippingMethodId = shippingRate.Id;
                else
                    context.SourceBasket.ShippingMethodId = null;
 
                context.SourceBasket.IsSuggestedShippingMethod = isDefaultRate;
                OnSourceBasketChanged(context);
            }
        }