4

I would like to delete order's invoice, credit memo & shipment programmatically.

How to achieve this by passing only order id?

8

There are few steps are required for this.

Load order by Order Factory

Inject the factory class \Magento\Sales\Model\OrderFactory on __construct class.for getting order.

Then order get by order id

 $order = $this->orderFactory->create()->load($orderId);

Get Invoice collection from Order object

Get Invoice collection from Order object using getInvoiceCollection()

As you want run delete operation then you should set $this->registry->register('isSecureArea', true);

Get Shipment collection from Order object

Get Shipment collection from Order object using getShipmentsCollection()

Get credit memo collection from Order object

Get credit memo collection from Order object using getCreditmemosCollection()

code:

<?php
namespace [YournameSapce];
class DeleteorderInvoiceetc
{
  protected $order;
  protected $redirectFactory;
  protected $registry;

  public function __construct(
      ........
      \Magento\Sales\Model\OrderFactory $orderFactory,
      \Magento\Framework\Registry $registry
  ) {
      ......
      $this->orderFactory = $orderFactory;
      $this->registry = $registry;
  }   
  public function Order(){
      $orderId = 'xyz';
      if(!$this->order){  
          $this->order = $this->orderFactory->create()->load($orderId);
      }
       return $this->order;
  }
  public function deleteInvoices(){

      if(!$this->Order()){
          return;
      }

      if($this->registry->registry('isSecureArea')){
          $this->registry->unregister('isSecureArea');
      }
      $this->registry->register('isSecureArea', true);

      $_invoices = $this->Order()->getInvoiceCollection();

      if($_invoices){
          foreach($_invoices as $invoice){
              $invoice->delete();
          }
      }

  }
  /*
  * Shipemrnt delete
  */
  public function deleteShipments(){

      if(!$this->Order()){
          return;
      }

      if($this->registry->registry('isSecureArea')){
          $this->registry->unregister('isSecureArea');
      }
      $this->registry->register('isSecureArea', true);

      $_shipments = $this->Order()->getShipmentsCollection();

      if($_shipments){
          foreach($_shipments as $_shipment){
              $_shipment->delete();
          }
      }

  }
      /*
  * Credit memo delete
  */
  public function deleteCreditmemos(){

      if(!$this->Order()){
          return;
      }

      if($this->registry->registry('isSecureArea')){
          $this->registry->unregister('isSecureArea');
      }
      $this->registry->register('isSecureArea', true);

      $_creditmemos = $this->Order()->getCreditmemosCollection();

      if($_creditmemos){
          foreach($_creditmemos as $_creditmemo){
              $_creditmemo->delete();
          }
      }

  }   
}
  • HI @Amit It's required to do isSecureArea to true? – Ankit Shah Apr 5 '17 at 5:46
  • yes. you want run delete operation magento exclude admin – Amit Bera Apr 5 '17 at 6:03
  • Hi Amit It will delete that thing. But in Order if I had refunded some amount then it's still showing – Ankit Shah Apr 5 '17 at 6:25
  • Thanks @AmitBera you saved the day :) – Shoaib Munir Jun 17 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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