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(); 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?
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
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);
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!
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:
It would be nice to have some working code sample :)