The information in this thread might be outdated
If the solution did not help, please, check our Help Portal or contact Ecwid Team.
 
 
Thread Tools Display Modes
  #1  
Old 01-12-2015, 04:01 PM
Web Dev1's Avatar
Web Dev1 is offline
 
Join Date: Dec 2014
Posts: 9
Default

javaScript API addProduct() problem adding more than one product to cart


Hello - I am trying to add multiple different products to the cart programmatically using your JS API.

I am able to add one product and quantity using the JS API but when I try to add another product it seems to be over-writing the first product I added to the cart.

Here is the code I'm using:

Code:
<script type="text/javascript" src="http://app.ecwid.com/script.js?xxxxxxx" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8"> xMinicart("style=","layout=Mini"); </script>


<script type="text/javascript" charset="utf-8">
var jsProductIds = [1,2,3,4]
var jsProdQty = [2,6,1,3]
for (var i = 0; i < jsProductIds.length; i++) {
    Ecwid.OnAPILoaded.add(function(additems) {
    var product={ id: jsProductIds[i], quantity: jsProdQty[i] };
    Ecwid.Cart.addProduct(product);
	alert(jsProductIds[i]); // these alerts show that the code is working
	alert(jsProdQty[i]);    // but ecwid is only placing the last product/qty in the cart
    });
}
</script>
What would be the correct way to add multiple products to the cart?
Thanks!
  #2  
Old 01-13-2015, 02:11 AM
Web Dev1's Avatar
Web Dev1 Web Dev1 is offline
 
Join Date: Dec 2014
Posts: 9
Default

I figured it out... I just had to move the for loop...

For anyone else's benefit that might need to know... Here's the code that works:

Code:
<script type="text/javascript" charset="utf-8">
var jsProductIds = [1,2,3,4] var jsProdQty = [2,6,1,3] Ecwid.OnAPILoaded.add(function(additems) { for (var i = 0; i < jsProductIds.length; i++) { var myprod = jsProductIds[i]; var myqty = jsProdQty[i]; var product={ id: myprod, quantity: myqty }; Ecwid.Cart.addProduct(product); } });
</script>
The Following 2 Users Say Thank You to Web Dev1 For This Useful Post:
enot (01-13-2015), Sergio87 (01-13-2015)
  #3  
Old 01-13-2015, 09:16 AM
Sergio87's Avatar
Sergio87 Sergio87 is offline
Ecwid Team
 
Join Date: Mar 2014
Posts: 1,512
Default

Quote:
Originally Posted by Web Dev1 View Post
I figured it out... I just had to move the for loop...

For anyone else's benefit that might need to know... Here's the code that works:

Code:
<script type="text/javascript" charset="utf-8">
var jsProductIds = [1,2,3,4] var jsProdQty = [2,6,1,3] Ecwid.OnAPILoaded.add(function(additems) { for (var i = 0; i < jsProductIds.length; i++) { var myprod = jsProductIds[i]; var myqty = jsProdQty[i]; var product={ id: myprod, quantity: myqty }; Ecwid.Cart.addProduct(product); } });
</script>
Hello!

Thank you for your messages. I am really glad that you were able to implement the script you need.

Please feel free to explore this article about Javascript API in our Help Centre where you can find some useful examples: http://help.ecwid.com/customer/porta...api#Ecwid.Cart

Also please refer to this source to learn more about Ecwid API v3: http://developers.ecwid.com/api-documentation

It might be helpful for you.
__________________
Sergey G., Ecwid Team
------------------
Welcome to our Help Center with a lot of answers and tips!

Last edited by Vitaliy G; 01-04-2016 at 12:28 PM.
 
The information in this thread might be outdated
If the solution did not help, please, check our Help Portal or contact Ecwid Team.

Tags
add multiple products, addproduct, javascript api

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:42 AM.
Powered by vBulletin® Version 3.8.11. Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.