Several rows in tha cart for same variant

Vote:
 

Hi!

We are using Episerver Commerce and Quicksilver.

Our customer sells cables by meter so for instance you can buy the same variant twice with different lenght. Variant 99-77 2 meter and the the same variant with 5 meters. Using Quicksilver the AddToCart handles this by making one row in the cart with 5m and that is not correct. I haven't found out how to handle this, can you enlight me?

Thanks!

Episerver CMS 9.6.1
Episerver.Commerce 9.8.1

/Kristoffer

#146320
Mar 16, 2016 9:50
Vote:
 

Hi,

That is very strange - I just tested on QuickSilver and if you add two variants to the cart, they should appear to be two line items.

" Variant 99-77 2 meter and the the same variant with 5 meters" are they are two Variants or same Variant?

/Q

#146486
Mar 16, 2016 14:17
Vote:
 

They are the same variant. Since they sell the cable by free lenght we cannot have a variant för each value. I could actually buy one cable that is 1,3m and one that is 2,1m. It is the same variant but different lenght and we cannot add 0,1 0,2 0,3 and so on and own variants.

Thanks for the quick response!

#146489
Mar 16, 2016 14:26
Vote:
 

QuickSilver do not support that. If you look into CartController.Add, you will see it always treat same variant as same lineitem.

You might update that action, as well as CartService.Add to take another parameter (length), and add a metafield with type of decimal to LineItem . So you create another lineitem when the length is different.

Here an example of code to create lineitem:

/// <summary>
        /// Create the lineitem
        /// </summary>
        /// <param name="entry">The entry</param>
        /// <param name="quantity">The quantity</param>
        /// <param name="warehouseCode">The warehouse code</param>
        /// <returns></returns>
        private LineItem CreateLineItem(Entry entry, decimal quantity, decimal length, string warehouseCode)
        {
            LineItem lineItem = new LineItem();

            // If entry has a parent, add parents name
            if (entry.ParentEntry != null)
            {
                lineItem.DisplayName = String.Format("{0}: {1}", entry.ParentEntry.Name, entry.Name);
                lineItem.ParentCatalogEntryId = entry.ParentEntry.ID;
            }
            else
            {
                lineItem.DisplayName = entry.Name;
                lineItem.ParentCatalogEntryId = String.Empty;
            }

            MarketId currentMarketId = CurrentMarketService.Service.GetCurrentMarket().MarketId;
            IPriceValue listPrice = PriceService.Service.GetDefaultPrice(currentMarketId, FrameworkContext.Current.CurrentDateTime, new CatalogKey(entry), SiteContext.Current.Currency);

            lineItem.Code = entry.ID;
            lineItem.MaxQuantity = entry.ItemAttributes.MaxQuantity;
            lineItem.MinQuantity = entry.ItemAttributes.MinQuantity;
            lineItem.Quantity = quantity;
            lineItem.Length = length;

            if (String.IsNullOrEmpty(warehouseCode))
            {
                lineItem.WarehouseCode = string.Empty; // was "entry.ItemAttributes.WarehouseCode;" but questionable
                lineItem.InventoryStatus = (int)entry.InventoryStatus;
            }
            else
            {
                lineItem.WarehouseCode = warehouseCode;
                lineItem.InventoryStatus = (int)GetInventoryStatus(entry, warehouseCode);
            }

            if (listPrice != null)
            {
                lineItem.ListPrice = listPrice.UnitPrice.Amount;
                lineItem.PlacedPrice = listPrice.UnitPrice.Amount;
                lineItem.ExtendedPrice = listPrice.UnitPrice.Amount;
            }
            else
            {
                lineItem.ListPrice = lineItem.PlacedPrice;
            }

            //Populate LineItem description field from the Entry object, if it exists in entry metaclass
            //Entry description metafield is also configurable so it could map another field to the description field . This fixes issue #718
            if (!string.IsNullOrEmpty(EntryDescriptionMetaFieldName))
            {
                MetaClass mc = MetaClass.Load(CatalogContext.MetaDataContext, entry.MetaClassId);
                foreach (MetaField mf in mc.MetaFields)
                {
                    if (mf.Name.Equals(EntryDescriptionMetaFieldName, StringComparison.InvariantCultureIgnoreCase) &&
                        entry.ItemAttributes[EntryDescriptionMetaFieldName] != null)
                    {
                        //description mapped field found - add to LineItem.Description field
                        lineItem.Description = entry.ItemAttributes[EntryDescriptionMetaFieldName].ToString();
                    }
                }
            }

            return lineItem;
        }
#146492
Edited, Mar 16, 2016 14:35
Vote:
 

Thanks, that worked like a charm. I had to change both AddToCart and ChangeQuantity. Since ChangeQuantity only look for code when updating I had to add lineItemId to update the correct line.

public LineItem AddToCart(string code, out string warningMessage)
{
    var entry = CatalogContext.Current.GetCatalogEntry(code);
    LineItem lineItem;

    if (CartHelper.LineItems.Any(l => l.Code == code))
    {
        lineItem = CreateLineItem(entry, code);
        CartHelper.OrderForm.LineItems.Add(lineItem);
        AcceptChanges();
    }
    else
    {
        lineItem = CartHelper.AddEntry(entry);
        CartHelper.Cart.ProviderId = "frontend"; // if this is not set explicitly, place price does not get updated by workflow    
    }
                         

    return lineItem;
}
public void ChangeQuantity(int lineItemId, string code, decimal quantity)
{
    if (quantity == 0)
    {
        RemoveLineItem(lineItemId, code);
    }
    var lineItem = CartHelper.Cart.GetLineItem(lineItemId, code);
    if (lineItem != null)
    {
        _cartItems = null;
        lineItem.Quantity = quantity;
        ValidateCart();
        AcceptChanges();
    }
}

Every where the ChangeCartItem was used I had to add

@Html.Hidden("lineItemId", Model.LineItemId)

Then the lineItemId was posted to the change function.

Thanks again!

/Kristoffer

#146570
Mar 17, 2016 16:34
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.