jxycms
7/5/2017 - 1:10 AM

shopping cart transformation example

shopping cart transformation example

<%@ Register Src="~/CMSModules/Ecommerce/Controls/Checkout/CartItemRemove.ascx" TagName="CartItemRemove"
    TagPrefix="cms" %>
<%@ Register Src="~/CMSModules/Ecommerce/Controls/Checkout/EcobudCartItemUnits.ascx" TagName="CartItemUnits"
    TagPrefix="cms" %>
<%@ Register Src="~/CMSModules/Ecommerce/Controls/Checkout/CartItemPriceDetail.ascx" TagName="CartItemPriceDetail"
    TagPrefix="cms" %>
<%@ Register Src="~/CMSModules/Ecommerce/Controls/Checkout/CartItemOptionsLineList.ascx" TagName="CartItemOptionsLineList"
    TagPrefix="cms" %>
<tr>
  <td class='product-remove'>
    <cms:CartItemRemove runat="server"  CartItemID='<%# Eval("CartItemID")%>' ControlType="image" ControlLabel="remove" ImageURL="~/App_Themes/EcommerceSite/Images/Ecommerce/checkout_remove.png" />
  </td>
  <td class='product-name'>
    <a href='<%# GetAbsoluteUrl(GetDocumentUrl()) %>'>
      <img alt='' src='<%# Eval("SKU.SKUImageShoppingCart") %>'>
      <span>
        <ul class='list-unstyled'>
          <li><%# EvalText("SKU.SKUName", true) %></li>
          <li class='pcode'>Product Code: <%# EvalText("SKU.SKUNumber", true) %></li>
        </ul>
      </span>
    </a>
  </td>
  <td class='product-price'>
    <p class='price'><%# FormatPrice(EvalDouble("UnitTotalPrice"))%></p>
  </td>
  <td class= "shoppingcart-quantity">
    <div>
      <cms:CartItemUnits runat="server" CartItemID='<%# Eval("CartItemID")%>' UnitFormControlName="TextBoxControl" ShowUpdate="true" ImageURL="~/App_Themes/EcommerceSite/Images/Ecommerce/checkout_refresh.png"  ControlType="image" />
    </div>
  </td>
  <td class='product-subtotal'>
    <p class='price'><%# FormatPrice(EvalDouble("UnitTotalDiscount"))%></p>
  </td>
  <td class='product-subtotal'>
    <p class='price'><%# FormatPrice(EvalDouble("TotalPriceIncludingOptions"))%></p>
  </td>
</tr>

**only show shopping cart item number
use default user control:<%@ Register Src="~/CMSModules/Ecommerce/Controls/Checkout/CartItemUnits.ascx" TagName="CartItemUnits" TagPrefix="cms" %>
<cms:CartItemUnits runat="server" CartItemID='<%# Eval("CartItemID")%>' UnitFormControlName="LabelControl" ShowUpdate="false"/>
using System;
using System.Linq;

using CMS.Ecommerce;
using CMS.ExtendedControls;
using CMS.PortalControls;
using CMS.Helpers;
using CMS.Base;

/// <summary>
/// Cart Item Units control for cart content web part transformation
/// </summary>
public partial class CMSModules_Ecommerce_Controls_Checkout_EcobudCartItemUnits : CMSCheckoutWebPart
{
    #region "Variables"

    private int mCartItemID;
    private ShoppingCartItemInfo mShoppingCartItemInfoObject;
    private CMSAbstractWebPart mShoppingCartContent;

    #endregion


    #region "Private properties"

    private bool ReadOnly
    {
        get
        {
            return ValidationHelper.GetBoolean(ShoppingCartContent.GetValue("ReadOnlyMode"), false);
        }
    }


    /// <summary>
    /// The parent web part, in which transformation the current control is placed.
    /// </summary>
    private CMSAbstractWebPart ShoppingCartContent
    {
        get
        {
            if (mShoppingCartContent == null)
            {
                // Gets the parent control
                mShoppingCartContent = ControlsHelper.GetParentControl<CMSAbstractWebPart>(this);

                if (mShoppingCartContent != null)
                {
                    return mShoppingCartContent;
                }
            }

            return mShoppingCartContent;
        }
    }


    /// <summary>
    /// The current ShoppingCartInfo object on which the transformation is applied.
    /// </summary>
    private ShoppingCartItemInfo ShoppingCartItemInfoObject
    {
        get
        {
            return mShoppingCartItemInfoObject ?? (mShoppingCartItemInfoObject = ShoppingCart.CartItems.FirstOrDefault(i => i.CartItemID == CartItemID));
        }
    }

    #endregion


    #region "Public properties"

    /// <summary>
    /// ID of the cart item to handle.
    /// </summary>
    public int CartItemID
    {
        get
        {
            return mCartItemID;
        }
        set
        {
            mCartItemID = ValidationHelper.GetInteger(value, 0);
        }

    }

    #endregion


    #region "Page events"

    /// <summary>
    /// Load event handler.
    /// </summary>
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        SetupControl();
    }


    /// <summary>
    /// Handle the visibility and the properties of the control, which are based on the parent control.
    /// </summary>  
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (ShoppingCartItemInfoObject != null)
        {
            iValue.Value = ShoppingCartItemInfoObject.CartItemUnits.ToString();
        }
    }

    #endregion


    #region "Event handling"

    /// <summary>
    /// Updates the unit count for the current cart item and it's children.
    /// </summary>    
    public void Update(object sender, EventArgs e)
    {
        // Disable update operation for ReadOnly mode
        if (ReadOnly)
        {
            return;
        }

        if ((ShoppingCartItemInfoObject != null) && !ShoppingCartItemInfoObject.IsProductOption)
        {
            var count = ValidationHelper.GetInteger(iValue.Value, -1);
            // Do nothing (leave old value) for invalid/same input
            if ((count < 0) || (count == ShoppingCartItemInfoObject.CartItemUnits))
            {
                return;
            }

            if (count == 0)
            {
                // Delete all item options if available        
                foreach (var option in ShoppingCart.CartItems.Where(scii => scii.CartItemParentGUID == ShoppingCartItemInfoObject.CartItemGUID))
                {
                    ShoppingCartItemInfoProvider.DeleteShoppingCartItemInfo(option);
                }

                // Delete all bundle items if available        
                foreach (var bundleItem in ShoppingCartItemInfoObject.BundleItems)
                {
                    ShoppingCartItemInfoProvider.DeleteShoppingCartItemInfo(bundleItem);
                }

                // Deletes the CartItem from the database
                ShoppingCartItemInfoProvider.DeleteShoppingCartItemInfo(ShoppingCartItemInfoObject.CartItemGUID);
                // Delete the CartItem form the shopping cart object (session)
                ShoppingCartInfoProvider.RemoveShoppingCartItem(ShoppingCart, ShoppingCartItemInfoObject.CartItemGUID);
            }
            else
            {
                ShoppingCartItemInfoProvider.UpdateShoppingCartItemUnits(ShoppingCartItemInfoObject, count);
            }

            // Invalidate the shopping cart so it is recalculated with the correct values
            ShoppingCart.InvalidateCalculations();
            ShoppingCartInfoProvider.EvaluateShoppingCart(ShoppingCart);
        }

        // Raise the change event for all subscribed web parts
        ComponentEvents.RequestEvents.RaiseEvent(sender, e, SHOPPING_CART_CHANGED);
    }

    #endregion


    #region "Methods"

    /// <summary>
    /// Initializes the control.
    /// </summary>
    public void SetupControl()
    {
        if (!StopProcessing)
        {
            
        }
    }

    #endregion

    protected void lbUp_OnClick(object sender, EventArgs e)
    {
        var count = ValidationHelper.GetInteger(iValue.Value, -1);
        iValue.Value = count < 0 ? "0" : (count + 1).ToString();
        Update(null, null);
    }

    protected void lbDown_OnClick(object sender, EventArgs e)
    {
        var count = ValidationHelper.GetInteger(iValue.Value, -1);
        iValue.Value = count < 0 ? "0" : (count - 1 <= 0? "0": (count-1).ToString());
        Update(null, null);
    }
}
<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSModules_Ecommerce_Controls_Checkout_EcobudCartItemUnits" CodeFile="EcobudCartItemUnits.ascx.cs" %>
<%@ Register Src="~/CMSAdminControls/UI/UniControls/UniButton.ascx" TagName="UniButton" TagPrefix="cms" %>

<span class="minus">
    <asp:LinkButton ID="lbDown" runat="server" data-dir='dwn' OnClick="lbDown_OnClick">
        <span class='glyphicon glyphicon-minus' style="color:black;"></span>
    </asp:LinkButton>
</span>
<input class='form-control text-center qty' type='text' value='1' size="4" step="1" min="0" runat="server" ID="iValue"/>
<span class="plus">
    <asp:LinkButton ID="lbUp" runat="server" data-dir='up' OnClick="lbUp_OnClick">
        <span class='glyphicon glyphicon-plus' style="color:black;"></span>
    </asp:LinkButton>
</span>