Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 21, 2017 @ 11:41
    Simon Dingley
    0

    Anonymous Customer Basket Conversion on Login

    I have a site that for some reason loses the anonymous basket contents when the user logs in and I've so far not been able to track down why.

    My config as follows:

    <strategies>
        <strategy alias="DefaultPackaging" type="Merchello.Core.Strategies.Packaging.DefaultWarehousePackagingStrategy, Merchello.Core" />
        <strategy alias="DefaultShipmentRateQuote" type="Merchello.Core.Gateways.Shipping.DefaultShipmentRateQuoteStrategy, Merchello.Core" />
        <strategy alias="DefaultInvoiceTaxRateQuote" type="Merchello.Core.Gateways.Taxation.FixedRate.FixedRateTaxCalculationStrategy, Merchello.Core" />
        <strategy alias="DefaultAnonymousBasketConversionStrategy" type="MyCustomer.Infrastructure.Workflow.BasketConversionByReplacingPreviousCustomerBasket, ZoeLee.Infrastructure" />
        <strategy alias="InvoiceItemizationStrategy" type="Merchello.Core.Strategies.Itemization.DefaultInvoiceItemizationStrategy, Merchello.Core" />
    </strategies>
    

    I swapped out the DefaultAnonymousBasketConversionStrategy with one of my own which is at the moment an exact copy of BasketConversionByDiscardingPreviousCustomerBasket so that I can step through the code and for some reason the AnonymousBasket is already empty by the time it attempts to execute my Basket Conversion workflow.

    Any ideas?

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 25, 2017 @ 10:34
    Simon Dingley
    0

    After quite some digging I have been able to isolate the source of the problem to a custom feature that allows the user to get a shipment quote before they checkout. At the moment my best guess is that the call to PackageBasket in order to get the quote is what is clearing the basket contents?

    Here is the API method I am calling:

    [System.Web.Http.HttpGet]
    public UpdatedSaleSummary GetShipRateQuote(string customerToken, string countryCode)
    {
        var catalog = MerchelloContext.Current.Services.WarehouseService.GetAllWarehouseCatalogs().FirstOrDefault();
        var gatewayproviderService = MerchelloContext.Current.Services.GatewayProviderService;
        var country = gatewayproviderService.GetShipCountry(catalog.Key, countryCode);
        var tokenKey = customerToken.DecryptWithMachineKey();
        var customerKey = new Guid(tokenKey);
        var customerBase = this._merchelloContext.Services.CustomerService.GetAnyByKey(customerKey);
    
        var provinces = GetProvincesForCountry(country.CountryCode);
        var province = provinces.Any() ? provinces.FirstOrDefault().Code : string.Empty;
        var shipment =
            customerBase.Basket()
                .PackageBasket(new Address { Region = province, CountryCode = country.CountryCode })
                .FirstOrDefault();
        var quote = shipment.ShipmentRateQuotes(false).FirstOrDefault(x => x.ShipMethod.ShipCountryKey == country.Key);
    
        var checkoutMgr = customerBase.Basket().GetCheckoutManager();
    
        // Clear previous shipment quotes
        checkoutMgr.Shipping.ClearShipmentRateQuotes();
    
        if (quote != null)
        {
            quote.Rate = CurrencyHelper.ConvertToCurrency(quote.Rate, CurrencyHelper.GetCustomerCurrency());
            checkoutMgr.Shipping.SaveShipmentRateQuote(quote);
        }
    
        checkoutMgr.Customer.SaveBillToAddress(new Address { CountryCode = countryCode });
        checkoutMgr.Customer.SaveShipToAddress(new Address { CountryCode = countryCode });
    
        var invoice = checkoutMgr.Payment.PrepareInvoice();
    
        var summary = new UpdatedSaleSummary()
                          {
                              TotalLabel = "Total",
                              InvoiceTotal = ModelExtensions.FormatPrice(invoice.Total, this._currency),
                              TaxTotal = ModelExtensions.FormatPrice(invoice.TaxLineItems().Sum(x => x.TotalPrice), this._currency),
                              DiscountsTotal =
                                  ModelExtensions.FormatPrice(invoice.DiscountLineItems().Sum(x => x.TotalPrice), this._currency),
                              ShippingTotal =
                                  ModelExtensions.FormatPrice(invoice.ShippingLineItems().Sum(x => x.TotalPrice), this._currency)
                          };
    
        return summary;
    }
    

    The cart contents are actually being wiped out on the call to customerBase.Basket().GetCheckoutManager();.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 25, 2017 @ 11:55
    Simon Dingley
    0

    I've tried changing the CheckoutContextSettings passed to the GetCheckoutManager() method but still, the problem persists:

    var checkoutMgr = customerBase.Basket().GetCheckoutManager(
                    new CheckoutContextSettings
                        {
                            ResetCustomerManagerDataOnVersionChange = false,
                            ResetPaymentManagerDataOnVersionChange = false,
                            ResetExtendedManagerDataOnVersionChange = false,
                            ResetShippingManagerDataOnVersionChange = false,
                            ResetOfferManagerDataOnVersionChange = false,
                            EmptyBasketOnPaymentSuccess = false,
                            ApplyTaxesToInvoice = true,
                            RaiseCustomerEvents = false
                        });
    

    This issue seems to have been introduced between 2.2.0 and 2.5.0 as it is not currently happening on the production site. Any ideas on how to work around it would be appreciated.

    Thanks, Simon

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 26, 2017 @ 14:24
    Simon Dingley
    0

    So, in CheckoutExtensions.cs there is an internal method called CreateCheckoutContext, at the point of entering this method my IBasketObject contains an item, the item I see in my cart.

    After the call to basket.Validate() the basket then has no items! Progress? Perhaps, but no closer to finding the source of the issue at the moment still but will start look into the possibility of some failed basket validation at the moment being the cause.

  • Lee 1130 posts 3088 karma points
    Sep 26, 2017 @ 15:14
    Lee
    0

    Is this to do with the basket strategy in the merchello.config?

    <strategy alias="DefaultAnonymousBasketConversionStrategy" type="Merchello.Web.Workflow.BasketConversionByDiscardingPreviousCustomerBasket, Merchello.Web" />
        <!--strategy alias="DefaultAnonymousBasketConversionStrategy" type="Merchello.Web.Workflow.BasketConversionByCombiningAnonymousBasket, Merchello.Web" /-->
    

    By default, BasketConversionByDiscardingPreviousCustomerBasket is by default... maybe you need the other strategy. Just uncomment it and comment out the one above.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 26, 2017 @ 15:21
    Simon Dingley
    101

    OK, so FINALLY I have managed to track this down but hopefully, this thread may help someone else. Enabling DEBUG logging (which I should have done sooner) revealed the following entry when the problem occurs…

    Basket validation returned messages -> Item is out of stock.  Item removed was XXX_PRODUCT_SKU
    

    The issue was completely and utterly my fault and was due to a recent change request whereby the customer wanted to show out of stock sizes still on the site but have them disabled in the drop-down selection. Due to my own human error, the logic was reversed and all in stock items were disabled and out of stock items were enabled. One stupid exclamation mark has cost me about a day of debugging :(

    This is exactly why I have a love/hate relationship with my work!!!

    Still, lesson learned and it is handy to know that there is logic in place to remove items that make their way into the cart but are out of stock when it comes to checkout. Just need to find a better way to handle this event if it occurs.

Please Sign in or register to post replies

Write your reply to:

Draft