diff --git a/src/Controller/AddToWishlistAction.php b/src/Controller/AddToWishlistAction.php index 80ef505..4121d17 100644 --- a/src/Controller/AddToWishlistAction.php +++ b/src/Controller/AddToWishlistAction.php @@ -6,6 +6,7 @@ use Doctrine\Persistence\ManagerRegistry; use Setono\Doctrine\ORMTrait; +use Setono\SyliusWishlistPlugin\Controller\DTO\ToggleWishlistResponse; use Setono\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use Setono\SyliusWishlistPlugin\Factory\WishlistItemFactoryInterface; use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; @@ -13,7 +14,7 @@ use Sylius\Component\Core\Model\ProductVariantInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Twig\Environment; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * This controller action is responsible for adding a product or product variant to the wishlist @@ -25,9 +26,9 @@ final class AddToWishlistAction public function __construct( private readonly WishlistProviderInterface $wishlistProvider, private readonly WishlistItemFactoryInterface $wishlistItemFactory, - private readonly Environment $twig, ManagerRegistry $managerRegistry, private readonly WishlistFactoryInterface $wishlistFactory, + private readonly UrlGeneratorInterface $urlGenerator, /** @var class-string $className */ private readonly string $className, ) { @@ -48,26 +49,23 @@ public function __invoke(int $id): JsonResponse if ([] === $preSelectedWishlists) { $preSelectedWishlists = [$this->wishlistFactory->createNew()]; } + + $wishlistItemsCount = 0; foreach ($preSelectedWishlists as $wishlist) { $manager = $this->getManager($wishlist); $manager->persist($wishlist); $wishlist->addItem($wishlistItem); + + $wishlistItemsCount += $wishlist->getQuantity(); } $manager->flush(); - return new JsonResponse([ - 'toggleButton' => $this->twig->render('@SetonoSyliusWishlistPlugin/shop/wishlist/_toggle_button.html.twig', [ - 'product' => $entity instanceof ProductInterface ? $entity : $entity->getProduct(), - 'productVariant' => $entity instanceof ProductVariantInterface ? $entity : null, - ]), -// Will be added in a later version -// 'selectWishlistsForm' => $this->twig->render('@SetonoSyliusWishlistPlugin/shop/wishlist/_select_wishlists.html.twig', [ -// 'product' => $entity instanceof ProductInterface ? $entity : $entity->getProduct(), -// 'productVariant' => $entity instanceof ProductVariantInterface ? $entity : null, -// 'form' => $form->createView(), -// ]), - ]); + return new JsonResponse(new ToggleWishlistResponse( + ToggleWishlistResponse::EVENT_ADDED, + $this->urlGenerator->generate($entity instanceof ProductInterface ? 'setono_sylius_wishlist_shop_wishlist_remove_product' : 'setono_sylius_wishlist_shop_wishlist_remove_product_variant', ['id' => $entity->getId()]), + $wishlistItemsCount, + )); } } diff --git a/src/Controller/DTO/ToggleWishlistResponse.php b/src/Controller/DTO/ToggleWishlistResponse.php new file mode 100644 index 0000000..d9ebc97 --- /dev/null +++ b/src/Controller/DTO/ToggleWishlistResponse.php @@ -0,0 +1,19 @@ + $className */ private readonly string $className, ) { @@ -39,20 +40,19 @@ public function __invoke(Request $request, int $id): JsonResponse throw new NotFoundHttpException(sprintf('Product with id %s not found', $id)); } - foreach ($this->wishlistProvider->getWishlists() as $wishlistEntity) { - $entity instanceof ProductInterface ? $wishlistEntity->removeProduct($entity) : $wishlistEntity->removeProductVariant($entity); + $wishlistItemsCount = 0; + foreach ($this->wishlistProvider->getWishlists() as $wishlist) { + $entity instanceof ProductInterface ? $wishlist->removeProduct($entity) : $wishlist->removeProductVariant($entity); + + $wishlistItemsCount += $wishlist->getQuantity(); } $this->getManager($entity)->flush(); - return new JsonResponse([ - 'toggleButton' => $this->twig->render( - '@SetonoSyliusWishlistPlugin/shop/wishlist/_toggle_button.html.twig', - [ - 'product' => $entity instanceof ProductInterface ? $entity : $entity->getProduct(), - 'productVariant' => $entity instanceof ProductVariantInterface ? $entity : null, - ], - ), - ]); + return new JsonResponse(new ToggleWishlistResponse( + ToggleWishlistResponse::EVENT_REMOVED, + $this->urlGenerator->generate($entity instanceof ProductInterface ? 'setono_sylius_wishlist_shop_wishlist_add_product' : 'setono_sylius_wishlist_shop_wishlist_add_product_variant', ['id' => $entity->getId()]), + $wishlistItemsCount, + )); } } diff --git a/src/Model/Wishlist.php b/src/Model/Wishlist.php index 9d14770..477ea94 100644 --- a/src/Model/Wishlist.php +++ b/src/Model/Wishlist.php @@ -123,4 +123,14 @@ public function removeProductVariant(ProductVariantInterface $productVariant): v } } } + + public function getQuantity(): int + { + $quantity = 0; + foreach ($this->items as $item) { + $quantity += $item->getQuantity(); + } + + return $quantity; + } } diff --git a/src/Model/WishlistInterface.php b/src/Model/WishlistInterface.php index 1b36c46..82333e0 100644 --- a/src/Model/WishlistInterface.php +++ b/src/Model/WishlistInterface.php @@ -40,4 +40,6 @@ public function removeProduct(ProductInterface $product): void; public function hasProductVariant(ProductVariantInterface $productVariant): bool; public function removeProductVariant(ProductVariantInterface $productVariant): void; + + public function getQuantity(): int; } diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 1295770..86c488e 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -51,9 +51,9 @@ class="Setono\SyliusWishlistPlugin\Controller\AddToWishlistAction" public="true" abstract="true"> - + - + - button.wishlist-toggle { + .ssw-toggle { border: none; margin: 0; padding: 0; @@ -14,4 +14,12 @@ line-height: 30px; cursor: pointer; } + + .ssw-toggle:not(.ssw-added) i.heart:not(.outline) { + display: none; + } + + .ssw-added i.heart.outline { + display: none; + } diff --git a/src/Resources/views/shop/wishlist/_toggle_button.html.twig b/src/Resources/views/shop/wishlist/_toggle_button.html.twig index 698a803..ed9acd1 100644 --- a/src/Resources/views/shop/wishlist/_toggle_button.html.twig +++ b/src/Resources/views/shop/wishlist/_toggle_button.html.twig @@ -9,10 +9,7 @@ {%- set wishlistPath = onWishlist ? path('setono_sylius_wishlist_shop_wishlist_remove_product', { 'id': product.id }) : path('setono_sylius_wishlist_shop_wishlist_add_product', { 'id': product.id }) -%} {%- endif -%} -