From 1e03ddd995e77fa827aced7d9d516d50ede1c29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 9 Sep 2024 11:01:54 +0200 Subject: [PATCH] Take the 'ship' transition on the shipment instead of the order and also set the tracking number on the shipment --- composer.json | 1 + .../OrderPackedWebhookHandler.php | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 1feb90a..f5555ae 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "sylius/payment": "^1.0", "sylius/product": "^1.0", "sylius/resource-bundle": "^1.0", + "sylius/shipping": "^1.0", "sylius/ui-bundle": "^1.0", "symfony/config": "^5.4 || ^6.4", "symfony/console": "^5.4 || ^6.4", diff --git a/src/WebhookHandler/OrderPackedWebhookHandler.php b/src/WebhookHandler/OrderPackedWebhookHandler.php index e526765..95a89e4 100644 --- a/src/WebhookHandler/OrderPackedWebhookHandler.php +++ b/src/WebhookHandler/OrderPackedWebhookHandler.php @@ -13,10 +13,10 @@ use Setono\SyliusPeakPlugin\Model\OrderInterface; use SM\Factory\FactoryInterface; use Sylius\Component\Core\Model\OrderItemInterface; -use Sylius\Component\Core\OrderShippingTransitions; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Order\OrderTransitions; use Sylius\Component\Payment\PaymentTransitions; +use Sylius\Component\Shipping\ShipmentTransitions; use Webmozart\Assert\Assert; final class OrderPackedWebhookHandler implements WebhookHandlerInterface, LoggerAwareInterface @@ -65,13 +65,7 @@ public function handle(object $data): void throw new \InvalidArgumentException(sprintf('Order lines on order %s are different', $data->orderId)); } - $orderShippingStateMachine = $this->stateMachineFactory->get($order, OrderShippingTransitions::GRAPH); - - if ($orderShippingStateMachine->can(OrderShippingTransitions::TRANSITION_SHIP)) { - $this->logger->debug(sprintf('Shipment: Taking the "%s" transition', OrderShippingTransitions::TRANSITION_SHIP)); - - $orderShippingStateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP); - } + $this->completeShipment($order, $data); if ($data->paymentCaptured) { $this->completePayment($order); @@ -126,6 +120,26 @@ private static function assertSame(array $syliusOrderLines, array $peakOrderLine Assert::count($peakOrderLines, 0); } + private function completeShipment(OrderInterface $order, WebhookDataPickOrderPacked $data): void + { + $shipment = $order->getshipments()->last(); + if (false === $shipment) { + $this->logger->debug('There is no shipment on the order'); + + return; + } + + $shipment->setTracking($data->trackingNumber); + + $shipmentStateMachine = $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH); + + if ($shipmentStateMachine->can(ShipmentTransitions::TRANSITION_SHIP)) { + $this->logger->debug(sprintf('Shipment: Taking the "%s" transition', ShipmentTransitions::TRANSITION_SHIP)); + + $shipmentStateMachine->apply(ShipmentTransitions::TRANSITION_SHIP); + } + } + private function completePayment(OrderInterface $order): void { $this->logger->debug(sprintf('The payment is captured, so we will check if we can take the "%s" transition', PaymentTransitions::TRANSITION_COMPLETE));