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

Vote:
 

I am reading documentation about new order calculations: http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Commerce/9/Orders/calculating-orders-beta/

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.AddEntry(entry);
	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
Vote:
 

I found an example here:

http://epiwiki.se/developing/episerver-commerce/order-system-abstractions-(beta)-sample-code-for-version-9

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();
orderForm.LineItems.Add(lineItem);
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
Vote:
 

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
Vote:
 

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,
        DateTime.UtcNow,
        new CatalogKey(new Guid(variation.ApplicationId), variation.Code),
        SiteContext.Current.Currency);
            
var lineItem = CreateLineItem(variation, quantity, price.UnitPrice.Amount);
cart.Forms.First().Shipments.First().LineItems.Add(lineItem);
PromotionEngine.Run(cart);
OrderRepository.Save(cart);
#143309
Jan 20, 2016 1:25
Vote:
 

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

Here is my initializable module:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class SwitchOnNewPromoEngine : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var featureSwitch = ServiceLocator.Current.GetInstance<IFeatureSwitch>();
        featureSwitch.InitializeFeatures();
        featureSwitch.Features.Add(new WorkflowsVNext());
        featureSwitch.EnableFeature(WorkflowsVNext.FeatureWorkflowsVNext);
    }
 
    public void Uninitialize(InitializationEngine context) { }
}

I created it same as in this article: http://www.david-tec.com/2015/07/creating-a-custom-promotion-with-the-new-episerver-commerce-9-promotion-engine-beta---part-1/

Anyway thanks Sebastian and Mark!

#143314
Jan 20, 2016 7:42
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.