Magento 2 : specific product remove from quote












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









share|improve this question























  • 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
















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









share|improve this question























  • 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














4












4








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 7:33









Rohan HapaniRohan Hapani

1




1













  • 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



















  • 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

















What you're getting in $item->getId() ?

– Aditya Shah
Nov 20 '18 at 7:47





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





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





$quoteItem = $quoteModel->load($quoteId); $quoteItem->delete();

– Aditya Shah
Nov 20 '18 at 7:49




2




2





I think you need to update section data as well.

– Ramkishan Suthar
Nov 20 '18 at 8:02







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





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










2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer


























  • 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



















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>





share|improve this answer


























  • Correct @Amit Bera this is the right way to achieve this. Happy coding :)

    – Ramkishan Suthar
    Nov 20 '18 at 17:16











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer


























  • 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
















2














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.






share|improve this answer


























  • 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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 9:00

























answered Nov 20 '18 at 7:52









Sukumar GoraiSukumar Gorai

6,5403528




6,5403528













  • 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



















  • 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

















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





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





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





Okay :) Accepted your answer.

– Rohan Hapani
Nov 20 '18 at 8:59













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>





share|improve this answer


























  • Correct @Amit Bera this is the right way to achieve this. Happy coding :)

    – Ramkishan Suthar
    Nov 20 '18 at 17:16
















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>





share|improve this answer


























  • Correct @Amit Bera this is the right way to achieve this. Happy coding :)

    – Ramkishan Suthar
    Nov 20 '18 at 17:16














2












2








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>





share|improve this answer















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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 19:56

























answered Nov 20 '18 at 8:13









Amit BeraAmit Bera

58.1k1474173




58.1k1474173













  • Correct @Amit Bera this is the right way to achieve this. Happy coding :)

    – Ramkishan Suthar
    Nov 20 '18 at 17:16



















  • Correct @Amit Bera this is the right way to achieve this. Happy coding :)

    – Ramkishan Suthar
    Nov 20 '18 at 17:16

















Correct @Amit Bera this is the right way to achieve this. Happy coding :)

– Ramkishan Suthar
Nov 20 '18 at 17:16





Correct @Amit Bera this is the right way to achieve this. Happy coding :)

– Ramkishan Suthar
Nov 20 '18 at 17:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?