Skip to content

Commit

Permalink
OP-289: On-hold handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Sep 16, 2024
1 parent 892447b commit 917c6f9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/Inventory/Operator/OrderInventoryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
33 changes: 27 additions & 6 deletions src/Inventory/Operator/ProductBundleOrderInventoryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
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
{
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);
}
}
}

Expand All @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Resources/config/services/operator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="bitbag_sylius_product_bundle.operator.order_inventory_operator"
<service id="sylius.custom_inventory.order_inventory_operator"
class="BitBag\SyliusProductBundlePlugin\Inventory\Operator\OrderInventoryOperator"
decorates="sylius.custom_inventory.order_inventory_operator">
<argument type="service" id="bitbag_sylius_product_bundle.operator.order_inventory_operator.inner"/>
decorates="sylius.inventory.order_inventory_operator">
<argument type="service" id="sylius.custom_inventory.order_inventory_operator.inner"/>
<argument type="service" id="sylius.manager.product_variant"/>
<argument type="service"
id="bitbag_sylius_product_bundle.checker.bundled_product_inventory_management_feature_flag_checker"/>
<argument type="service" id="sylius.manager.product_variant"/>
<argument type="service"
id="bitbag_sylius_product_bundle.operator.product_bundle_order_inventory_operator"/>
</service>
Expand Down

0 comments on commit 917c6f9

Please sign in to comment.