diff --git a/src/Inventory/Operator/OrderInventoryOperator.php b/src/Inventory/Operator/OrderInventoryOperator.php index 2ec16c7c..c8bcbec4 100644 --- a/src/Inventory/Operator/OrderInventoryOperator.php +++ b/src/Inventory/Operator/OrderInventoryOperator.php @@ -28,8 +28,8 @@ final class OrderInventoryOperator implements OrderInventoryOperatorInterface { public function __construct( private readonly OrderInventoryOperatorInterface $decorated, - private readonly FeatureFlagCheckerInterface $featureFlagChecker, private readonly EntityManagerInterface $productVariantManager, + private readonly FeatureFlagCheckerInterface $featureFlagChecker, private readonly ProductBundleOrderInventoryOperatorInterface $productBundleOrderInventoryOperator, ) { } @@ -58,13 +58,14 @@ public function cancel(OrderInterface $order): void public function hold(OrderInterface $order): void { + $this->lockOrderProductVariants($order); + if (!$this->featureFlagChecker->isEnabled()) { $this->decorated->hold($order); - } - $this->lockOrderProductVariants($order); + return; + } - //TODO figure out how to split handling bundles and products without copy-pasted code $this->productBundleOrderInventoryOperator->hold($order); } @@ -147,7 +148,7 @@ private function lockOrderItemProductVariants(OrderItemInterface $orderItem): vo { /** @var ProductInterface $product */ $product = $orderItem->getProduct(); - if ($product->isBundle()) { + if ($this->featureFlagChecker->isEnabled() && $product->isBundle()) { $this->lockBundledOrderItemProductVariants($orderItem); } else { $this->lockOrderItemProductVariant($orderItem); diff --git a/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php b/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php index 95a102b6..296afc34 100644 --- a/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php +++ b/src/Inventory/Operator/ProductBundleOrderInventoryOperator.php @@ -12,7 +12,9 @@ namespace BitBag\SyliusProductBundlePlugin\Inventory\Operator; use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface; +use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Core\Model\ProductVariantInterface; final class ProductBundleOrderInventoryOperator implements ProductBundleOrderInventoryOperatorInterface { @@ -20,7 +22,13 @@ public function hold(OrderInterface $order): void { /** @var OrderItemInterface $orderItem */ foreach ($order->getItems() as $orderItem) { - $this->holdBundleOrderItems($orderItem); + /** @var ProductInterface $product */ + $product = $orderItem->getProduct(); + if ($product->isBundle()) { + $this->holdBundleOrderItem($orderItem); + } else { + $this->holdRegularOrderItem($orderItem); + } } } @@ -29,17 +37,30 @@ public function sell(OrderInterface $order): void // TODO: Implement sell() method. } - private function holdBundleOrderItems(OrderItemInterface $orderItem): void + private function holdBundleOrderItem(OrderItemInterface $orderItem): void { foreach ($orderItem->getProductBundleOrderItems() as $bundleOrderItem) { $quantity = $orderItem->getQuantity() * $bundleOrderItem->getQuantity(); $variant = $bundleOrderItem->getProductVariant(); - if (!$variant->isTracked()) { - continue; - } + $this->holdProductVariant($variant, $quantity); + } + } + + private function holdRegularOrderItem(OrderItemInterface $orderItem): void + { + $quantity = $orderItem->getQuantity(); + $variant = $orderItem->getVariant(); + + $this->holdProductVariant($variant, $quantity); + } - $variant->setOnHold((int)$variant->getOnHold() + $quantity); + private function holdProductVariant(ProductVariantInterface $variant, int $quantity): void + { + if (!$variant->isTracked()) { + return; } + + $variant->setOnHold((int) $variant->getOnHold() + $quantity); } } diff --git a/src/Resources/config/services/operator.xml b/src/Resources/config/services/operator.xml index 2c39fd39..c353aa9e 100644 --- a/src/Resources/config/services/operator.xml +++ b/src/Resources/config/services/operator.xml @@ -3,13 +3,13 @@ - - + decorates="sylius.inventory.order_inventory_operator"> + + -