cancel
Showing results for 
Search instead for 
Did you mean: 

REST add to cart of current visitor

REST add to cart of current visitor

I would like to be able to add items to the current visitors cart via REST from the magento site all via Javascript on the front-end.   The item to add is configured outside of Magento but must be ordered within Magento.   Here is my situation:

 

1) Visitor (either anonymous or logged in) comes to site

2) Lands on page (called landing page)  that loads ui and data from 3rd party site (via javascript CORS).

3) Javascript event is raised from 3rd party site

4) Landing page front-end listens for event and calls the REST api for add to cart

5) Visitor goes to cart page and sees product added.

 

Step 1 and 5 is standard Magento

Steps 2 and 3 - I know how to do and have it working

Step 4 is causing the trouble.

 

How do I know the customer id or cart if of the current visitor via the front-end?

Can the REST api be called so that  /carts/mine will reference the current visitor's cart (either anonymous or logged in)?  I would assume the phpsessionid would do that or some other cookie that is set on the browser.

 

Thanks.

5 REPLIES 5

Re: REST add to cart of current visitor

I am trying to do something very similar and am having problems as well. It seems like this should be fairly straight forward but I just can't get it.

 

I'm looking at the API call...

quoteGuestCartManagementV1 : Cart Management interface for guest carts

 

Surely I'm missing something completely obvious. Did you have any luck @jschneller?

 

Re: REST add to cart of current visitor

Here is what I did although not sure if it is the "right" way of doing it.  

 

I modified one of the header phtml files to push the cart id into a javascript variable.

 

Then I used the non-guest cart management REST calls to add the item to the cart via javascript that I added to the page.

 

This works except that the REST call does not fire the added to cart event so the cart item count on the page (upper right corner in the default template) is not refreshed.  Since the REST call doesn't fire the event then my observer to set the price of the item added is never called.

 

Things I don't like:

header.phtml code needs to create a cart and put the id into a javascript variables -- seems hackish

events are not fired.  I need this to happen for my use case

Re: REST add to cart of current visitor

Hi,

 

I thing we have the same problem to solve

I want a connect a shirtconfigurator other platform than Magento 2 (it is .Net) iframed

from there I want to add to cart  to MAGENTO 2

 

I do not know how add to cart in MAGENTO 2

Do you have found solution ?  I am intresterd thanks

 

 

 

 

Re: REST add to cart of current visitor

Hi,

 

I join the party. I also want to add products to the cart of the current visitor. The item to add is customized in a configurator, this conifugrator will be displayed in the Magento store via iframe.

 

I also listen for a "Add to cart" event and then try to add the product to the cart. 

The only thing I've achieved so far: getting the quote id, create a new anonymous cart, add the product to the anonymous cart. 

 

Problems:

  • The cart is not in anyway associated with the currently active client/user, it's just an invisible cart where I add the product(s). I can interact with the cart via API, but it's not shown to the client
  • Furthermore this code only works for guests and not for customers who are logged in
  • A new cart will be created each time, ignoring existing user carts

 

I've already posted a question on the Magento Stackexchange site, but there is no answer so far. https://magento.stackexchange.com/questions/176842/add-to-cart-via-javascript-in-ce-2-1

Re: REST add to cart of current visitor

I finally found a solution which works for me.

 

https://magento.stackexchange.com/a/178747/5222