Proper use of the Delivery and DeliveryItem classes in Kentico E-Commerce (8.2)

Keith Donnell asked on January 4, 2016 23:57

I have written my own custom multi carrier provider, and in the implementation of GetPrice, it accepts a Delivery object and a string (currencyCode):

    public decimal GetPrice(Delivery delivery, string currencyCode)
    {
       // Custom logic to calculate pricing
    }

Prior to now, I have been able to get everything I need from delivery.Items[x].Product, which is a SKUInfo object. I now need to add some additional logic that is based off of a custom property within my ShoppingCartItemInfo object. There is currently no way to access the cart or cart item from a Delivery object, and the DeliveryItem.CustomData collection is empty, so it is definitely not automatically mapping any custom properties to this collection.

How can I get access to the ShoppingCartItemInfo, given a DeliveryItem?

Correct Answer

Keith Donnell answered on January 5, 2016 17:49

The answer I got externally was to create a custom DeliveryBuilder and include whatever columns I needed in the InitCustomData(cart) implementation:

using CMS;
using CMS.Ecommerce;

[assembly: RegisterImplementation(typeof(IDeliveryBuilder), typeof(CustomDeliveryBuilder))]
public class CustomDeliveryBuilder : DefaultDeliveryBuilder
{
    protected override void InitCustomData(ShoppingCartInfo cart)
    {
        base.InitCustomData(cart);

        cart.ShoppingCartCustomData.ColumnNames.ForEach(column => SetCustomData(column, cart.ShoppingCartCustomData[column]));
    }
}

While that works for custom data at the cart level, here is my implementation for the cart item level:

using CMS;
using CMS.Ecommerce;

[assembly: RegisterImplementation(typeof(IDeliveryBuilder), typeof(CustomDeliveryBuilder))]
public class CustomDeliveryBuilder : DefaultDeliveryBuilder
{
    protected override void InitItemCustomData(DeliveryItem deliveryItem, ShoppingCartItemInfo cartItem)
    {
        base.InitItemCustomData(deliveryItem, cartItem);

        cartItem.CartItemCustomData.ColumnNames.ForEach(column => deliveryItem.CustomData.SetValue(column, cartItem.CartItemCustomData[column]));
    }
}
2 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.