In this article, we will learn how to create shipments using custom console command in magento 2.
To create shipments using CLI please follow below steps:
Step 01: Create basic module
Create registration.php file under app/code/[vendor_name]/[module_name]
(In my case, vendor_name is ‘BluethinkInc’ and module_name is ‘ShipmentCli’)
1 2 3 4 5 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'BluethinkInc_ShipmentCli', __DIR__); |
Now, create module.xml file under app/code/BluethinkInc/ShipmentCli/etc
1 2 3 4 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="BluethinkInc_ShipmentCli"/> </config> |
Step 02: Add dependency in di.xml
Create di.xml file under app/code/BluethinkInc/ShipmentCli/etc
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="Shipment" xsi:type="object">BluethinkInc\InvoiceCli\Console\Command\Shipment</item> </argument> </arguments> </type> </config> |
Step 03: Create Shipment.php file under
app/code/BluethinkInc/ShipmentCli/Console/Command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php declare(strict_types=1); namespace BluethinkInc\ShipmentCli\Console\Command; use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\App\ObjectManager; use Magento\Framework\DB\TransactionFactory; use Magento\Sales\Model\Convert\OrderFactory; use Symfony\Component\Console\Command\Command; use Magento\Sales\Api\OrderRepositoryInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; class Shipment extends Command { private const ORDERID = 'orderId'; public function __construct( State $state, ShipmentSender $shipmentSender, OrderFactory $convertOrderFactory, TransactionFactory $transactionFactory, OrderRepositoryInterface $orderRepository ) { parent::__construct(); $this->state = $state; $this->shipmentSender = $shipmentSender; $this->orderRepository = $orderRepository; $this->transactionFactory = $transactionFactory; $this->orderConverter = $convertOrderFactory->create(); } protected function execute( InputInterface $input, OutputInterface $output ) { $this->state->setAreaCode(Area::AREA_ADMINHTML); $orderId = $input->getOption(self::ORDERID); $objectManager = ObjectManager::getInstance(); $isOrderExists = $objectManager->create('\Magento\Sales\Model\Order')->loadByIncrementId($orderId); if ($orderId) { if ($isOrderExists->getId()) { $order = $this->orderRepository->get($orderId); // check shipment exist for order or not if ($order->canShip()) { // Initialize the order shipment object $shipment = $this->orderConverter->toShipment($order); foreach ($order->getAllItems() as $orderItem) { // Check if order item has qty to ship or is order is virtual if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) { continue; } $qtyShipped = $orderItem->getQtyToShip(); // Create shipment item with qty $shipmentItem = $this->orderConverter->itemToShipmentItem($orderItem)->setQty($qtyShipped); // Add shipment item to shipment $shipment->addItem($shipmentItem); } // Register shipment $shipment->register(); $shipment->getOrder()->setIsInProcess(true); try { $transaction = $this->transactionFactory->create()->addObject($shipment) ->addObject($shipment->getOrder()) ->save(); if ($shipment) { $this->shipmentSender->send($shipment); } $output->writeln("<info>Shipment is generated for the order id : $orderId</info>"); } catch (\Exception $e) { $output->writeln("<error>We can\'t generate shipment.</error>"); } } else { $output->writeln("<error>Shipment for this order is already generated.</error>"); } } else { $output->writeln("<error>Order-Id doesn't exist.</error>"); } /** * @inheritdoc */ protected function configure() { $this->setName("create:shipment"); $this->setDescription("It will genereate shipment for given order id"); $this->addOption( self::ORDERID, null, InputOption::VALUE_REQUIRED, 'orderId' ); parent::configure(); } } |
That’s it!!!! Now you are able to create shipments from your console command.
Thanks for reading my blog. I hope you would learn a lot of things from this blog. Keep liking and sharing!!
bluethinkinc_blog
2023-03-22