Skip to content

Commit

Permalink
OP-550 - Fix displaying products in shop page
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPalen committed Dec 17, 2024
1 parent 702918a commit a8daee4
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 213 deletions.
130 changes: 130 additions & 0 deletions src/Component/Product/AddToCartFormComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\Component\Product;

use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartDtoFactory;
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\CoreBundle\Provider\FlashBagProvider;
use Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactory;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\AddToCartFormComponent as BaseAddToCartFormComponent;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductLivePropTrait;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductVariantLivePropTrait;
use Sylius\Bundle\UiBundle\Twig\Component\TemplatePropTrait;
use Sylius\Component\Core\Factory\CartItemFactoryInterface;
use Sylius\Component\Core\Model\OrderItem;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\SyliusCartEvents;
use Sylius\TwigHooks\LiveComponent\HookableLiveComponentTrait;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
final class AddToCartFormComponent extends BaseAddToCartFormComponent
{
//TODO there should be decoration instead of extension ? To be tested
use ComponentToolsTrait;
use ComponentWithFormTrait;
use DefaultActionTrait;
use HookableLiveComponentTrait;
use ProductLivePropTrait;
use ProductVariantLivePropTrait;
use TemplatePropTrait;

/**
* @param CartItemFactoryInterface<OrderItem> $cartItemFactory
* @param class-string $formClass
* @param ProductRepositoryInterface<ProductInterface> $productRepository
* @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository
*/
public function __construct(
private readonly FormFactoryInterface $formFactory,
private readonly ObjectManager $manager,
private readonly RouterInterface $router,
private readonly RequestStack $requestStack,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly CartContextInterface $cartContext,
private readonly AddToCartCommandFactory $addToCartCommandFactory,
private readonly CartItemFactoryInterface $cartItemFactory,
private readonly string $formClass,
ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository,
private readonly AddProductBundleToCartDtoFactory $addProductBundleToCartDtoFactory,

) {
$this->initializeProduct($productRepository);
$this->initializeProductVariant($productVariantRepository);

parent::__construct($this->formFactory,
$this->manager,
$this->router,
$this->requestStack,
$this->eventDispatcher,
$this->cartContext,
$this->addToCartCommandFactory,
$this->cartItemFactory,
$this->formClass,
$this->productRepository,
$this->productVariantRepository,
);
}

#[LiveAction]
public function addToCart(): RedirectResponse
{
$this->submitForm();
$addToCartCommand = $this->getForm()->getData();

$this->eventDispatcher->dispatch(new GenericEvent($addToCartCommand), SyliusCartEvents::CART_ITEM_ADD);
$this->manager->persist($addToCartCommand->getCart());
$this->manager->flush();

FlashBagProvider
::getFlashBag($this->requestStack)
->add('success', 'sylius.cart.add_item');

return new RedirectResponse($this->router->generate(
$this->routeName,
$this->routeParameters,
));
}

protected function instantiateForm(): FormInterface
{
/** @var OrderItemInterface $orderItem */
$orderItem = $this->cartItemFactory->createForProduct($this->product);
/** @var null|ProductInterface $orderProduct */
$orderProduct = $orderItem->getProduct();

$addToCartCommand = $this->addProductBundleToCartDtoFactory->createNew(
$this->cartContext->getCart(),
$orderItem,
$orderProduct,
);

return $this->formFactory->create($this->formClass, $addToCartCommand, ['product' => $this->product]);
}
}
169 changes: 0 additions & 169 deletions src/Controller/OrderItemController.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/EventListener/CartItemAddListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\EventListener;

use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface;
use Sylius\Component\Order\Modifier\OrderModifierInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;

final class CartItemAddListener
{
public function __construct(
private readonly OrderModifierInterface $orderModifier
) {
}

public function addToOrder(GenericEvent $event): void
{
$addToCartCommand = $event->getSubject();

Assert::isInstanceOf($addToCartCommand, AddProductBundleToCartDtoInterface::class);

$this->orderModifier->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
}
}
8 changes: 5 additions & 3 deletions src/Factory/AddProductBundleToCartDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public function createNew(
/**
* @return AddProductBundleItemToCartCommand[]
*/
private function getProcessedProductBundleItems(ProductBundleInterface $productBundle): array
private function getProcessedProductBundleItems(?ProductBundleInterface $productBundle): array
{
$addProductBundleItemToCartCommands = [];

foreach ($productBundle->getProductBundleItems() as $bundleItem) {
$addProductBundleItemToCartCommands[] = $this->addProductBundleItemToCartCommandFactory->createNew($bundleItem);
if (null !== $productBundle) {
foreach ($productBundle->getProductBundleItems() as $bundleItem) {
$addProductBundleItemToCartCommands[] = $this->addProductBundleItemToCartCommandFactory->createNew($bundleItem);
}
}

return $addProductBundleItemToCartCommands;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<import resource="services/twig.xml"/>
<import resource="services/validator.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/product.xml"/>
</imports>
</container>
8 changes: 8 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
<tag name="kernel.event_listener" event="sylius.product.pre_update" method="addProductToProductBundle" />
<tag name="kernel.event_listener" event="sylius.product.pre_create" method="addProductToProductBundle" />
</service>

<service id="bitbag_sylius_product_bundle_plugin.listener.cart_item_add"
class="BitBag\SyliusProductBundlePlugin\EventListener\CartItemAddListener"
decorates="sylius_shop.listener.cart_item_add"
>
<argument type="service" id="sylius.modifier.order" />
<tag name="kernel.event_listener" event="sylius.cart_item_add" method="addToOrder" />
</service>
</services>
</container>
Loading

0 comments on commit a8daee4

Please sign in to comment.