I am having a module which gives the ability to cart with checkbox to select which items you want to proceed to checkout.
Although I have an issue with Observer event and always in order I get all items of the cart with totals of the selected items for checkout. For some reason inside order the items which are not selected in cart still remaining in final order without being calculated in totals
Thsi is events.xml (app/code/mymodule/etc/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_model_service_quote_submit_before">
<observer name="remove_unselected_order_items"
instance="MymoduleSplitCartObserverMagentoRemoveUnselectedOrderItems" />
</event>
</config>
I am using sales_model_service_quote_submit_before and when I debug I see that it is triggered:
[2025-04-16T18:27:55.495465+00:00] main.DEBUG: Observer sales_model_service_quote_submit_before triggered. [] []
[2025-04-16T18:27:55.495883+00:00] main.DEBUG: Checking item ID 55099, SKU: chester mcn [] []
[2025-04-16T18:27:55.495960+00:00] main.DEBUG: Checking item ID 55100, SKU: 83-B70-FL [] []
[2025-04-16T18:27:55.496046+00:00] main.DEBUG: Removing item ID 55100, SKU: 83-B70-FL [] []
[2025-04-16T18:27:55.506558+00:00] main.DEBUG: Quote totals collected and saved after filtering. [] []
And this is my RemoveUnselectedOrderItems.php (with debug code)
<?php
namespace MymoduleSplitCartObserverMagento;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoQuoteModelQuoteRepository;
use PsrLogLoggerInterface;
class RemoveUnselectedOrderItems implements ObserverInterface
{
protected $request;
protected $quoteRepository;
protected $logger;
public function __construct(
RequestInterface $request,
QuoteRepository $quoteRepository,
LoggerInterface $logger
) {
$this->request = $request;
$this->quoteRepository = $quoteRepository;
$this->logger = $logger;
}
public function execute(Observer $observer)
{
$this->logger->debug('Observer sales_model_service_quote_submit_before triggered.');
/** @var MagentoQuoteModelQuote $quote */
$quote = $observer->getEvent()->getQuote();
if (!$quote) {
$this->logger->debug('Quote is null.');
return;
}
foreach ($quote->getAllVisibleItems() as $item) {
$itemId = $item->getId();
$sku = $item->getSku();
$this->logger->debug("Checking item ID {$itemId}, SKU: {$sku}");
if (!$item->getAvailableToCheckout()) {
$this->logger->debug("Removing item ID {$itemId}, SKU: {$sku}");
$quote->removeItem($itemId);
}
}
$quote->collectTotals();
$this->quoteRepository->save($quote);
$this->logger->debug('Quote totals collected and saved after filtering.');
}
}
But I can’t make it work. As I said before inside order the items which are not selected in cart still remaining in final order without being calculated in totals
Any help please?