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.
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();.
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.
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.
By default, BasketConversionByDiscardingPreviousCustomerBasket is by default... maybe you need the other strategy. Just uncomment it and comment out the one above.
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.
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:
I swapped out the
DefaultAnonymousBasketConversionStrategy
with one of my own which is at the moment an exact copy ofBasketConversionByDiscardingPreviousCustomerBasket
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?
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:
The cart contents are actually being wiped out on the call to
customerBase.Basket().GetCheckoutManager();
.I've tried changing the CheckoutContextSettings passed to the
GetCheckoutManager()
method but still, the problem persists: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
So, in
CheckoutExtensions.cs
there is an internal method calledCreateCheckoutContext
, 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.Is this to do with the basket strategy in the merchello.config?
By default, BasketConversionByDiscardingPreviousCustomerBasket is by default... maybe you need the other strategy. Just uncomment it and comment out the one above.
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…
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.
is working on a reply...