Skip to content

Commit

Permalink
Merge pull request #3 from outeredge/fix-mage-2-4-3
Browse files Browse the repository at this point in the history
Allow Magento to choose source, support stores without MSI
  • Loading branch information
davidwindell authored Dec 20, 2022
2 parents bd5e17e + bc5bde8 commit f5b6a44
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions Controller/Adminhtml/Order/MassOrderShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -16,7 +18,7 @@ class MassOrderShip extends \Magento\Backend\App\Action
protected $orderModel;

/**
* @var ConvertOrder
* @var Order
*/
protected $convertOrder;

Expand All @@ -30,32 +32,43 @@ class MassOrderShip extends \Magento\Backend\App\Action
*/
protected $shipmentNotifier;

/**
* @var \Magento\Framework\DB\TransactionFactory
*/
protected $transactionFactory;

/**
* Preparation constructor.
* @param Context $context
* @param ManagerInterface $messageManager
* @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);

$this->messageManager = $messageManager;
$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');
Expand All @@ -66,7 +79,6 @@ public function execute()

$order = $this->orderModel->create()->load($orderId);
if ($order->canShip()) {

$convertOrder = $this->convertOrder->create();
$shipment = $convertOrder->toShipment($order);

Expand All @@ -86,24 +98,31 @@ public function execute()
$shipment->register();
$shipment->getOrder()->setIsInProcess(true);

try {
// Save created shipment and order
$shipment->getExtensionAttributes()->setSourceCode('default');
$shipment->save();
$shipment->getOrder()->save();
//Add shipment to transaction
$saveTransaction->addObject($shipment);
$saveTransaction->addObject($shipment->getOrder());
$shipmentArray[$order->getIncrementId()] = $shipment;

// Send email
if ($notify) {
} else {
$this->messageManager->addError(__("Cannot ship order".$order->getIncrementId().". It's already created or something went wrong"));
}
}

if (!empty($shipmentArray)) {
try {
// Save created shipment
$saveTransaction->save();

// Send email
if ($notify) {
foreach ($shipmentArray as $shipment) {
$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()));
}

} else {
$this->messageManager->addError(__("Cannot ship order, becuase It's already created or something went wrong"));
$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()));
}
}
}
Expand Down

0 comments on commit f5b6a44

Please sign in to comment.