I am integratting my site with the magento 2 api. I can insert products into the cart by guest-carts api rest, that's ok, but how do I get this guest-cart created by api to be read at the checkout page from magento.
I am doing with php and javascript. any solution makes me happy please!
Just wondering if you ever got this figured out? I am working on the same type of integration and confused about how to associate a created guest cart with the user's current session.
Any help is GREATLY appreciated!
The way I managed to associate a guest-cart with a customer was like so (AJAX calls, written synchronously for ease of understanding).
let guestCartQuoteId = client.post("V1/guest-carts")
// ... log in with user ...
let customerCartId = client.post("V1/carts/mine")
let guestCart = client.get(`V1/guest-carts/${guestCartQuoteId}`)
let customerCart = client.get('V1/carts/mine')
let newCart = {
"quote": {
"id": guestCart.id, //this is the numeric id for a guest-cart
"customer": {
"id": customerCart.customer.id //this is the numeric id for the customer
}
}
}
client.put('V1/carts/mine', newCart) // this assigns the guest-cart to the customer that is logged in
You can send the cart id in the checkout url like this http://example.com/checkout/?cartId=22, now in magento create a observer for checkout action predispatch(controller_action_predispatch_checkout_index_index), in this observer if the url has cartId then you can simply set the cart id to current guest checkout session, like this:
$observer->create("Magento\Quote\Model\QuoteFactory")->create()->load(22)->setActive(1);
$objectManager->get("Magento\Checkout\Model\Session")->setQuoteId(22);
more better approach will be to send some kind of token to the url and then decode the token to get the cart id and than set the cart id to current session. This is not the best way, but it works
@ashu_webkul could you please provide an example with a custom module that can do this?