Skip to content

Commit 3dff2a8

Browse files
committed
OP-289: Fix handling unpacked bundles in shop
1 parent 65e9982 commit 3dff2a8

8 files changed

Lines changed: 61 additions & 9 deletions

src/Command/AddProductBundleToCartCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111

1212
namespace BitBag\SyliusProductBundlePlugin\Command;
1313

14+
use Doctrine\Common\Collections\Collection;
15+
1416
final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface, ProductCodeAwareInterface
1517
{
18+
/** @var Collection<int, AddProductBundleItemToCartCommand> */
19+
private Collection $productBundleItems;
20+
1621
public function __construct(
1722
private int $orderId,
1823
private string $productCode,
@@ -34,4 +39,16 @@ public function getQuantity(): int
3439
{
3540
return $this->quantity;
3641
}
42+
43+
/** @return Collection<int, AddProductBundleItemToCartCommand> */
44+
public function getProductBundleItems(): Collection
45+
{
46+
return $this->productBundleItems;
47+
}
48+
49+
/** @param Collection<int, AddProductBundleItemToCartCommand> $productBundleItems */
50+
public function setProductBundleItems(Collection $productBundleItems): void
51+
{
52+
$this->productBundleItems = $productBundleItems;
53+
}
3754
}

src/Factory/AddProductBundleToCartCommandFactory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,33 @@
1111

1212
namespace BitBag\SyliusProductBundlePlugin\Factory;
1313

14+
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand;
1415
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand;
1516
use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface;
17+
use Doctrine\Common\Collections\Collection;
1618

1719
final class AddProductBundleToCartCommandFactory implements AddProductBundleToCartCommandFactoryInterface
1820
{
21+
/** @param Collection<int, AddProductBundleItemToCartCommand> */
1922
public function createNew(
2023
int $orderId,
2124
string $productCode,
2225
int $quantity,
26+
Collection $productBundleItems,
2327
): AddProductBundleToCartCommand {
24-
return new AddProductBundleToCartCommand($orderId, $productCode, $quantity);
28+
$command = new AddProductBundleToCartCommand($orderId, $productCode, $quantity);
29+
$command->setProductBundleItems($productBundleItems);
30+
31+
return $command;
2532
}
2633

2734
public function createFromDto(AddProductBundleToCartDtoInterface $dto): AddProductBundleToCartCommand
2835
{
2936
$cartId = $dto->getCart()->getId();
3037
$productCode = $dto->getProduct()->getCode() ?? '';
3138
$quantity = $dto->getCartItem()->getQuantity();
39+
$productBundleItems = $dto->getProductBundleItems();
3240

33-
return $this->createNew($cartId, $productCode, $quantity);
41+
return $this->createNew($cartId, $productCode, $quantity, $productBundleItems);
3442
}
3543
}

src/Factory/AddProductBundleToCartCommandFactoryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313

1414
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand;
1515
use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface;
16+
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface;
17+
use Doctrine\Common\Collections\Collection;
1618

1719
interface AddProductBundleToCartCommandFactoryInterface
1820
{
21+
/** @param Collection<int, ProductBundleOrderItemInterface> $productBundleItems */
1922
public function createNew(
2023
int $orderId,
2124
string $productCode,
2225
int $quantity,
26+
Collection $productBundleItems,
2327
): AddProductBundleToCartCommand;
2428

2529
public function createFromDto(AddProductBundleToCartDtoInterface $dto): AddProductBundleToCartCommand;

src/Factory/ProductBundleOrderItemFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace BitBag\SyliusProductBundlePlugin\Factory;
1313

14+
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand;
1415
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
1516
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface;
1617
use Sylius\Component\Resource\Factory\FactoryInterface;
@@ -41,4 +42,16 @@ public function createFromProductBundleItem(ProductBundleItemInterface $bundleIt
4142

4243
return $productBundleOrderItem;
4344
}
45+
46+
public function createFromAddProductBundleItemToCartCommand(AddProductBundleItemToCartCommand $addItemToCartCommand): ProductBundleOrderItemInterface
47+
{
48+
/** @var ProductBundleOrderItemInterface $productBundleOrderItem */
49+
$productBundleOrderItem = $this->decoratedFactory->createNew();
50+
51+
$productBundleOrderItem->setProductBundleItem($addItemToCartCommand->getProductBundleItem());
52+
$productBundleOrderItem->setProductVariant($addItemToCartCommand->getProductVariant());
53+
$productBundleOrderItem->setQuantity($addItemToCartCommand->getQuantity());
54+
55+
return $productBundleOrderItem;
56+
}
4457
}

src/Factory/ProductBundleOrderItemFactoryInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

1212
namespace BitBag\SyliusProductBundlePlugin\Factory;
1313

14+
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand;
1415
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
1516
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface;
1617
use Sylius\Component\Resource\Factory\FactoryInterface;
1718

1819
interface ProductBundleOrderItemFactoryInterface extends FactoryInterface
1920
{
2021
public function createFromProductBundleItem(ProductBundleItemInterface $bundleItem): ProductBundleOrderItemInterface;
22+
23+
public function createFromAddProductBundleItemToCartCommand(AddProductBundleItemToCartCommand $addItemToCartCommand): ProductBundleOrderItemInterface;
2124
}

src/Handler/AddProductBundleToCartHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function __invoke(AddProductBundleToCartCommand $addProductBundleToCartCo
4646
$quantity = $addProductBundleToCartCommand->getQuantity();
4747
Assert::greaterThan($quantity, 0);
4848

49-
$this->cartProcessor->process($cart, $productBundle, $quantity);
49+
$items = $addProductBundleToCartCommand->getProductBundleItems();
50+
Assert::false($items->isEmpty());
51+
52+
$this->cartProcessor->process($cart, $productBundle, $quantity, $items);
5053
$this->orderRepository->add($cart);
5154
}
5255
}

src/Handler/AddProductBundleToCartHandler/CartProcessor.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
1515
use BitBag\SyliusProductBundlePlugin\Factory\OrderItemFactoryInterface;
1616
use BitBag\SyliusProductBundlePlugin\Factory\ProductBundleOrderItemFactoryInterface;
17+
use Doctrine\Common\Collections\Collection;
1718
use Sylius\Component\Core\Model\ProductVariantInterface;
1819
use Sylius\Component\Order\Model\OrderInterface;
1920
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
@@ -23,17 +24,18 @@
2324
final class CartProcessor implements CartProcessorInterface
2425
{
2526
public function __construct(
26-
private OrderItemQuantityModifierInterface $orderItemQuantityModifier,
27-
private ProductBundleOrderItemFactoryInterface $productBundleOrderItemFactory,
28-
private OrderModifierInterface $orderModifier,
29-
private OrderItemFactoryInterface $cartItemFactory,
27+
private readonly OrderItemQuantityModifierInterface $orderItemQuantityModifier,
28+
private readonly ProductBundleOrderItemFactoryInterface $productBundleOrderItemFactory,
29+
private readonly OrderModifierInterface $orderModifier,
30+
private readonly OrderItemFactoryInterface $cartItemFactory,
3031
) {
3132
}
3233

3334
public function process(
3435
OrderInterface $cart,
3536
ProductBundleInterface $productBundle,
3637
int $quantity,
38+
Collection $productBundleOrderItems,
3739
): void {
3840
Assert::greaterThan($quantity, 0);
3941

@@ -47,8 +49,8 @@ public function process(
4749
$cartItem = $this->cartItemFactory->createWithVariant($productVariant);
4850
$this->orderItemQuantityModifier->modify($cartItem, $quantity);
4951

50-
foreach ($productBundle->getProductBundleItems() as $bundleItem) {
51-
$productBundleOrderItem = $this->productBundleOrderItemFactory->createFromProductBundleItem($bundleItem);
52+
foreach ($productBundleOrderItems as $addBundleItemToCartCommand) {
53+
$productBundleOrderItem = $this->productBundleOrderItemFactory->createFromAddProductBundleItemToCartCommand($addBundleItemToCartCommand);
5254
$cartItem->addProductBundleOrderItem($productBundleOrderItem);
5355
}
5456

src/Handler/AddProductBundleToCartHandler/CartProcessorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace BitBag\SyliusProductBundlePlugin\Handler\AddProductBundleToCartHandler;
1313

1414
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
15+
use Doctrine\Common\Collections\Collection;
1516
use Sylius\Component\Order\Model\OrderInterface;
1617

1718
interface CartProcessorInterface
@@ -20,5 +21,6 @@ public function process(
2021
OrderInterface $cart,
2122
ProductBundleInterface $productBundle,
2223
int $quantity,
24+
Collection $productBundleOrderItems,
2325
): void;
2426
}

0 commit comments

Comments
 (0)