Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Created August 19, 2016 10:36
Show Gist options
  • Save jstemerdink/2daa362de8eb7c712b3c610f9e3b3e54 to your computer and use it in GitHub Desktop.
Save jstemerdink/2daa362de8eb7c712b3c610f9e3b3e54 to your computer and use it in GitHub Desktop.

Update lineitems in shipment when lineitems in cart change

Read my blog here

Powered by ReSharper image

using System.Linq;
using EPiServer.Commerce.Order;
using Mediachase.Commerce.Orders;
using Mediachase.Commerce.Workflow.Activities;
using Mediachase.Commerce.WorkflowCompatibility;
public class ExtendedValidateLineItemsActivity : ValidateLineItemsActivity
{
/// <summary>
/// Called by the workflow runtime to execute an activity.
/// </summary>
/// <param name="executionContext">The <see cref="T:Mediachase.Commerce.WorkflowCompatibility.ActivityExecutionContext" /> to associate with this <see cref="T:Mediachase.Commerce.WorkflowCompatibility.Activity" /> and execution.</param>
/// <returns>
/// The <see cref="T:Mediachase.Commerce.WorkflowCompatibility.ActivityExecutionStatus" /> of the run task, which determines whether the activity remains in the executing state, or transitions to the closed state.
/// </returns>
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
// Validate the properties at runtime
this.ValidateRuntime();
// Process Shipment now
this.UpdateShipment();
// Retun the closed status indicating that this activity is complete.
return base.Execute(executionContext);
}
/// <summary>
/// Updates the first shipment with the amount for the lineitem in the cart.
/// </summary>
private void UpdateShipment()
{
OrderForm orderForm;
if (this.OrderGroup.OrderForms.Count > 0)
{
orderForm = this.OrderGroup.OrderForms[0];
}
else
{
orderForm = new OrderForm { Name = this.OrderGroup.Name };
this.OrderGroup.OrderForms.Add(orderForm);
}
// Create a new shipment if there is no shipment.
Shipment shipment = orderForm.Shipments.FirstOrDefault() ?? orderForm.Shipments.AddNew();
// Remove items in the shipment that are not in the Cart anymore
foreach (ILineItem shipmentItem in shipment.LineItems)
{
LineItem lineItem = orderForm.LineItems.FindItem(shipmentItem.LineItemId);
if (lineItem != null)
{
continue;
}
int shipmentItemIndex = shipment.LineItems.ToList().IndexOf(shipmentItem);
shipment.RemoveLineItemIndex(shipmentItemIndex);
}
for (int i = 0; i < orderForm.LineItems.Count; i++)
{
LineItem lineItem = orderForm.LineItems[i];
int lineItemIndex = orderForm.LineItems.IndexOf(lineItem);
// If the item already exists, update or delete if the quantity is 0
if (shipment.LineItemIndexes.Contains(lineItemIndex.ToString()))
{
if (lineItem.Quantity == 0)
{
shipment.RemoveLineItemIndex(lineItemIndex);
}
else
{
shipment.SetLineItemQuantity(lineItemIndex, lineItem.Quantity);
}
continue;
}
// If the item does not exits, add
shipment.AddLineItemIndex(lineItemIndex, lineItem.Quantity);
}
this.OrderGroup.AcceptChanges();
}
}
using System.Linq;
using EPiServer.Commerce.Order;
using Mediachase.Commerce.Core.Features;
using Mediachase.Commerce.Markets;
using Mediachase.Commerce.Workflow;
using Mediachase.Commerce.Workflow.Activities;
using Mediachase.Commerce.Workflow.Activities.OrderGroupActivities;
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class SiteInitialization : IConfigurableModule
{
public void Initialize(InitializationEngine context)
{
}
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Container.Configure(c =>
{
c.For<ValidateLineItemsActivity>().Use<ExtendedValidateLineItemsActivity>();
});
}
public void Uninitialize(InitializationEngine context) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment