4

Add multiple products to cart using Magento2 REST API using custom method Their is no default magento api this is single product add to cart json code in rest api body section.

{ "cartItem": 
   { 
     "quote_id": "ee6e72fc6e3226558197d23cb1f88cbe", 
     "sku": "24-MB02",
     "qty": 1
   }
}

i am trying to modify the above code to bellow format and did respected changes in webapi.xml, inteface and model

{ "cartItem": 
  [
   { 
     "quote_id": "ee6e72fc6e3226558197d23cb1f88cbe", 
     "sku": "24-MB02",
     "qty": 1
   },
  { 
    "quote_id": "ee6e72fc6e3226558197d23cb1f88cbe", 
    "sku": "24-MB03",
    "qty": 2 
   }
 ]
}

vendor/magento/module-quote/etc/webapi.xml

<route url="/V1/guest-cart/:cartId/item" method="POST">
    <service class="Magento\Quote\Api\GuestCartItemRepositoryInterface" method="saveData"/>
    <resources>
        <resource ref="anonymous" />
    </resources>
</route>

vendor/magento/module-quote/Api/GuestCartItemRepositoryInterface.php

    public function saveData(\Magento\Quote\Api\Data\CartItemInterface $cartItem);

vendor/magento/module-quote/Model/GuestCart/GuestCartItemRepository.php

in model i am stuck

public function saveData(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
{
    /** @var $quoteIdMask QuoteIdMask */
    $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartItem->getQuoteId(), 'masked_id');
    $cartItem->setQuoteId($quoteIdMask->getQuoteId());
    return $this->repository->save($cartItem);
}

is it possible Can any one please hlep me, Thanks.

-1

Here is the answer please find bellow link, it is divided in to two parts for Gest and Cstomer, just wrote a custom method for it

https://magentosnipets.blogspot.in/2017/11/add-multiple-products-to-cart-using.html

Thanks.

  • blog not found. – user4536 Jun 28 at 12:35

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.