Friday, May 21, 2010

LineItem Deletion

CS 09 has an issue w/ LineItem deletions on a Basket. The issue is with the underlying CS 2007 API in that the LineItems.Remove method is overloaded to take either the index or the lineitem. The sequence components use the Remove method with the lineitem which is causing the first one in the collection to be removed. The cleanest approach in solving the problem is to override the Sequence Component. It is simply one method that needs to be overridden. Here is the solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Providers.Utility;
using DevWerx.Web.Commerce.Common;
using Microsoft.Commerce.Providers.Components;
 
namespace DevWerx.Web.Commerce.Extensions.SequenceComponents
{
  public class BasketLineItemsProcessor : Microsoft.Commerce.Providers.Components.BasketLineItemsProcessor
  {
    protected override void DeleteRelatedItem(CommerceDeleteRelatedItem deleteRelatedItemOperation)
    {
      string str = BasketRelatedItemOperationSequenceComponent.GetSearchModelId(deleteRelatedItemOperation, "LineItem", true);
      if (!string.IsNullOrEmpty(str))
      {
        Microsoft.CommerceServer.Runtime.Orders.LineItem lineItemFromCachedOrderGroup = base.GetLineItemFromCachedOrderGroup(str);
        // replacing lineitem with index
        base.CachedOrderGroup.GetDefaultOrderForm().LineItems.Remove(lineItemFromCachedOrderGroup.Index);
      }
      else
      {
        base.CachedOrderGroup.GetDefaultOrderForm().LineItems.Clear();
      }
    }
  }
}
 

Then, of course you'll need to modify your ChannelConfiguration.config file:

   <MessageHandler name="CommerceUpdateOperation_Basket" responseType="Microsoft.Commerce.Contracts.Messages.CommerceUpdateOperationResponse, Microsoft.Commerce.Contracts, Version=1.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35">
    <OperationSequence>
     <Component name="Basket Loader" type="Microsoft.Commerce.Providers.Components.BasketLoader, Microsoft.Commerce.Providers, Version=1.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35" />
     <Component name="Basket processor" type="Microsoft.Commerce.Providers.Components.BasketProcessor, Microsoft.Commerce.Providers, Version=1.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35" />
     <!--<Component name="Line Items processor" type="Microsoft.Commerce.Providers.Components.BasketLineItemsProcessor, Microsoft.Commerce.Providers, Version=1.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35"> -->
     <Component name="Line Items processor" type="DevWerx.Web.Commerce.Extensions.SequenceComponents.BasketLineItemsProcessor, DevWerx.Web.Commerce.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ed11d75c6be4ce25" >
     ...
 

No comments:

Post a Comment