diff --git a/Observer/AutoShipment.php b/Observer/AutoShipment.php index 56c9eba..747e48f 100644 --- a/Observer/AutoShipment.php +++ b/Observer/AutoShipment.php @@ -10,6 +10,8 @@ use DHLParcel\Shipping\Model\Service\Printing as PrintingService; use DHLParcel\Shipping\Model\Service\Shipment as ShipmentService; use Magento\Framework\Event\ManagerInterface as EventManager; +use Magento\Sales\Api\OrderRepositoryInterface; +use Magento\Sales\Model\Order; class AutoShipment implements \Magento\Framework\Event\ObserverInterface { @@ -21,9 +23,11 @@ class AutoShipment implements \Magento\Framework\Event\ObserverInterface protected $presetService; protected $eventManager; protected $productMetadata; + protected $orderRepository; public function __construct( \Magento\Framework\App\ProductMetadataInterface $productMetadata, + OrderRepositoryInterface $orderRepository, EventManager $eventManager, Data $helper, OrderService $orderService, @@ -33,6 +37,7 @@ public function __construct( PresetService $presetService ) { $this->productMetadata = $productMetadata; + $this->orderRepository = $orderRepository; $this->helper = $helper; $this->orderService = $orderService; $this->shipmentService = $shipmentService; @@ -50,13 +55,25 @@ public function execute(\Magento\Framework\Event\Observer $observer) return; } - $order = $observer->getEvent()->getOrder(); + $observerOrder = $observer->getEvent()->getOrder(); + if (!$observerOrder || !$observerOrder->getId()) { + return; + } - if ($order->getStatus() !== $this->helper->getConfigData('usability/automation/on_order_status')) { + if ($observerOrder->getStatus() !== $this->helper->getConfigData('usability/automation/on_order_status')) { + return; + } + if (!$observerOrder->canShip() || $observerOrder->hasShipments() || $observerOrder->getShipmentsCollection()->count() > 0) { return; } - if (!$order->canShip() || $order->hasShipments() || !empty($order->getShipmentsCollection()->getData())) { + /** + * Force new order from database, looks like object is not always reliable + * + * @var $order Order + */ + $order = $this->orderRepository->get($observer->getEvent()->getOrder()->getId()); + if (!$order->canShip() || $order->hasShipments() || $order->getShipmentsCollection()->count() > 0) { return; } diff --git a/composer.json b/composer.json index c3d3936..a0108a5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "dhlparcel/magento2-plugin", "description": "DHL Parcel plugin for Magento 2", "type": "magento2-module", - "version": "1.0.25", + "version": "1.0.26", "license": [ "OSL-3.0" ], diff --git a/etc/module.xml b/etc/module.xml index 14c2cbb..8340a5d 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -18,7 +18,7 @@ - +