allBlogsList

Custom Promotion with Sitecore Commerce Connect

Sitecore Commerce Connect gives the ability to connect the Sitecore website to any Commerce application. Below are the steps to create a custom promotion, which will allow us to offer special discounts or products based on the user cart.

To implement this feature, a Cart Service class will be created. This service will recalculate the cart and add any valid promotions. Each time the recalculate is called the cart will be reset before the promotions are applied.

using Sitecore.Commerce.Entities.Carts;
using Sitecore.Commerce.Services.Prices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommerceAnalyticsModule.Service
{
    public class CartService
    {
        public static void Recalculate(Cart cart)
        {
        }
    }
}

The function to recalculate the order will loop through the order lines. The price for each order line will be generated using the commerce connect pricing service.

            PricingServiceProvider pricing = new PricingServiceProvider();

            foreach (CartLine currentLine in cart.Lines)
            {
                GetProductPricesResult priceResult = pricing.GetProductPrices(new GetProductPricesRequest(currentLine.Product.ProductId));

                currentLine.Total.Amount = priceResult.Prices.FirstOrDefault().Value.Amount;
            }

After we have reset the price the promotion discount will be reapplied. In the code example the Product Id is hardcoded to 1234 and the user will receive a 10% discount.

                if (currentLine.Product.ProductId == "1234")
                {
                    currentLine.Total.Amount = currentLine.Total.Amount * 0.9M;
                }

Below is the complete class for CartService.

using Sitecore.Commerce.Entities.Carts;
using Sitecore.Commerce.Services.Prices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommerceAnalyticsModule.Service
{
    public class CartService
    {
        public static void Recalculate(Cart cart)
        {
            PricingServiceProvider pricing = new PricingServiceProvider();

            foreach (CartLine currentLine in cart.Lines)
            {
                GetProductPricesResult priceResult = pricing.GetProductPrices(new GetProductPricesRequest(currentLine.Product.ProductId));

                currentLine.Total.Amount = priceResult.Prices.FirstOrDefault().Value.Amount;

                if (currentLine.Product.ProductId == "1234")
                {
                    currentLine.Total.Amount = currentLine.Total.Amount * 0.9M;
                }
            }
        }
    }
}

The next step is to update the Sitecore Commerce Connect cart pipelines to apply the promotions. The logic to apply the promotions will be handled in the Cart Service.

using Sitecore.Commerce.Pipelines;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Commerce.Services.Carts;
using Sitecore.Commerce.Entities.Carts;
using Sitecore.Commerce.Services.Prices;
using CommerceAnalyticsModule.Service;

namespace CommerceAnalyticsModule.Pipelines
{
    public class UpdateLinesToCart : PipelineProcessor

The final step is to add the following config under app_config/include. This will have the promotion logic apply before the promotion is saved.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
	<sitecore>
		<pipelines>
			<commerce.carts.addCartLines>
				<processor type="CommerceAnalyticsModule.Pipelines.UpdateLinesToCart, CommerceAnalyticsModule"
								 patch:before="processor[@type='Sitecore.Commerce.Pipelines.Carts.Common.RunSaveCart, Sitecore.Commerce']">
					<param ref="insite/clients/CartClient" />
				</processor>
			</commerce.carts.addCartLines>
			<commerce.carts.removeCartLines>
				<processor type="CommerceAnalyticsModule.Pipelines.UpdateLinesToCart, CommerceAnalyticsModule"
								 patch:before="processor[@type='Sitecore.Commerce.Pipelines.Carts.Common.RunSaveCart, Sitecore.Commerce']">
					<param ref="insite/clients/CartClient" />
				</processor>
			</commerce.carts.removeCartLines>
			<commerce.carts.updateCartLines>
				<processor type="CommerceAnalyticsModule.Pipelines.UpdateLinesToCart, CommerceAnalyticsModule"
								 patch:before="processor[@type='Sitecore.Commerce.Pipelines.Carts.Common.RunSaveCart, Sitecore.Commerce']">
					<param ref="insite/clients/CartClient" />
				</processor>
			</commerce.carts.updateCartLines>
		</pipelines>
	</sitecore>
</configuration>

The following shows how to update the Sitecore Commerce Connect to support custom promotions. This will allow you to meet the business complex requirements while using your current commerce system.