-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OP-550 - Fix displaying products in shop page
- Loading branch information
Showing
11 changed files
with
232 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.