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.
Best Answer
For Guest:
POST: Create a Guest Cart
http://test.com/rest/V1/guest-carts/
POST: Add multiple products to the cart
http://test.com/rest/V1/guest-carts/94b93061280ddc2e050e196203ba598b/
In Postman, Body=>Raw=>json add bellow code
{ "products": [
{ "quote_id": "94b93061280ddc2e050e196203ba598b","pid": "2354", "qty":1 },
{ "quote_id": "94b93061280ddc2e050e196203ba598b","pid": "2353", "qty":1 },
{ "quote_id": "94b93061280ddc2e050e196203ba598b","pid": "2352", "qty":1 }
]
}
GET: Cart items
http://test.com/rest/V1/guest-multicart/
For Registered Customer:
POST: Customer login
http://test.com/rest/V1/integration/customer/token?username=nagendra.k@test.com&password=rest@123
POST: Add multiple products to the cart
http://test.com/rest/V1/carts/mine
In Postman, Body=>Raw=>json add bellow code
{ "products": [
{ "quote_id": "6","pid": "2354", "qty":1 },
{ "quote_id": "6","pid": "2353", "qty":1 },
{ "quote_id": "6","pid": "2352", "qty":1 }
]
}
GET: Cart items
http://test.com/rest/V1/cart/mine-multicart
Sky\Mobileshop\etc\webapi.xml
Sky\Mobileshop\etc\di.xml
Sky\Mobileshop\Api\GuestmulticartInterface.php
Sky\Mobileshop\Model\Guestmulticart.php
Sky\Mobileshop\Api\MinemulticartInterface.php
Sky\Mobileshop\Model\Minemulticart.php