4

I want to place an order for a customer from API. Currently, I do:

  1. Login as integration (OAuth).
  2. Create customer
  3. Create customer cart (or take an existing one).
  4. Remove all items from the cart.
  5. Add items to the cart.
  6. Place order.

A customer may have only 1 active cart. I must use the customer cart from Magento and remove the customer's products from his cart. This is not good.

How can I use the cart without touching an existing customer cart?

1

I assume you add items by posting to

[POST] /rest/V1/carts/mine/items

But you can also post to a specific cart id

[POST] /rest/V1/carts/{{cart-id}}/items

Hope it helps..

  • Yes, I can. But I would like to work with more user cart. – Mykhailo Shatilov Aug 22 '16 at 12:49
  • I guess i fail to understand what you want to do.. – Mackelito Aug 22 '16 at 13:11
  • Currently we have 1 user = 1 cart, I want to have 1 user = n carts. – Mykhailo Shatilov Aug 29 '16 at 13:08
  • Perhaps you could use wishlist instead? – Mackelito Aug 29 '16 at 13:09
  • Unfortunately no. I'm developing a system with a creation of order and I need to work with a cart. I know the other way, but it is very complicated. If you're wondering devdocs.magento.com salesOrderRepositoryV1 PUT. – Mykhailo Shatilov Aug 30 '16 at 19:27
0

Here is sample Code:

 if ($customerId) {
                //START create cart
                $apiUrl =$baseUrl."rest/V1/customers/".$customerId."/carts";
                $quoteId = $this->curlCall($apiUrl, '', 'POST', $setHaders);
                //END create cart

               //START add product to cart
                $productArray = array(
                    "cartItem" => array(
                        "sku" => $skuToAddToCart,
                        "qty" => $rawDataArr['qty'],
                        "quote_id" => $quoteId,
                        "product_option" => array(
                            "extension_attributes" => $productOptions
                        )
                    )
                );
                $preparedPost = str_replace('\"', '', json_encode($productArray));
                $apiUrl = $baseUrl."rest/V1/carts/$quoteId/items";
                $this->curlCall($apiUrl, $preparedPost, 'POST', $setHaders);
                //END add product to cart

                //START add shipping address to cart
                $preparedPost = str_replace('\"', '', $shippingAddressJSON);
                $apiUrl = $baseUrl.'rest/V1/carts/'.$quoteId.'/shipping-information';
                $this->curlCall($apiUrl, $preparedPost, 'POST', $setHaders);
                //END add shipping address to cart

                //START place order
                $preparedPost = str_replace('\"', '', $paymentMethodJSON);
                $apiUrl = $baseUrl.'rest/V1/carts/'.$quoteId.'/order';
                $orderId = $this->curlCall($apiUrl, $preparedPost, 'PUT', $setHaders);
                //END place order
            }
  • Hi @vishal ! Did you find a solution? – where Oct 17 '18 at 21:00
  • @where I have used above script to create cart for customer. – Vishal Oct 23 '18 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.