skip to Main Content

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:

  1. Create a cart and get its id:
  1. Add items to cart by:
    "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


  1. 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

    Login or Signup to reply.
  2. 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:

    protected $_responseFactory;
    protected $_modelSession;
    protected $_url;
    
    public function __construct(
        MagentoFrameworkAppResponseFactory $responseFactory,
        MagentoFrameworkUrlInterface $url,
        MagentoCheckoutModelSession $modelSession
    )
    {
        $this->_responseFactory = $responseFactory;
        $this->_modelSession = $modelSession;
        $this->_url = $url;
    }
    
    public function execute()
    {
        ...
        
        $this->_modelSession->setQuoteId($cartId);
        
        $url = $this->_url->getUrl('checkout');
        $this->_responseFactory->create()->setRedirect($url)->sendResponse();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search