Guides

Basket

Documentation for the Merchello Basket object

πŸ“˜

CustomerItemCacheBase classes

Basket, WishList

Accessing a customer basket

The basket is accessed through an extension method off of the CurrentCustomer property of the CustomerContext.

var basket = CustomerContext.CurrentCustomer.Basket();

Properties

NameDescription
decimal TotalBasketPriceGets the sum of all basket item "amount" multiplied by quantity (price)
bool EnableDataModifiersGets or sets a value indicating whether enable data modifiers.
Guid VersionKeyRead only - Gets the item caches version key
ICustomerBase CustomerRead only - Gets the customer associated with the item cache
LineItemCollection ItemsRead only - Gets the item cache line items
int TotalItemCountRead only - Gets the item cache's item count
int TotalQuantityCountRead only - Gets the sum of all item cache item quantities
decimal TotalItemCachePriceRead only - Gets the sum of all item cache item "amount" multiplied by quantity (price)
bool IsEmptyRead only - Gets a value indicating whether or not the item cache contains any items

Methods

NameDescription
void AddItem(IProduct product)Adds a IProduct to the item cache
void AddItem(IProduct product, int quantity)Adds a IProduct to the item cache
void AddItem(IProduct product, string name, int quantity)Adds a IProduct to the item cache
void AddItem(IProduct product, string name, int quantity, ExtendedDataCollection extendedData)Adds a IProduct to the item cache
void AddItem(ProductDisplay product)Adds a ProductDisplay to the item cache
void AddItem(ProductDisplay product, int quantity)Adds a ProductDisplay to the item cache
void AddItem(ProductDisplay product, string name, int quantity)Adds a ProductDisplay to the item cache
void AddItem(ProductDisplay product, string name, int quantity, ExtendedDataCollection extendedData)Adds a ProductDisplay to the item cache
void AddItem(IProductVariant productVariant)Adds a IProductVariant to the item cache
void AddItem(IProductVariant productVariant, int quantity)Adds a IProductVariant to the item cache
void AddItem(IProductVariant productVariant, string name, int quantity)Adds a IProduct to the item cache
void AddItem(IProductVariant productVariant, string name, int quantity, ExtendedDataCollection extendedData)Adds a IProductVariant to the item cache
void AddItem(ProductVariantDisplay productVariant)Adds a IProductVariant to the item cache
void AddItem(ProductVariantDisplay productVariant, int quantity)Adds a ProductVariantDisplay to the item cache
void AddItem(ProductVariantDisplay productVariant, string name, int quantity)Adds a ProductVariantDisplay to the item cache
void AddItem(ProductVariantDisplay productVariant, string name, int quantity, ExtendedDataCollection extendedData)Adds a ProductVariantDisplay to the item cache
void AddItem(string name, string sku, decimal price)Adds a item to the item cache
void AddItem(string name, string sku, int quantity, decimal price)Adds a item to the item cache
void AddItem(string name, string sku, int quantity, decimal price, ExtendedDataCollection extendedData)Adds a item to the item cache
void AddItem(IItemCacheLineItem lineItem)Adds a item to the item cache
void UpdateQuantity(Guid key, int quantity)Updates the quantity of an item in the item cache
void UpdateQuantity(string sku, int quantity)Updates the quantity of an item in the item cache
void UpdateQuantity(IProductVariant productVariant, int quantity)Updates the quantity of an item in the item cache
void RemoveItem(Guid itemKey)Removes a product variant from the item cache
void RemoveItem(string sku)Removes a product variant from the item cache
void RemoveItem(IProductVariant productVariant)Removes a product variant from the item cache
void Empty()Empties the item cache
void Refresh()Refreshes cache with database values
bool Validate()Validates values stored in the internal item cache to make certain items being purchase
reflect most recent values in the back office.
void Accept(ILineItemVisitor visitor)Accepts visitor class to visit item cache items

Examples - Adding an Item to the Basket

/// <summary>
    /// Simple Model for the Add To Cart form.
    /// </summary>
    public partial class AddItemModel
    {
        /// <summary>
        /// Gets or sets the Content Id of the ProductDetail page
        /// </summary>
        public int ContentId { get; set; }

        /// <summary>
        /// Gets or sets the basket page id.
        /// </summary>
        public int BasketPageId { get; set; }

        /// <summary>
        /// Gets or sets the wish list page id.
        /// </summary>
        public int WishListPageId { get; set; }

        /// <summary>
        /// Gets or sets the product.
        /// </summary>
        public ProductDisplay Product { get; set; }

        /// <summary>
        /// Gets or sets the option choices (if there are any), used to determine the variant 
        /// </summary>
        public Guid[] OptionChoices { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether show wish list.
        /// </summary>
        public bool ShowWishList { get; set; }

        /// <summary>
        /// Gets or sets the currency.
        /// </summary>
        public ICurrency Currency { get; set; }
    }
[HttpPost]
public ActionResult AddToBasket(AddItemModel model)
{


  // We've added some data modifiers that can handle such things as including taxes in product
  // pricing.  The data modifiers can either get executed when the item is added to the basket or
  // as a result from a MerchelloHelper query - you just don't want them to execute twice.

  var merchello = new MerchelloHelper(false);

  var product = merchello.Query.Product.GetByKey(model.Product.Key);

  // In the event the product has options we want to add the "variant" to the basket.
  // -- If a product that has variants is defined, the FIRST variant will be added to the cart. 
  // -- This was done so that we did not have to throw an error since the Master variant   is no
  // -- longer valid for sale.
  if (model.OptionChoices != null && model.OptionChoices.Any())
  {
   var variant = product.GetProductVariantDisplayWithAttributes(model.OptionChoices);
                this.Basket.AddItem(variant, variant.Name, 1, extendedData);
  }
  else
  {
     this.Basket.AddItem(product, product.Name, 1, extendedData);
  }

   this.Basket.Save();
}

Example - Updating Basket Quantities

/// <summary>
/// The basket table model.
/// </summary>
public partial class BasketTableModel : ItemCollectionTable
{
        /// <summary>
        /// Gets or sets the checkout page.
        /// </summary>
        public IPublishedContent CheckoutPage { get; set; }

        /// <summary>
        /// Gets or sets the currency.
        /// </summary>
        public ICurrency Currency { get; set; }

        /// <summary>
        /// Gets or sets the basket line items.
        /// </summary>
        public BasketLineItem[] Items { get; set; }

        /// <summary>
        /// Gets the total price.
        /// </summary>
        public decimal TotalPrice { get; internal set; }

        /// <summary>
        /// Gets or sets a value indicating whether show wish list buttons.
        /// </summary>
        public bool ShowWishList { get; set; }
}
/// <summary>
/// Responsible for updating the quantities of items in the basket
/// </summary>
/// <param name="model">The <see cref="IEnumerable{T}"/></param>
/// <returns>Redirects to the current Umbraco page (the basket page)</returns>
[HttpPost]
public ActionResult UpdateBasket(BasketTableModel model)
{
     if (!this.ModelState.IsValid) return this.CurrentUmbracoPage();

     // The only thing that can be updated in this basket is the quantity
     foreach (var item in model.Items.Where(item => this.Basket.Items.First(x => x.Key == item.Key).Quantity != item.Quantity))
     {
         this.Basket.UpdateQuantity(item.Key, item.Quantity);
     }

     this.Basket.Save();

     return this.CurrentUmbracoPage();
}