Magento 2 : specific product remove from quote
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
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();
magento2 cart quote
add a comment |
up vote
4
down vote
favorite
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();
magento2 cart quote
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
2
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 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 at 8:34
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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();
magento2 cart quote
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();
magento2 cart quote
magento2 cart quote
asked Nov 20 at 7:33
Rohan Hapani
4,96521559
4,96521559
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
2
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 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 at 8:34
add a comment |
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
2
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 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 at 8:34
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
2
2
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 at 8:02
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 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 at 8:34
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 at 8:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You need to inject MagentoQuoteModelQuoteItem
class in your construct as like below way :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
/**
* Add constructor.
* @param MagentoQuoteModelQuoteItem $itemModel
*/
public function __construct(
.........
MagentoQuoteModelQuoteItem $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. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
add a comment |
up vote
2
down vote
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>
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to inject MagentoQuoteModelQuoteItem
class in your construct as like below way :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
/**
* Add constructor.
* @param MagentoQuoteModelQuoteItem $itemModel
*/
public function __construct(
.........
MagentoQuoteModelQuoteItem $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. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
add a comment |
up vote
1
down vote
accepted
You need to inject MagentoQuoteModelQuoteItem
class in your construct as like below way :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
/**
* Add constructor.
* @param MagentoQuoteModelQuoteItem $itemModel
*/
public function __construct(
.........
MagentoQuoteModelQuoteItem $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. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to inject MagentoQuoteModelQuoteItem
class in your construct as like below way :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
/**
* Add constructor.
* @param MagentoQuoteModelQuoteItem $itemModel
*/
public function __construct(
.........
MagentoQuoteModelQuoteItem $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.
You need to inject MagentoQuoteModelQuoteItem
class in your construct as like below way :
/**
* @var MagentoQuoteModelQuoteItem
*/
protected $_itemModel;
/**
* Add constructor.
* @param MagentoQuoteModelQuoteItem $itemModel
*/
public function __construct(
.........
MagentoQuoteModelQuoteItem $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.
edited Nov 20 at 9:00
answered Nov 20 at 7:52
Sukumar Gorai
5,5873527
5,5873527
Thanks @Sukumar. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
add a comment |
Thanks @Sukumar. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
Thanks @Sukumar. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Thanks @Sukumar. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
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 at 8:51
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 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 at 8:58
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 at 8:58
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
Okay :) Accepted your answer.
– Rohan Hapani
Nov 20 at 8:59
1
1
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
Thanks. Happy Coding.
– Sukumar Gorai
Nov 20 at 9:00
add a comment |
up vote
2
down vote
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>
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
add a comment |
up vote
2
down vote
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>
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
add a comment |
up vote
2
down vote
up vote
2
down vote
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>
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>
edited Nov 20 at 19:56
answered Nov 20 at 8:13
Amit Bera♦
56.1k1369167
56.1k1369167
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
add a comment |
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Thanks @AmitBera. Let me try this.
– Rohan Hapani
Nov 20 at 8:35
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
Correct @Amit Bera this is the right way to achieve this. Happy coding :)
– Ramkishan Suthar
Nov 20 at 17:16
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f250600%2fmagento-2-specific-product-remove-from-quote%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you're getting in $item->getId() ?
– Aditya Shah
Nov 20 at 7:47
if you're getting the value then please try below code
– Aditya Shah
Nov 20 at 7:49
$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();
– Aditya Shah
Nov 20 at 7:49
2
I think you need to update section data as well.
– Ramkishan Suthar
Nov 20 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 at 8:34