2

I am currently working on a combo offer on NopCommerce. So I need to add multiple product to cart at a single click. The built in NopCommerce format is for adding single product into cart is

AjaxCart.addproducttocart_catalog('/addproducttocart/catalog/' + productId + '/1/1'

and

AjaxCart.addproducttocart_details('/addproducttocart/details/' +productId + '/1', '#product-details-form')

Both of them work fine for adding single product. But when I want to add multiple product then the it just add single product to the cart. Mention that I am sending a string with a coma separative value which is list of products and inside Javascript it is parsing as a single prodcut Id. However, it is adding only single product in the cart. Which Product Id is the the lowest that product is adding to the cart.

Here is my piece of javascript code

function addComboProductToCart(ids) {
        var arrayOfStrings = ids.split(',');
        for (var i = 0; i < arrayOfStrings.length; i++) {
            AjaxCart.addproducttocart_catalog('/addproducttocart/catalog/' + arrayOfStrings[i] + '/1/1');
        }
    }

But it is not showing a single error too. So where is the problem?

Shoumen Agdm
  • 187
  • 15

1 Answers1

3

At the first you need to change in public.ajaxcart.js as below as there is an ajax call on every Add to Cart we need to set it async:false for this you have to add a parameter called async for more clear take a look at below code

//add a parameter async
    addproducttocart_catalog: function (urladd,async) {
        if (this.loadWaiting != false) {
            return;
        }
        this.setLoadWaiting(true);

        $.ajax({
            cache: false,
            async:async,
            url: urladd,
            type: 'post',
            success: this.success_process,
            complete: this.resetLoadWaiting,
            error: this.ajaxFailure
        });
    },

Now i have modified your function by addding just a parameter false

function addComboProductToCart(ids) {
    var arrayOfStrings = ids.split(',');
    for (var i = 0; i < arrayOfStrings.length; i++) {

       AjaxCart.addproducttocart_catalog('/addproducttocart/catalog/' + arrayOfStrings[i] + '/1/1',false);

    }
}

To know what async:false is doing kindly find this answer as well for more clarification

Let me know if you need any further assistance.

Community
  • 1
  • 1
Curiousdev
  • 5,668
  • 3
  • 24
  • 38
  • 2
    Yeah!!!!!, it's working fine. Hats off!! But I have one question regarding this update on NopCommerce Base file. As I am developing a plugin so my plugin wont be able to access(`public.ajaxcart.js`) the Base file therefore do I have to write the same function with that modification on my own way on the script section?? – Shoumen Agdm Feb 01 '17 at 12:29
  • 2
    Ohh yes you have to add that function for your plug in I know it's bad practice to write the same code to everywhere but I'll analyze on that once I found any solution I'll update you – Curiousdev Feb 01 '17 at 18:36
  • Thanks for being so kind. I am looking for a solution too. – Shoumen Agdm Feb 02 '17 at 06:26
  • did you find any reliable solution regarding this problem?? – Shoumen Agdm Feb 08 '17 at 05:17