0

I am using API to build app but I need clear all items in cart but couldn't able find api for deleting cart..

Kindly let me know the API to delete the quote/all items in cart

3 Answers 3

1

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);

Individual items removal we can remove/delete the cart(quote) items by deleteById of item id,

/V1/carts/:cartId/items/:itemId

For guest cart items removable as ,

/V1/guest-carts/:cartId/items/:itemId

For customers' session cart items removable as,

/V1/carts/mine/items/:itemId

Please note, Replace your :cartId with cart/quote id and :itemId with quote item id

0

You can only delete the cart items one by one using the request DELETE {{magento_api_url}}/rest/default/V1/carts/<cartId>/items/<itemId>

0

I would create a new API clone of the default cart item remove v1/carts/cartId/Items/itemid and remove all the cart product using cartId. Please refer below code, that will help you to remove all the item from the cart.

  public function deleteQuoteItems(){
        //$checkoutSession = $this->getCheckoutSession();// Instead of checkout session you need to load quote factory using cart id in api.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkoutSession =$objectManager->create('Magento\Quote\Model\Quote')->loadByIdWithoutStor‌​e($cartId);

        $allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session
        foreach ($allItems as $item) {
            $itemId = $item->getItemId();//item id of particular item
            $quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id
            $quoteItem->delete();//deletes the item
        }
    }
    
     
    public function getItemModel(){
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();//instance of object manager
        $itemModel = $objectManager->create('Magento\Quote\Model\Quote\Item');//Quote item model to load quote item
        return $itemModel;
    }

Hope this will help you.

5
  • There is a cart truncate function in \Magento\Checkout\Controller\Cart\UpdatePost::_emptyShoppingCart which is: $this->cart->truncate()->save(); So I think we don't need to rewrite the logic
    – Jimmy
    Mar 16, 2021 at 2:44
  • Ya, you can use it but I think this is for the web, not for API. Still, you can check and make sure. :)
    – Nits
    Mar 16, 2021 at 4:25
  • I'm sorry but I'm not sure if I understand what you mean by not for API. The API you wrote already in the answer, I just mean that you replace the function's body by the truncate cart method
    – Jimmy
    Mar 16, 2021 at 4:38
  • @Jimmy yes you haven't got my point. In simple words, you can't use that controller file code for truncate cart from API. Because that is used for website only.
    – Nits
    Mar 16, 2021 at 4:48
  • Could you give more information? I don't use controller at all, the function is cart->truncate() where cart can be get from session - just like in your code.
    – Jimmy
    Mar 16, 2021 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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