1

I try to delete all items with a custom product type from the quote before the order is placed. So I created an observer for the event sales_order_place_before. In this observer I am collection all items with my custom product type ral_surcharge and try to delete them from the quote. But after the order is placed, the items are still there.

What am I doing wrong?

MODULE/etc/webapi_rest/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_order_place_before">
        <observer instance="Company\RalSurcharge\Observer\Webapi\Sales\OrderPlaceBefore"
                  name="company_ralsurcharge_observer_webapi_sales_orderplacebefore_sales_order_place_before"/>
    </event>
</config>

MODULE/etc/webapi_soap/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_order_place_before">
        <observer instance="Company\RalSurcharge\Observer\Webapi\Sales\OrderPlaceBefore"
                  name="company_ralsurcharge_observer_webapi_sales_orderplacebefore_sales_order_place_before"/>
    </event>
</config>

MODULE/Observer/Webapi/Sales/OrderPlaceBefore.php

<?php

namespace Company\RalSurcharge\Observer\Webapi\Sales;

use \Magento\Quote\Model\QuoteFactory;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use \Company\RalSurcharge\Helper\RalSurcharge as RalHelper;

class OrderPlaceBefore implements ObserverInterface
{
    /**
     * @var \Magento\Quote\Model\QuoteFactory
     */
    private $quoteFactory;

    /**
     * @var \Company\RalSurcharge\Helper\RalSurcharge
     */
    private $ralHelper;

    public function __construct(
        RalHelper $ralHelper,
        QuoteFactory $quoteFactory
    ) {
        $this->ralHelper = $ralHelper;
        $this->quoteFactory = $quoteFactory;
    }

    public function execute(Observer $observer)
    {
        $quoteId = $observer->getOrder()
                            ->getQuoteId();
        $quoteModel = $this->quoteFactory->create();
        $quote = $quoteModel->loadByIdWithoutStore($quoteId);
        $this->ralHelper->main($quote);
    }



}

MODULE/Helper/RalSurcharge.php

<?php

namespace Company\RalSurcharge\Helper;

class RalSurcharge
{
    private $allItems;
    private $quote;

    private $logger;

    /**
     * @param \Magento\Quote\Model\Quote $quote
     */
    public function main($quote)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/zend_debug.log');
        $this->logger = new \Zend\Log\Logger();
        $this->logger->addWriter($writer);

        $this->logger->info("Hello from main");

        $this->quote = $quote;
        $allItems = $this->quote->getAllItems();
        $collection = $this->getItemsWithProductType("ral_surcharge");
        $this->deleteItemsFromQuote($collection)
    }

    private function deleteItemsFromQuote($items)
    {
        if (empty($items)) { return false; }

        foreach($items as $item) {
            $this->logger->info("delete item '". $item->getName() . "' from quote.");
            $this->quote->deleteItem($item);
        }
        $this->quote->save();
    }

    private function getItemsWithProductType($type)
    {
        if (empty($this->allItems)) { return false; }

        $collection = [];
        foreach($this->allItems as $item) {
            if ($item->getProductType() === $type) {
                $collection[] = $item;
            }
        }
        return $collection;
    }

    ...
}

1 Answer 1

2

sales_order_place_before is not the right event to Modify quote items before order placement.

sales_order_place_before event fire after quote converted to order that why it is not the right place.

I think you can use \Magento\Quote\Model\QuoteManagement::submit because of this function hit before quote convert to order.

1
  • Now I understand why it failed, thank you! I replaced the event with checkout_submit_before now it works.
    – Black
    Nov 18, 2020 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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