4

I set product's expire time in my custom module. When, product is expire at that time that specific product should be remove from quote. I used event layout_load_before for that.

Because, this code should be working from any page in whole site. If, any other event should i should then, please inform me that also.

Actual Result :

Now, point is that if I remove specific product from quote. Then, product will be remove but minicart is not update.

Expected Result :

When product remove from cart/quote then minicart also refresh and product should be remove from quote/cart.

How to do that ? Please help me.

My observer code :

$items = $this->_checkoutSession->getQuote()->getAllItems();
$createdAt = $this->_stdTimezone->date()->format('Y-m-d H:i:s');
foreach ($items as $key => $item) {
    if (strtotime($customModel->getExpireTime()) <= strtotime($createdAt)) {
        $this->_cart->removeItem($item->getId());
    }
}
$this->_cart->save();
5
  • What you're getting in $item->getId() ? Commented Nov 20, 2018 at 7:47
  • if you're getting the value then please try below code Commented Nov 20, 2018 at 7:49
  • $quoteItem = $quoteModel->load($quoteId); $quoteItem->delete(); Commented Nov 20, 2018 at 7:49
  • 2
    I think you need to update section data as well. Commented Nov 20, 2018 at 8:02
  • Yes. Exactly @RamkishanSuthar . Actually, I know about I need to update section data. But, I don't know about how to update using observer. Commented Nov 20, 2018 at 8:34

2 Answers 2

3

You need to inject \Magento\Quote\Model\Quote\Item class in your construct as like below way :

/**
 * @var \Magento\Quote\Model\Quote\Item
 */
protected $_itemModel;
/**
 * Add constructor.
 * @param \Magento\Quote\Model\Quote\Item $itemModel
 */
public function __construct(
    .........
    \Magento\Quote\Model\Quote\Item $itemModel
    .........
) {
    .........
    $this->_itemModel = $itemModel;
    .........
}

Add this below code in your function

$items = $this->_checkoutSession->getQuote()->getAllItems();
$createdAt = $this->_stdTimezone->date()->format('Y-m-d H:i:s');
foreach ($items as $key => $item) {
    if (strtotime($customModel->getExpireTime()) <= strtotime($createdAt)) {
        $this->_itemModel->load($item->getItemId())->delete();
    }
}
$this->_cart->save();

Remove generation/generated folder and clean cache.

4
  • Thanks @Sukumar. It's working. Can you please tell me that layout_load_before is feasible event or not as per my requirement ? I checked many events but, this only single event is working. Commented Nov 20, 2018 at 8:51
  • If you want to check this on every page as minicart is loading on every page then its a correct event. Or you can also try this event controller_action_predispatch. Commented Nov 20, 2018 at 8:58
  • Okay :) Accepted your answer. Commented Nov 20, 2018 at 8:59
  • @SukumarGorai, how to check if item has specific sku, and delete only that from cart Commented May 20, 2019 at 8:05
3

RamKrishna, is talk about the right point, you have to update the customer Private content after removing an item from the quote.

See the article for how Magento use update private data

As the private content is updated when request type is POST`,So it means using layout_load_before on get Request you could changes Cart details OR user private data.

So, in this case, you have to perform a post request, which will remove the item from cart and have to implement sections.xml for update cart detail.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="{FrontendName}/{contollerName}/{POSTACtionName}">
        <section name="cart"/>
    </action>
</config>
1
  • Correct @Amit Bera this is the right way to achieve this. Happy coding :) Commented Nov 20, 2018 at 17:16

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.