Proper way to add item to the cart for new BETA features

Member since: 2012

I am reading documentation about new order calculations:

It mentions that for calculations to work properly, line items should exist in shipment: "The order calculators will only calculate line items that belongs to a shipment. This is a changed behaviour from how it worked in the workflow activities."

So what is the proper way to add new line item now?

Previously I used Quicksilver's CartService implementation, but I am not sure if it supports new way of handling things:

public bool AddToCart(string code, out string warningMessage)
	var entry = CatalogContext.Current.GetCatalogEntry(code);
	CartHelper.Cart.ProviderId = "frontend"; // if this is not set explicitly, place price does not get updated by workflow
	ValidateCart(out warningMessage);

	return CartHelper.LineItems.Select(x => x.Code).Contains(code);

It would be nice to have some working code sample :)

#143295 Jan 19, 2016 16:08
  • Member since: 2012

    I found an example here:

    var lineItem = CreateLineItem(variant, 1m, (decimal)price.UnitPrice.Amount);
    // A shipment needs to exists 
    // Replaced in 9.2: cart.Forms.First().Shipments.First().LineItems.Add(lineItem);
    var orderForm = cart.OrderForms.First();
    var index = orderForm.LineItems.IndexOf(lineItem);
    cart.OrderForms.First().Shipments.First().AddLineItemIndex(index, lineItem.Quantity);

    But this seems too complicated. Is it correct way to handle line item adding?

    #143296 Jan 19, 2016 16:11
  • Member since: 2013

    I used dotPeak too see whats happening inside "CartHelper.AddEntry(entry);" and as fare as I can see/understand of the code then it detects if the new system is used and do that for you

    #143303 Jan 19, 2016 22:22
  • Member since: 2011

    Sebastian is right if you use CartHelper and VNextWorkflow enabled then it will make sure shipment is there.  Also is VNextWorkflow is enabled you can use the workflow sytem as is and discount will be calculated with the new engine.

    If you want to use the new api without CartHelper something like this will work

    var cart = OrderRepository.LoadOrCreate<Cart>(PrincipalInfo.CurrentPrincipal.GetContactId(), Cart.DefaultName);
    var price = PriceService.GetDefaultPrice(marketId,
            new CatalogKey(new Guid(variation.ApplicationId), variation.Code),
    var lineItem = CreateLineItem(variation, quantity, price.UnitPrice.Amount);

    #143309 Jan 20, 2016 1:25
  • Member since: 2012

    Seems that my initializable module didn't enabled new Workflows. I now configured it in and it started to work.

    Here is my initializable module:

    public class SwitchOnNewPromoEngine : IInitializableModule
        public void Initialize(InitializationEngine context)
            var featureSwitch = ServiceLocator.Current.GetInstance<IFeatureSwitch>();
            featureSwitch.Features.Add(new WorkflowsVNext());
        public void Uninitialize(InitializationEngine context) { }

    I created it same as in this article:

    Anyway thanks Sebastian and Mark!

    #143314 Jan 20, 2016 7:42