I've written plugin for Magento\Checkout\Controller\Cart::Delete
Plugin works great and removed my desirable product from cart.
As much as I know Minicart is render with local storage. So that we have to update it with sections.xml
.
But I looked into it and found that sections.xml
only contains actions.
So How can I reload minicart whenever I use plugin ?
UPDATE
Thing is like I want to remove one specific product when any changes made to the cart Like, Update qty, Remove existing item from cart etc.
If any of above changes occur than let's say I've product ID-5 that product needs to be remove from automatically.
Example:
I've 4 products in cart including my free product(has price USD 0)
Item Name - Qty
Product-1 - 1
Product-2 - 2
Product-3 - 1
Product-4 - 1
(Free product)
If any changes to qty happens to products expect free product than free product needs to be removed and qty update will be there for that product.
PS: I've disabled qty update for free product.
What I've tried.
public function beforeExecute(
\Magento\Checkout\Controller\Cart\Delete $subject
)
{
$items = $this->checkoutSession->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$product = $this->product->load($item->getProductId());
if($product->getSku() == $this->helperData->getGeneralConfig('free_product')){ // get product sku
$this->cart->removeItem($item->getItemId())->save();
$this->checkoutSession->getQuote()->collectTotals()->save();
}
}
$customerBeforeAuthUrl = $this->url->getUrl('checkout/cart/');
$this->responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
die();
}
Above is working but minicart isn't update. along with side bar total on cart page.
If you have any other solution rather than plugin than add your solution too.
Any help would be appreciated. Thanks in Advance.