# Add Button to Add to Cart

**URL:** <https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [January 2, 2019, 5:04pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693 "2019-01-02T17:04:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![info84220](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info84220/32/7898_2.png) [@info84220](https://forum.wixstudio.com/u/info84220)\
**Post date:** [January 2, 2019, 5:04pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/1 "2019-01-02T17:04:24Z")

</div>

Hello

I am having trouble setting up my button to add to cart, can someone please give me a break down of what I need to do

---

<div class="post-metadata">

**Author:** ![stevendc](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stevendc/32/669_2.png) [@stevendc](https://forum.wixstudio.com/u/stevendc)\
**Post date:** [January 2, 2019, 11:05pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/2 "2019-01-02T23:05:20Z")

</div>

What is the url for your page? Can you share the code you are using?

---

<div class="post-metadata">

**Author:** ![info84220](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info84220/32/7898_2.png) [@info84220](https://forum.wixstudio.com/u/info84220)\
**Post date:** [January 2, 2019, 11:07pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/3 "2019-01-02T23:07:39Z")

</div>

Hi Steve, the url for my website is [https://www.creationssublime.com/gourmet-meal-for-one](https://www.creationssublime.com/gourmet-meal-for-one)

I am playing around on a duplicated site. I’ve created a repeater and I am trying to use the button as a faster add to cart feature. I am not an experienced coder and I am trying to use code through other forums.

Thank you for the response

---

<div class="post-metadata">

**Author:** ![stevendc](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stevendc/32/669_2.png) [@stevendc](https://forum.wixstudio.com/u/stevendc)\
**Post date:** [January 3, 2019, 8:18am UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/4 "2019-01-03T08:18:54Z")

</div>

@info84220 OK so here are the key elements you need to work with:

1. [Shopping Cart Icon](https://www.wix.com/code/reference/$w.CartIcon.html) :

![](https://us1.discourse-cdn.com/wix/original/3X/3/8/3818656af7b93d13309974b6cb55526cce79bdc2.png)  
This is the element in Wix Code that gives you access to the [addToCart()](https://www.wix.com/code/reference/$w.CartIcon.html#addToCart) function. It can be accessed like this:

```javascript
$w('#shoppingCartIcon1').addToCart(<product id>);

```

The syntax for his is:

 ![](https://us1.discourse-cdn.com/wix/original/3X/4/5/4550cf6b09ef884d4ff35bc5ccfef15d9adbaf33.png)  
2. Repeated Item Scope Selectors: When you add a button to a repeater you are placing it inside of a container which is duplicated inside of a repeater element. So as a simple example:

```javascript
repeater-start
    container #1 start
        addToCartButton
    container #1 end
    container #2 start
       addToCartButton
    container #2 end
repeater-end

```

When the event handler for onClick() is called upon a button click you have to determine which container the onClick() was called from (the repeater item scope). You do this using the $w.at( ) function.

```javascript
$w.onReady( function () {
   $w("#myRepeatedImage").onClick( (event) => {
     let $item = $w.at(event.context); // Get container scope
     $item("#myRepeatedText").text = "Selected";
   } );
 } );

```

![](https://us1.discourse-cdn.com/wix/original/3X/5/e/5ef7800090dc5cc777524982c8cfd104fc0c762f.png)  
Now assuming you have connected a Stores/Products collection to a dataset with the ID (\_id) of #dataset1.

Also assuming that you have connected #dataset1 to your repeater and its product elements then you can access the item from #dataset1 that is used by the container that the onClick() function was called from like this:

```javascript
let selectedProduct = $item('#dataset1').getCurrentItem();

```

The selectedProduct will be a row from the data collection that the dataset is connected to and will include the ID field for the product. We use the field key, ‘\_id’, to access the ID field like this:

```javascript
let productId = selectedProduct._id;

```

Now you have the product Id you can add it to your cart:

```javascript
$w('#shoppingCartIcon1').addToCart(productId);

```

So this will be what your code looks like:

```javascript
$w.onReady( function () {
    $w("#myRepeatedImage").onClick( (event) => {
        let $item = $w.at(event.context); // Get container scope
        let selectedProduct = $item('#dataset1').getCurrentItem();
        let productId = selectedProduct._id;
        $w('#shoppingCartIcon1').addToCart(productId)
        .then(() => {
            // Item added to the shopping cart
            console.log("add product ${productId} success");
        })
        .catch((error) => {
            // Catch an error that occurs
            console.log("Error: ${error.stack}");
        });
    } );
} );

```

Here’s a cheat sheet that might help:

 ![](https://us1.discourse-cdn.com/wix/original/3X/7/f/7fb27d9ff6c078c8378dddfb7b716292367d4769.png)  
Good luck

---

<div class="post-metadata">

**Author:** ![info84220](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info84220/32/7898_2.png) [@info84220](https://forum.wixstudio.com/u/info84220)\
**Post date:** [January 3, 2019, 6:14pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/5 "2019-01-03T18:14:27Z")

</div>

@stevendc  
Thank you for the detailed reply. I’ve been trying out your tips and it feels like I am getting closer Here is the code I’ve written

* * *

$w.onReady( function () {  
$w(“#button98”).onClick( (event) =\> {  
let $item = $w.at(event.context);

let selectedProduct = $item(‘#dataset1’).getCurrentItem();  
let productId = selectedProduct.\_id;  
$w(‘#shoppingCartIcon1’).addToCart(productId)  
.then(() =\> {  
console.log(“add product ${productId} success”);  
})  
. catch ((error) =\> {  
console.log(“Error: ${error.stack}”);  
});

});

});

I am now getting an error message when the page is in preview mode and I click the button (#button98). It says Error: $(error.stack) Line 14

My image for the page is connected to my dataset images. My title is connected to the dataset title, and price is set up to the price in my dataset

I really appreciate your detailed response.

 ![](https://us1.discourse-cdn.com/wix/original/3X/7/3/7370fbb42044cc60f8bdd93329baac9e949265a7.png)

 ![](https://us1.discourse-cdn.com/wix/original/3X/1/1/11f5480f4d82a00aa084332998d3360f20d4dc7b.png)

---

<div class="post-metadata">

**Author:** ![stevendc](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stevendc/32/669_2.png) [@stevendc](https://forum.wixstudio.com/u/stevendc)\
**Post date:** [January 4, 2019, 11:01am UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/6 "2019-01-04T11:01:35Z")

</div>

OK change the line  
console.log(“Error: ${error.stack}”);  
to  
console.log(”Error: “+(error.stack? error.stack:error.message));

If that fails (it should’nt unless there is a syntax error) just log the error object:

console.log(error);

Cheers  
Steve

---

<div class="post-metadata">

**Author:** ![info84220](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info84220/32/7898_2.png) [@info84220](https://forum.wixstudio.com/u/info84220)\
**Post date:** [January 4, 2019, 3:10pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/7 "2019-01-04T15:10:44Z")

</div>

Thank you for the response again. I made the change you suggested and went with line  
console.log(error);

my code now looks like

$w.onReady( **function** () {  
$w(“#button98”).onClick((event) =\> {  
**let** $item = $w.at(event.context);

**let** selectedProduct = $item(‘#dataset1’).getCurrentItem();  
**let** productId = selectedProduct.\_id;  
$w(‘#shoppingCartIcon1’).addToCart(productId)  
.then(() =\> {  
console.log(“add product ${productId} success”);  
})  
. **catch** ((error) =\> {  
console.log(error);  
});

```
}); 

```

});

#button98 is the id of my button Add to Cart  
#dataset1 is the id of my Dataset  
#shoppingCartIcon1 is the id of my shooping card

The error I am now receiving says

Array(1)  
0:  
“{"code":"CANNOT\_ADD\_CART","commandName":"AddToCart","message":"option Portion Size for product with id 3803a691-3f56-c07e-20f6-2078b915b3d8 is required","field":""}”

It feels like we are getting close! Thank you again for giving your time to help me

---

<div class="post-metadata">

**Author:** ![stevendc](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stevendc/32/669_2.png) [@stevendc](https://forum.wixstudio.com/u/stevendc)\
**Post date:** [January 5, 2019, 11:55am UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/8 "2019-01-05T11:55:18Z")

</div>

Hi there,

Well this is where you really need to read and understand the API documentation.

As I mentioned above you need to look at the addToCart() api

> **[CartIcon - Velo API Reference - Wix.com](https://www.wix.com/velo/reference/$w/carticon)**
>
> An icon that leads users to the shopping cart.

You will see there are other arguments that you can give while adding an product. How many products (quantity) and any optional features (options).  
You appear to have a product that has options that are required that you have configured in your store. You need to add an option field for the one you require which seems to be “Portion Size”.

So once you ha e the Option you can use it by setting quantity to 1 or more and then add a quantity property & value in an addToCartOptions object.

Time for some bookwork and review of any related examples.

Cheers  
Steve

---

<div class="post-metadata">

**Author:** ![SCW](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/scw/32/9173_2.png) [@SCW](https://forum.wixstudio.com/u/SCW)\
**Post date:** [November 29, 2019, 1:14am UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/9 "2019-11-29T01:14:18Z")

</div>

I have been reading this post and trying to see if I am missing something.  
(looks like the post originator ended up not being able to solve his issue either as his site seems to be using the default store apps)  
The code works but it doesn’t pick the right product. That is because the repeater is connected to another dataset to collect the unique product info. The same products also exist in the stores database but naturally they have different Product IDs. Is it possible to connect the repeater items from a custom dataset to the store dataset so the Add To Cart selects the right item in each container?  
Thank you.

---

<div class="post-metadata">

**Author:** ![priyeshggupta5](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/priyeshggupta5/32/15709_2.png) [@priyeshggupta5](https://forum.wixstudio.com/u/priyeshggupta5)\
**Post date:** [February 18, 2020, 3:50pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/10 "2020-02-18T15:50:20Z")

</div>

hey were you able to solve the issue?

---

<div class="post-metadata">

**Author:** ![SCW](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/scw/32/9173_2.png) [@SCW](https://forum.wixstudio.com/u/SCW)\
**Post date:** [February 18, 2020, 7:30pm UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/11 "2020-02-18T19:30:54Z")

</div>

I was able to fix it with code. Depending on the structure of your databases this can be different so I am not sure how to help without seeing your code or database setup. One thing you should look into is setting up "reference fields."This will help connecting items between databases.

---

<div class="post-metadata">

**Author:** ![priyeshggupta5](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/priyeshggupta5/32/15709_2.png) [@priyeshggupta5](https://forum.wixstudio.com/u/priyeshggupta5)\
**Post date:** [February 19, 2020, 11:28am UTC](https://forum.wixstudio.com/t/add-button-to-add-to-cart/14693/12 "2020-02-19T11:28:52Z")

</div>

@info52 would you care to take a look at my website? [https://priyeshggupta5.wixsite.com/mysite-4](https://priyeshggupta5.wixsite.com/mysite-4)  
this is the url of my duplicate editor where i am testing the codes. Your help will be really appreciated.  
Thank you
