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.

Your Answer

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

Browse other questions tagged or ask your own question.