Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Puck Holshuijsen 184 posts 727 karma points
    Jul 18, 2017 @ 14:21
    Puck Holshuijsen
    0

    Adding multiple products to basket at the same time

    Hi Guys,

    Maybe anybody of you could help me. I am trying to add multiple products at once, while pushing the add to cart button only 1 time.

    So for example: I am selling a stove and you get the option of an extra tray you could buy. This is an extra product. And I want to add the stove AND the tray in the basket.

    Here is the code I used, but it isn't working: inside public virtual ActionResult AddBasketItem(TAddItem model)

     public virtual ActionResult AddBasketItem(TAddItem model)
        {
            // Instantiating the ExtendedDataCollection in this manner allows for additional values 
            // to be added in the factory OnCreate override.
            // e.g. if you need to store custom extended data values, create your own factory
            // inheriting from BasketItemExtendedDataFactory and override the "OnCreate" method to store
            // any addition values you have added to the model
            var extendedData = this._addItemExtendedDataFactory.Create(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.
    
            // In this case we want to get the product without any data modification
            var merchello = new MerchelloHelper(false);
    
            try
            {
                var product = merchello.Query.Product.GetByKey(model.ProductKey);
    
                // ensure the quantity on the model
                var quantity = model.Quantity <= 0 ? 1 : model.Quantity;
    
    this.Basket.AddItem(product, product.Name, quantity, extendedData);
    
                if (model.OptionChoices != null && model.OptionChoices.Any())
                {
                    foreach (var option in model.OptionChoices)
                    {
                        var productOption = merchello.Query.Product.GetByKey(option);
                        this.Basket.AddItem(productOption, productOption.Name, quantity, extendedData);
                    }
                }
    
    
    
                this.Basket.Save();
    
                // If this request is not an AJAX request return the redirect
                return this.HandleAddItemSuccess(model);
            }
            catch (Exception ex)
            {
                var logData = GetBaseLoggingData();
                logData.AddCategory("Controllers");
                MultiLogHelper.Error<BasketControllerBase<TBasketModel, TBasketItemModel, TAddItem>>("Failed to add item to basket", ex, logData);
                return this.HandleAddItemException(model, ex);
            }
        }
    

    i am add the stove, and after that i am going through the options. I tweaked the code, so it gets the guid's of my options. But for some reason it adds the stove twice, and in the first line i get double amount and the second line i get the single price.

    Can anybody point me in the right direction?

    Thanks in advance. Puck

  • Puck Holshuijsen 184 posts 727 karma points
    Jul 19, 2017 @ 05:53
    Puck Holshuijsen
    100

    I found it out myself! I was using the extended data of the product. Deleted that for the option, and now it is working!

Please Sign in or register to post replies

Write your reply to:

Draft