From a40744227656bca96978b83cab382021f8d71385 Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Wed, 14 Dec 2022 13:18:43 +0000 Subject: [PATCH 1/2] Fix to work with mage 2.4 --- Controller/Adminhtml/Order/MassOrderShip.php | 57 +++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/Controller/Adminhtml/Order/MassOrderShip.php b/Controller/Adminhtml/Order/MassOrderShip.php index 751812a..45ed4ea 100644 --- a/Controller/Adminhtml/Order/MassOrderShip.php +++ b/Controller/Adminhtml/Order/MassOrderShip.php @@ -5,8 +5,10 @@ use Magento\Backend\App\Action\Context; use Magento\Sales\Model\OrderFactory as ModelOrder; use Magento\Sales\Model\Convert\OrderFactory as ConvertOrder; +use Magento\Sales\Model\Convert\Order; use Magento\Framework\Message\ManagerInterface; use Magento\Shipping\Model\ShipmentNotifier; +use Magento\Framework\DB\TransactionFactory; class MassOrderShip extends \Magento\Backend\App\Action { @@ -16,7 +18,7 @@ class MassOrderShip extends \Magento\Backend\App\Action protected $orderModel; /** - * @var ConvertOrder + * @var Order */ protected $convertOrder; @@ -30,6 +32,11 @@ class MassOrderShip extends \Magento\Backend\App\Action */ protected $shipmentNotifier; + /** + * @var \Magento\Framework\DB\TransactionFactory + */ + protected $transactionFactory; + /** * Preparation constructor. * @param Context $context @@ -37,13 +44,15 @@ class MassOrderShip extends \Magento\Backend\App\Action * @param ModelOrder $orderModel * @param ConvertOrder $convertOrder * @param ShipmentNotifier $shipmentNotifier + * @param TransactionFactory $transactionFactory */ public function __construct( Context $context, ManagerInterface $messageManager, ModelOrder $orderModel, ConvertOrder $convertOrder, - ShipmentNotifier $shipmentNotifier + ShipmentNotifier $shipmentNotifier, + TransactionFactory $transactionFactory ) { parent::__construct($context); @@ -51,11 +60,15 @@ public function __construct( $this->orderModel = $orderModel; $this->convertOrder = $convertOrder; $this->shipmentNotifier = $shipmentNotifier; + $this->transactionFactory = $transactionFactory; } public function execute() { + /* @var $saveTransaction \Magento\Framework\DB\Transaction */ + $saveTransaction = $this->transactionFactory->create(); $resultRedirect = $this->resultRedirectFactory->create(); + $shipmentArray = []; $ordersId = $this->getRequest()->getParam('selected'); $notify = $this->getRequest()->getParam('notify'); @@ -66,7 +79,6 @@ public function execute() $order = $this->orderModel->create()->load($orderId); if ($order->canShip()) { - $convertOrder = $this->convertOrder->create(); $shipment = $convertOrder->toShipment($order); @@ -86,25 +98,32 @@ public function execute() $shipment->register(); $shipment->getOrder()->setIsInProcess(true); - try { - // Save created shipment and order - $shipment->getExtensionAttributes()->setSourceCode('default'); - $shipment->save(); - $shipment->getOrder()->save(); - - // Send email - if ($notify) { - $this->shipmentNotifier->notify($shipment); - $shipment->save(); - } - $this->messageManager->addSuccess(__("Shipment Succesfully Generated for order: #".$order->getIncrementId())); - } catch (\Exception $e) { - $this->messageManager->addError(__('Cannot ship order'. $e->getMessage())); - } + //Add shipment to transaction + $saveTransaction->addObject($shipment); + $saveTransaction->addObject($shipment->getOrder()); + $shipmentArray[$order->getIncrementId()] = $shipment; } else { - $this->messageManager->addError(__("Cannot ship order, becuase It's already created or something went wrong")); + $this->messageManager->addError(__("Cannot ship order".$order->getIncrementId().". It's already created or something went wrong")); + } + } + + try { + // Save created shipment + $saveTransaction->save(); + + // Send email + if ($notify) { + foreach ($shipmentArray as $shipment) { + $this->shipmentNotifier->notify($shipment); + } + } + if (!empty($shipmentArray)) { + $ordersIncrementIds = implode(",", array_keys($shipmentArray)); + $this->messageManager->addSuccess(__("Shipment Succesfully Generated for orders: ".$ordersIncrementIds)); } + } catch (\Exception $e) { + $this->messageManager->addError(__('Cannot ship order'. $e->getMessage())); } } return $resultRedirect->setPath('sales/order/index', [], ['error' => true]); From bc5bde8cb1797847100fa76f76c4ae66e2ee0ac1 Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Wed, 14 Dec 2022 13:24:35 +0000 Subject: [PATCH 2/2] Fix to work with mage 2.4 --- Controller/Adminhtml/Order/MassOrderShip.php | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Controller/Adminhtml/Order/MassOrderShip.php b/Controller/Adminhtml/Order/MassOrderShip.php index 45ed4ea..3626a1e 100644 --- a/Controller/Adminhtml/Order/MassOrderShip.php +++ b/Controller/Adminhtml/Order/MassOrderShip.php @@ -108,22 +108,22 @@ public function execute() } } - try { - // Save created shipment - $saveTransaction->save(); - - // Send email - if ($notify) { - foreach ($shipmentArray as $shipment) { - $this->shipmentNotifier->notify($shipment); + if (!empty($shipmentArray)) { + try { + // Save created shipment + $saveTransaction->save(); + + // Send email + if ($notify) { + foreach ($shipmentArray as $shipment) { + $this->shipmentNotifier->notify($shipment); + } } - } - if (!empty($shipmentArray)) { $ordersIncrementIds = implode(",", array_keys($shipmentArray)); $this->messageManager->addSuccess(__("Shipment Succesfully Generated for orders: ".$ordersIncrementIds)); + } catch (\Exception $e) { + $this->messageManager->addError(__('Cannot ship order'. $e->getMessage())); } - } catch (\Exception $e) { - $this->messageManager->addError(__('Cannot ship order'. $e->getMessage())); } } return $resultRedirect->setPath('sales/order/index', [], ['error' => true]);