Remove products from shopping cart

This section describes how to remove a product from shopping cart.

There are two way to remove an item from shopping cart:

  1. Use the Litium.Foundation.Modules.ECommerce.WebControls.RemoveButton webcontrol
  2. Call method CurrentState.ShoppingCart.RemoveProduct(string articleNumber).

ShoppingCart.RemoveProduct(string articleNumber) method

This method need to be called to remove a product from the shopping cart, if you are implementing your own "Remove item from cart" button or link.

The ShoppingCart.RemoveProduct(string articleNumber) method does not automatically save the shopping cart.

Therefore, after calling the remove method you should call the

CurrentState.ShoppingCart.SetShoppingCart(CurrentState state) method to make the changes to be saved.

ShoppingCart.RemoveProduct(string articleNumber) method internally calls following method in ICheckoutFlow with quantity parameter set to zero.

ICheckoutFlow.EditOrderRow(OrderCarrier orderCarrier, Guid orderRowIdToEdit, decimal quantity, string comments, decimal discountPercentage, SecurityToken token)

 

The default implementation of ICheckoutFlow.EditOrderRow method remove the order row identified by orderRowIdToEdit parameter, when the quantity is zero.

Code sample

Following code sample is from accelerator implementation. Source can be found at: \Src\Litium.Studio.Accelerator\UI\WebControls\OrderRowRemoveButton.cs

       /// <summary>
        /// Handles the Click event of the RemoveButton control.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.
        /// </param>
        private void OrderRowRemoveButtonClick(object sender, EventArgs e)
        {
            try
            {
                Guid orderRowId = new Guid(Attributes[_orderRowId]);
                _controlState.CMS.CurrentState.ShoppingCart.UpdateRowQuantity(orderRowId, 0);
                _controlState.CMS.CurrentState.ShoppingCart.UpdateChangedRows();
                Cart.SetShoppingCart(_controlState.CMS.CurrentState, false);
            }
            catch (FormatException)
            {
                if (_controlState.ProductCatalog.CurrentArticle != null)
                {
                    _controlState.CMS.CurrentState.ShoppingCart.RemoveProduct(_controlState.ProductCatalog.CurrentArticle.ArticleNumber);
                    Cart.SetShoppingCart(_controlState.CMS.CurrentState);
                }
            }
        }
Is this page helpful?
Thank you for your feedback!