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();
  • What you're getting in $item->getId() ? – Aditya Shah Nov 20 '18 at 7:47
  • if you're getting the value then please try below code – Aditya Shah Nov 20 '18 at 7:49
  • $quoteItem = $quoteModel->load($quoteId); $quoteItem->delete(); – Aditya Shah Nov 20 '18 at 7:49
  • 2
    I think you need to update section data as well. – Ramkishan Suthar Nov 20 '18 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. – Rohan Hapani Nov 20 '18 at 8:34
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.

  • 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. – Rohan Hapani Nov 20 '18 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. – Sukumar Gorai Nov 20 '18 at 8:58
  • Okay :) Accepted your answer. – Rohan Hapani Nov 20 '18 at 8:59
  • @SukumarGorai, how to check if item has specific sku, and delete only that from cart – jafar pinjar May 20 at 8:05
2

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>
  • Correct @Amit Bera this is the right way to achieve this. Happy coding :) – Ramkishan Suthar Nov 20 '18 at 17:16

Your Answer

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

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