In an web or mobile app, we build a list of items to be ordered from a Magento2 eComm website.
The plan is to programmatically create the cart in Magento2 and open the checkout page in the browser for the user to finish the purchase.
I managed so far to:
- Create a cart and get its id:
- Post to: https://my-domain.com/rest/V1/guest-carts
- Returns "id-that-look-like: R5AiDfCG5aOUZnomRe8TVXEolAM8WadH"
- Add items to cart by:
- Post to: https://my-domain.com/rest/V1/guest-carts/R5AiDfCG5aOUZnomRe8TVXEolAM8WadH/items with data:
"cartItem": {
"sku": "-my-sku-",
"qty": 3
}
}
Received as answer json:
{
"item_id": 22048,
"sku": "-my-sku-",
"qty": 3,
"name": "... product name...",
"price": 10.86,
"product_type": "simple",
"quote_id": "11212"
}
And that’s it. I did not manage to open the checkout page with the cart-id that have the products in it.
I tried with: https://my-domain.com//checkout/?cartId=11212 (that is quote_id, also tried with item_id and the alphanumeric id). Nothing works.
How do I open the checkout page with the cart-id I received? so the products I added are in the cart?
Or is there another approach to achieve the desired result?
2
Answers
before check out, magento need set shipping address and set shipping method.
please check https://devdocs.magento.com/guides/v2.4/graphql/tutorials/checkout/checkout-place-order.html
I had the same problem and find solution here https://community.magento.com/t5/Magento-2-x-Programming/Magento-2-guest-carts-api-to-checkout-page/td-p/51065.
Add in constructor MagentoCheckoutModelSession and call setQuoteId($cartId), this work for me: