How to create custom command to delete order in magento2 – In Magento 2 you can create your own custom command for any action. As magento doesn’t allow to delete order from admin dashboard then in this case there comes a need for how to delete orders. So here using this post I am going to explain how to create your own custom command for deleting magento order.
For this you just need to follow some easy steps-
- Create a Command class in the path<module_dir>/Console/Command (As recommended by magento), like here I am using the path app/code/Webkul/CustomCommand/Console/Command
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120namespace Webkul\CustomCommand\Console\Command;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputArgument;use Symfony\Component\Console\Input\InputOption;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;use Magento\Sales\Model\Order;use Magento\Framework\Registry;/*** Class OrderDeleteCommand*/class OrderDeleteCommand extends Command{/*** Order argument*/const ORDER_ARGUMENT = 'order_id';/*** Allow all*/const ALLOW_ALL = 'allow-all';/*** All order id*/const ALL_ORDER = 'All';/*** Order** @var Order*/private $order;/*** @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory*/protected $orderCollectionFactory;/*** @var \Magento\Framework\Registry*/protected $registry;/*** @param ModuleListInterface $moduleList*/public function __construct(Order $order,\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,Registry $registry){$this->order = $order;$this->registry = $registry;$this->orderCollectionFactory = $orderCollectionFactory;parent::__construct();}/*** {@inheritdoc}*/protected function configure(){$this->setName('webkul:orderDelete')->setDescription('Order Delete command')->setDefinition([new InputArgument(self::ORDER_ARGUMENT,InputArgument::OPTIONAL,'OrderId'),new InputOption(self::ALLOW_ALL,'-a',InputOption::VALUE_NONE,'Allow all OrderId'),]);parent::configure();}/*** {@inheritdoc}*/protected function execute(InputInterface $input, OutputInterface $output){$orderId = $input->getArgument(self::ORDER_ARGUMENT);$allowAll = $input->getOption(self::ALLOW_ALL);if (is_null($orderId)) {if ($allowAll) {$this->registry->register('isSecureArea','true'); //create secure area to delete orders$orderAllColl = $this->orderCollectionFactory->create(); //Get Order Collectionforeach ($orderAllColl as $orderCollData) {$orderCollData->delete(); //Delete Order}$this->registry->unregister('isSecureArea'); //unset secure area$output->writeln('<info>All orders are deleted.</info>'); //Displaying output on terminal} else {throw new \InvalidArgumentException('Argument ' . self::ORDER_ARGUMENT . ' is missing.');}}if($orderId){$orderColl = $this->order->load($orderId);if(empty($orderColl) || !$orderColl->getId()){$output->writeln('<info>Order with id ' . $orderId . ' does not exist.</info>');}else{$this->registry->register('isSecureArea','true'); //create secure area to delete orders$orderColl->delete(); //Delete Order$this->registry->unregister('isSecureArea'); //unset secure area$output->writeln('<info>Order with id ' . $orderId . ' is deleted.</info>');}}}} - Declaration of Command class in
Magento\Framework\Console\CommandList
using dependency injection in file path “app/code/Webkul/CustomCommand/di.xml”
1234567<type name="Magento\Framework\Console\CommandList"><arguments><argument name="commands" xsi:type="array"><item name="order_delete_command" xsi:type="object">Webkul\CustomCommand\Console\Command\OrderDeleteCommand</item></argument></arguments></type> - Now clear the cache and compile the module.
- Now run the following command to check the working process of this commands-
php bin/magento –list
now you will see the custom created command as shown in the given screenshot
Now run command – php bin/magento webkul:orderDelete 1 to delete order with order id 1 through command line.For deleting all orders just run the command – php bin/magento webkul:orderDelete -a
So in this way you can create your own custom commands for any action.
For complete module please find at https://github.com/webkul/magento2-custom-command .
Be the first to comment.