Line Items being removed after workflow is run

Vote:
 

Hi,

I am using Episerver 9 Commerce (this was a recent upgrade from a site that was currently working).

After the upgrade - whilst in testing - we have noticed that the Basket is being emptied. After hours of investigating we found that it was being emptied in ListItemsSimpleView just after the workflow is run.

if (!isEmpty)
            {
                // restore coupon code to context
                string couponCode = Session[Constants.LastCouponCode] as string;
                if (!String.IsNullOrEmpty(couponCode))
                {
                    MarketingContext.Current.AddCouponToMarketingContext(couponCode);
                }
 
                CartHelper.Cart.ProviderId = "FrontEnd";
                CartHelper.RunWorkflow(Constants.CartValidateWorkflowName);
 
                // If cart is empty, remove it from the database - HERE IT IS NOW EMPTY
                isEmpty = CartHelper.IsEmpty;
                if (isEmpty)
                    CartHelper.Delete();
 
                CartHelper.Cart.AcceptChanges();
            }

Do you know why this is?

Jon

#148647
May 19, 2016 12:02
Vote:
 

As I answered in other thread, you can check this method:

private void ValidateItems()
        {
            CatalogRelationDto relationDto = null;
            CatalogDto.CatalogRow catalogRow = null;

            var marketTester = new ExcludedCatalogEntryMarketsField();
            var orderMarket = ServiceLocator.Current.GetInstance<IMarketService>().GetMarket(OrderGroup.MarketId);
            var lineItems = OrderGroup.OrderForms.SelectMany(x => x.LineItems.ToArray());
            var validLineItems = lineItems.Where(x => x.Code != "0" && !String.IsNullOrEmpty(x.Code) && !x.Code.StartsWith("@"));

            List<LineItem> lineItemsToRemove = new List<LineItem>();
            foreach (var lineItem in validLineItems)
            {
                var entryRow = GetEntryRowForLineItem(lineItem);

                if (entryRow == null)
                {
                    AddWarningSafe(Warnings, "LineItemCodeRemoved-" + lineItem.Id,
                        String.Format("The catalog entry code that maps to the line item {0} has been removed or changed.  The line item is no longer valid", lineItem.Code));
                    continue;
                }

                if (!marketTester.IsValidForMarket(entryRow, orderMarket))
                {
                    AddWarningSafe(Warnings, "LineItemRemoved-" + lineItem.LineItemId.ToString(),
                        String.Format("Item \"{0}\" has been removed from the cart because it is not available in your market.",
                        lineItem.DisplayName));
                    lineItemsToRemove.Add(lineItem);
                    continue;
                }

                var requestDate = FrameworkContext.Current.CurrentDateTime;

                if (catalogRow == null || catalogRow.CatalogId != entryRow.CatalogId)
                {
                    var catalogDto = CatalogContext.Current.GetCatalogDto(entryRow.CatalogId);
                    catalogRow = catalogDto.Catalog.FirstOrDefault();
                }

                //check if the catalog of this entry is not available
                if (catalogRow == null || !catalogRow.IsActive || requestDate < catalogRow.StartDate || requestDate > catalogRow.EndDate)
                {
                    AddWarningSafe(Warnings, "LineItemRemoved-" + lineItem.LineItemId.ToString(),
                                                               String.Format("Item \"{0}\" has been removed from the cart because the catalog of this entry is not available.",
                                                               lineItem.DisplayName));
                    lineItemsToRemove.Add(lineItem);
                    continue;
                }

                relationDto = CatalogContext.Current.GetCatalogRelationDto(entryRow.CatalogEntryId);
                // populate item
                lineItem.Catalog = catalogRow.Name;
                lineItem.ParentCatalogEntryId = GetParentCatalogEntryId(entryRow.CatalogEntryId, relationDto);

                //Variation Info
                PopulateVariationInfo(entryRow, lineItem, lineItemsToRemove);

                if (string.IsNullOrEmpty(lineItem.WarehouseCode))
                {
                    // This case was passed because lineItem.WarehouseCode will be set in next activity - GetFulfillmentWarehouseActivity
                    continue;
                }

                var inventoryRecord = InventoryService.Get(lineItem.Code, lineItem.WarehouseCode);

                if (inventoryRecord == null)
                {
                    AddWarningSafe(Warnings, "LineItemCodeRemoved-" + lineItem.Id,
                        String.Format("The catalog entry code that maps to the line item {0} has been removed or changed.  The line item is no longer valid", lineItem.Code));
                    lineItemsToRemove.Add(lineItem);
                    continue;
                }

                // Get minimum date that the entry is available. It should be is minimum date of preorder date and start date
                var minAvailableDate = entryRow.StartDate;
                if (inventoryRecord != null && inventoryRecord.PreorderAvailableUtc > SafeBeginningOfTime && minAvailableDate > inventoryRecord.PreorderAvailableUtc)
                {
                    minAvailableDate = inventoryRecord.PreorderAvailableUtc;
                }

                //check if the entry is not available
                if (!entryRow.IsActive || requestDate < minAvailableDate || requestDate > entryRow.EndDate)
                {
                    AddWarningSafe(Warnings, "LineItemRemoved-" + lineItem.LineItemId.ToString(),
                                            String.Format("Item \"{0}\" has been removed from the cart because it is not available.",
                                            lineItem.DisplayName));
                    lineItemsToRemove.Add(lineItem);
                    continue;
                }

                //Inventory Info
                PopulateInventoryInfo(inventoryRecord, lineItem);
            }

            if (lineItemsToRemove.Count > 0)
            {
                // remove lineitem from shipment
                foreach (OrderForm form in OrderGroup.OrderForms)
                {
                    foreach (var lineItem in lineItemsToRemove)
                    {
                        form.RemoveLineItemFromShipments(lineItem);
                    }
                }

                // remove lineitem from order
                foreach (var lineItem in lineItemsToRemove)
                {
                    lineItem.Delete();
                }
            }
        }

Of course, it's still the easiest way to download the workflow project then debug to see why your items are removed.

Regards,

/Q

#148656
May 19, 2016 13:37
Vote:
 

Hi!

We had the same problem and that was because our variants did not allow neither PreOrder or BackOrder and since the stock quantity was 0 the variant was removed from the shopping cart.
So if your item is out of stock but you still want to able order it, it has to have PreOrder or BackOrder enabled.

/Kristoffer

#148658
May 19, 2016 14:34
Vote:
 

Hi,

Thanks for your updates.

Our Variant has a quantity of 117. It also matches all the criteria in the above ValidateItems method so it should pass all this.

It's very odd,

jon

#148663
May 19, 2016 14:51
Vote:
 

Hi,

Have you inspected the warnings from the workflow execution to check what's going on? You could also download the full workflow project under Documentation > Related downloads > Epi Commerce, change your reference in your site project and debug the workflow activity.

The warnings added by the workflow activities can be read like this:

var workflowResult = OrderGroupWorkflowManager.RunWorkflow(CartHelper.Cart, workflowName);               

var warnings = OrderGroupWorkflowManager.GetWarningsFromWorkflowResult(workflowResult);



#148680
May 19, 2016 22:17
Vote:
 

Hi,

With latest Marketing Engine, Commerce MarketingContext.Current.AddCouponToMarketingContext(couponCode) will probably not work to add coupons,
Use 

(CartHelper.Cart.OrderForms[0] as IOrderForm).CouponCodes.Add("CoupoCode");

 


Cart Validate workflow you must be running even before this point, Investigate is Marketing Context effecting this () ? If yes, Is there any custom promotions are there ....

Custome work flows? If Yes, Enable Loggign of your website for level "All" to investigate any exception recorded.

Regards
/K

#148805
Edited, May 24, 2016 13:46
Vote:
 

Hi,

Do yu know how to enable logging for level "All"? Im a bit new to logging.

Thanks

Jon

#148808
May 24, 2016 14:03
Vote:
 

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Logging/logging-with-log4net/

#148810
May 24, 2016 14:08
Vote:
 

Hi, Sorry to bother you again, we have set this up but the file is not writing. Do you know if we need to do anything else?

Jon

#148819
May 24, 2016 14:55
Vote:
 

Check Configuration in EPiServerLog.config, IISUser should have access to File locations.

Regards
/K

#148840
May 24, 2016 18:55
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.