diff --git a/src/Command/AddProductBundleToCartCommand.php b/src/Command/AddProductBundleToCartCommand.php index 564a7bb6..6f594132 100644 --- a/src/Command/AddProductBundleToCartCommand.php +++ b/src/Command/AddProductBundleToCartCommand.php @@ -11,6 +11,7 @@ namespace BitBag\SyliusProductBundlePlugin\Command; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface, ProductCodeAwareInterface @@ -19,10 +20,11 @@ final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface private Collection $productBundleItems; public function __construct( - private int $orderId, - private string $productCode, - private int $quantity = 1, + private readonly int $orderId, + private readonly string $productCode, + private readonly int $quantity = 1, ) { + $this->productBundleItems = new ArrayCollection(); } public function getOrderId(): int diff --git a/src/DataTransformer/AddProductBundleToCartDtoDataTransformer.php b/src/DataTransformer/AddProductBundleToCartDtoDataTransformer.php index 323a6b30..23b3cce4 100644 --- a/src/DataTransformer/AddProductBundleToCartDtoDataTransformer.php +++ b/src/DataTransformer/AddProductBundleToCartDtoDataTransformer.php @@ -14,6 +14,7 @@ use ApiPlatform\Core\DataTransformer\DataTransformerInterface; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand; use BitBag\SyliusProductBundlePlugin\Dto\Api\AddProductBundleToCartDto; +use BitBag\SyliusProductBundlePlugin\Provider\AddProductBundleItemToCartCommandProviderInterface; use Sylius\Component\Order\Model\OrderInterface; use Webmozart\Assert\Assert; @@ -21,6 +22,11 @@ final class AddProductBundleToCartDtoDataTransformer implements DataTransformerI { public const OBJECT_TO_POPULATE = 'object_to_populate'; + public function __construct( + private readonly AddProductBundleItemToCartCommandProviderInterface $addProductBundleItemToCartCommandProvider, + ) { + } + /** * @param AddProductBundleToCartDto|object $object */ @@ -37,8 +43,13 @@ public function transform( $productCode = $object->getProductCode(); $quantity = $object->getQuantity(); + $variantCodes = $object->getVariantCodes(); + $addItemToCartCommands = $this->addProductBundleItemToCartCommandProvider->provide($productCode, $variantCodes); + + $command = new AddProductBundleToCartCommand($cart->getId(), $productCode, $quantity); + $command->setProductBundleItems($addItemToCartCommands); - return new AddProductBundleToCartCommand($cart->getId(), $productCode, $quantity); + return $command; } public function supportsTransformation( diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 569fa83a..16abc3a3 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -19,6 +19,7 @@ use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface; use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleItemType; use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleType; +use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleItemRepository; use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleRepository; use Sylius\Bundle\ResourceBundle\Controller\ResourceController; use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; @@ -80,7 +81,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void ->scalarNode('interface')->defaultValue(ProductBundleItemInterface::class)->cannotBeEmpty()->end() ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end() - ->scalarNode('repository')->cannotBeEmpty()->end() + ->scalarNode('repository')->defaultValue(ProductBundleItemRepository::class)->cannotBeEmpty()->end() ->scalarNode('form')->defaultValue(ProductBundleItemType::class)->cannotBeEmpty()->end() ->end() ->end() diff --git a/src/Dto/Api/AddProductBundleToCartDto.php b/src/Dto/Api/AddProductBundleToCartDto.php index 010434d9..f087584d 100644 --- a/src/Dto/Api/AddProductBundleToCartDto.php +++ b/src/Dto/Api/AddProductBundleToCartDto.php @@ -19,6 +19,7 @@ public function __construct( private string $productCode, private int $quantity = 1, private ?string $orderTokenValue = null, + private array $variantCodes = [], ) { } @@ -41,4 +42,14 @@ public function getQuantity(): int { return $this->quantity; } + + public function getVariantCodes(): array + { + return $this->variantCodes; + } + + public function setVariantCodes(array $variantCodes): void + { + $this->variantCodes = $variantCodes; + } } diff --git a/src/Factory/AddProductBundleToCartCommandFactory.php b/src/Factory/AddProductBundleToCartCommandFactory.php index 177082f0..dd66796b 100644 --- a/src/Factory/AddProductBundleToCartCommandFactory.php +++ b/src/Factory/AddProductBundleToCartCommandFactory.php @@ -18,7 +18,7 @@ final class AddProductBundleToCartCommandFactory implements AddProductBundleToCartCommandFactoryInterface { - /** @param Collection */ + /** @param Collection $productBundleItems */ public function createNew( int $orderId, string $productCode, diff --git a/src/Handler/AddProductBundleToCartHandler.php b/src/Handler/AddProductBundleToCartHandler.php index 004965e1..fc88282e 100644 --- a/src/Handler/AddProductBundleToCartHandler.php +++ b/src/Handler/AddProductBundleToCartHandler.php @@ -23,9 +23,9 @@ final class AddProductBundleToCartHandler implements MessageHandlerInterface { public function __construct( - private OrderRepositoryInterface $orderRepository, - private ProductRepositoryInterface $productRepository, - private CartProcessorInterface $cartProcessor, + private readonly OrderRepositoryInterface $orderRepository, + private readonly ProductRepositoryInterface $productRepository, + private readonly CartProcessorInterface $cartProcessor, ) { } diff --git a/src/Provider/AddProductBundleItemToCartCommandProvider.php b/src/Provider/AddProductBundleItemToCartCommandProvider.php new file mode 100644 index 00000000..ea42bf0b --- /dev/null +++ b/src/Provider/AddProductBundleItemToCartCommandProvider.php @@ -0,0 +1,44 @@ + */ + public function provide(string $bundleCode, array $variantCodes): Collection + { + $bundleItems = $this->productBundleItemRepository->findByBundleCode($bundleCode); + + $items = ''; + foreach ($bundleItems as $bundleItem) { + $items .= $bundleItem->getProductVariant()->getCode() . ', '; + } + + $commands = []; + foreach ($bundleItems as $bundleItem) { + $command = $this->addProductBundleItemToCartCommandFactory->createNew($bundleItem); + //TODO implement overwritting bundle's variants with variants provided in API call + } + } +} diff --git a/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php b/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php new file mode 100644 index 00000000..a0a4c6f2 --- /dev/null +++ b/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php @@ -0,0 +1,21 @@ + */ + public function provide(string $bundleCode, array $variantCodes): Collection; +} diff --git a/src/Repository/ProductBundleItemRepository.php b/src/Repository/ProductBundleItemRepository.php new file mode 100644 index 00000000..29995866 --- /dev/null +++ b/src/Repository/ProductBundleItemRepository.php @@ -0,0 +1,31 @@ +createQueryBuilder('pbi') + ->leftJoin('pbi.productBundle', 'pb') + ->leftJoin('pb.product', 'p') + ->where('p.code = :code') + ->setParameter('code', $bundleCode) + ->getQuery() + ->getResult(); + } +} diff --git a/src/Repository/ProductBundleItemRepositoryInterface.php b/src/Repository/ProductBundleItemRepositoryInterface.php new file mode 100644 index 00000000..e431613f --- /dev/null +++ b/src/Repository/ProductBundleItemRepositoryInterface.php @@ -0,0 +1,21 @@ + shop:cart:add_product_bundle + + shop:cart:add_product_bundle + diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index a968c028..064ba3c6 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -11,6 +11,7 @@ + diff --git a/src/Resources/config/services/provider.xml b/src/Resources/config/services/provider.xml new file mode 100644 index 00000000..e4669394 --- /dev/null +++ b/src/Resources/config/services/provider.xml @@ -0,0 +1,14 @@ + + + + + + + + + + diff --git a/src/Resources/config/services/transformer.xml b/src/Resources/config/services/transformer.xml index 4f212350..ccac7ace 100644 --- a/src/Resources/config/services/transformer.xml +++ b/src/Resources/config/services/transformer.xml @@ -7,6 +7,7 @@ id="bitbag_sylius_product_bundle.data_transformer.add_product_bundle_to_cart_dto" class="BitBag\SyliusProductBundlePlugin\DataTransformer\AddProductBundleToCartDtoDataTransformer" > +