We are in a project where Magento 2 CE is used as the commerce service provider. Usually when a guest user who has items in Cart login the cart gets merged to the customer's cart and the guest cart will be deleted. How can the same be achieved using Rest APIs in Magento 2? I know that in WCS, we needed to pass the guest token as a header to the Registered user login Rest API. Can someone help us on this?
-
hi @ many vijayan whether you got the solution for this api?– Nagaraju KasaNov 2, 2017 at 10:00
2 Answers
Use the below steps: Method - PUT
{URL}/rest/V1/guest-carts/{guest-card-id}
Param - {
"customerId": 3,
"storeId": 1
}
Authorization -"Bearer {user-token}"
It will assign the guest cart to a logged-in user
-
3what if the customer already having an active cart ? etting rror " Cannot assign customer to the given cart. Customer already has active cart." Nov 30, 2018 at 15:11
-
@AnandOntigeri here is related issue github.com/magento/magento2/issues/24808 and workaround github.com/troublediehard/plab-cartsmergefix Nov 1, 2019 at 22:39
I got stuck on the same thing. I didn't have any issues using the Magento REST API when I was logged-in but I ran into a bunch of problems using the guest cart API including connecting a guest cart initialized from the REST API to the user session in the backend and transitioning the cart from the guest state to the logged-in state.
In the end I created a custom web service module so that I could do this logic in PHP.
http://alankent.me/2015/07/24/creating-a-new-rest-web-service-in-magento-2/
https://blog.amasty.com/how-to-create-a-magento-2-extension/
This approach worked well for me so I wanted to put it out there as an option. I find that the Magento documentation for its REST API isn't as good as it could be.
-
1