2 added 195 characters in body
source | link

I had similar function like you required but it's auto adding product to quote.

Instead of quote load, I add a observer that is watching the quote save.

Here is some code:

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_save_before">
        <observer name="vendor_module_quote_save_before" instance="Vendor\Module\Observer\BeforeQuoteSave" />
    </event>
</config>

In the observer loops through all items to find you target item:

            foreach($quote->getAllItems() as $_item){
                if$stockItem = $this->stockItem->load($_item->getSku>getProductId(),'product_id');
 == $sku              if(!$stockItem->getIsInStock() || !$_item->getProduct()->isInStock()){
                    $quote->deleteItem($_item);
                }
            }
            $quote->setTotalsCollectedFlag(false)->collectTotals();

It should work, remember not to save the quote in before_quote_save, it will cause an infinity loop.

Just update the code, it should target your question now.

I had similar function like you required but it's auto adding product to quote.

Instead of quote load, I add a observer that is watching the quote save.

Here is some code:

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_save_before">
        <observer name="vendor_module_quote_save_before" instance="Vendor\Module\Observer\BeforeQuoteSave" />
    </event>
</config>

In the observer loops through all items to find you target item:

            foreach($quote->getAllItems() as $_item){
                if($_item->getSku() == $sku){
                    $quote->deleteItem($_item);
                }
            }
            $quote->setTotalsCollectedFlag(false)->collectTotals();

It should work, remember not to save the quote in before_quote_save, it will cause an infinity loop.

I had similar function like you required but it's auto adding product to quote.

Instead of quote load, I add a observer that is watching the quote save.

Here is some code:

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_save_before">
        <observer name="vendor_module_quote_save_before" instance="Vendor\Module\Observer\BeforeQuoteSave" />
    </event>
</config>

In the observer loops through all items to find you target item:

            foreach($quote->getAllItems() as $_item){
                $stockItem = $this->stockItem->load($_item->getProductId(),'product_id');
                if(!$stockItem->getIsInStock() || !$_item->getProduct()->isInStock()){
                    $quote->deleteItem($_item);
                }
            }
            $quote->setTotalsCollectedFlag(false)->collectTotals();

It should work, remember not to save the quote in before_quote_save, it will cause an infinity loop.

Just update the code, it should target your question now.

1
source | link

I had similar function like you required but it's auto adding product to quote.

Instead of quote load, I add a observer that is watching the quote save.

Here is some code:

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_save_before">
        <observer name="vendor_module_quote_save_before" instance="Vendor\Module\Observer\BeforeQuoteSave" />
    </event>
</config>

In the observer loops through all items to find you target item:

            foreach($quote->getAllItems() as $_item){
                if($_item->getSku() == $sku){
                    $quote->deleteItem($_item);
                }
            }
            $quote->setTotalsCollectedFlag(false)->collectTotals();

It should work, remember not to save the quote in before_quote_save, it will cause an infinity loop.