4

Is there any REST API to clear cart all product

One API is there to delete cart product using product ID http://example.com/V1/carts/mine/items/:itemId

But my quote/cart i have added many product then I want clear my whole cart product is there any way to do this?

Thanks

Jayesh Chopda
  • 41
  • 1
  • 5

2 Answers2

0

By Magento default, we don't have API for remove all quote items, so we have to extend to create an custom API with required access privilege and get the quote repository by request cartId and loop through all of the items and delete it.

$quote = $this->quoteRepository->getActive($cartId);

foreach ($quote->getAllItems() as $item) {

$itemId = $item->getItemId();

$quoteItem = $quote->getItemById($itemId);

if (!$quoteItem) {
throw new NoSuchEntityException(
    __('The %1 Cart doesn\'t contain the %2 item.', $cartId, $itemId)
);

}
try {
    $quote->removeItem($itemId);
    
} catch (\Exception $e) {


 throw new CouldNotSaveException(__("The item couldn't be removed from the quote."));
}
}

$this->quoteRepository->save($quote);
Mohamed Abbas
  • 111
  • 1
  • 4
-1

An easy way of doing this is to delete the entire quote from the database. It will solve your issue.

Delete from quote where customer_id = <customer_id>;