3

I want to delete the order item from observer I created like this:

namespace Namespace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterOrder implements ObserverInterface
{

    protected $_objectManager;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //not sure if it's correct
        $order = $observer->getEvent()->getOrder();  
        foreach($order->getAllItems() as $item){ 
          if(condition){
             //delete order item, how?
          }
        }
    }
}

events.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_onepage_controller_success_action">
       <observer name="module_controller_success_action" instance="Namespace\Module\Observer\AfterOrder"  />
   </event>
</config>
8
  • Please confirm whether u would like to delete order item/s after order has been success? Commented Jul 6, 2017 at 7:19
  • @NAGARAJUKASA i want to delete the order item after the order has been saved in one page checkout
    – simple guy
    Commented Jul 6, 2017 at 7:20
  • Please confirm whether your observer has been calling or not? Commented Jul 6, 2017 at 7:23
  • @NAGARAJUKASA yes my observer has been running
    – simple guy
    Commented Jul 6, 2017 at 7:32
  • Please try the below code which i have posted it's working for me Commented Jul 6, 2017 at 8:35

1 Answer 1

2

Please try the below code and keep in the observer

<?php
       namespace Namespace\Module\Observer;
       use Magento\Framework\Event\ObserverInterface;
       use Magento\Framework\App\RequestInterface;
       use Magento\Sales\Api\OrderRepositoryInterface;
       use Magento\Customer\Model\Session;
class AfterOrder implements ObserverInterface
{ 

    protected $orderRepository;
    protected $datetime;
    protected $_session;

    public function __construct(  
    OrderRepositoryInterface $OrderRepositoryInterface,
    \Magento\Framework\Stdlib\DateTime\DateTime $datetime,
    \Magento\Customer\Model\Session $session
    ) {
        $this->orderRepository = $OrderRepositoryInterface;
        $this->datetime = $datetime;
        $this->_session = $session;

    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {

           //Custom log:
           $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/TradeorderSuccess.log');
           $logger = new \Zend\Log\Logger();
           $logger->addWriter($writer);            

           $order_ids = $observer->getEvent()->getOrderIds()[0];
           $order = $this->orderRepository->get($order_ids);           
           $order_id = $order->getIncrementId();               

          foreach ($order->getAllVisibleItems() as $item)
          {
                //Here keep ur condition.
                $logger->info("xxxxxxxxxxxxxxxxxx===>".$item->getName());
                $item->isDeleted(true);
                $logger->info("deleted");
          } 

          $order->save();
          $logger->info("saved");
    }


}
3
  • not working at all
    – simple guy
    Commented Jul 7, 2017 at 1:51
  • After change the code do the di compile command Commented Jul 7, 2017 at 2:43
  • let me know what exact error or problem u r facing so that i can guide u accordingly Commented Jul 7, 2017 at 4:12

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.