From fc05b684f5e63914a319de86feae2d2d443833d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 3 Dec 2024 10:18:13 +0100 Subject: [PATCH] Fixed more todos --- src/Controller/RemoveWishlistItemAction.php | 11 +++++----- src/Factory/WishlistItemFactory.php | 6 ++++++ src/Form/Type/WishlistItemType.php | 22 +++++++++++++------- src/Provider/CompositeWishlistProvider.php | 10 +++++++-- src/Resources/config/routes.yaml | 4 ---- src/Resources/config/routes/admin.yaml | 1 - src/Resources/config/routes_no_locale.yaml | 6 +----- src/Resources/config/services/controller.xml | 2 +- src/Resources/config/services/form.xml | 12 ++++++++++- 9 files changed, 46 insertions(+), 28 deletions(-) delete mode 100644 src/Resources/config/routes/admin.yaml diff --git a/src/Controller/RemoveWishlistItemAction.php b/src/Controller/RemoveWishlistItemAction.php index d0eac9a..cdf979f 100644 --- a/src/Controller/RemoveWishlistItemAction.php +++ b/src/Controller/RemoveWishlistItemAction.php @@ -6,7 +6,6 @@ use Doctrine\Persistence\ManagerRegistry; use Setono\Doctrine\ORMTrait; -use Setono\SyliusWishlistPlugin\Model\UserWishlistInterface; use Setono\SyliusWishlistPlugin\Model\WishlistInterface; use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; use Setono\SyliusWishlistPlugin\Security\Voter\WishlistVoter; @@ -24,20 +23,20 @@ public function __construct( private readonly UrlGeneratorInterface $urlGenerator, private readonly Security $security, ManagerRegistry $managerRegistry, - /** @var class-string $userWishlistClass */ - private readonly string $userWishlistClass, + /** @var class-string $wishlistClass */ + private readonly string $wishlistClass, ) { $this->managerRegistry = $managerRegistry; } public function __invoke(string $uuid, int $id): RedirectResponse { - $manager = $this->getManager($this->userWishlistClass); + $manager = $this->getManager($this->wishlistClass); - /** @var UserWishlistInterface|null $wishlist */ + /** @var WishlistInterface|null $wishlist */ $wishlist = $manager->createQueryBuilder() ->select('o') - ->from($this->userWishlistClass, 'o') + ->from($this->wishlistClass, 'o') ->andWhere('o.uuid = :uuid') ->setParameter('uuid', $uuid) ->getQuery() diff --git a/src/Factory/WishlistItemFactory.php b/src/Factory/WishlistItemFactory.php index 9a6aa8d..f5f13d5 100644 --- a/src/Factory/WishlistItemFactory.php +++ b/src/Factory/WishlistItemFactory.php @@ -30,6 +30,12 @@ public function createWithProduct(ProductInterface $product, int $quantity = 1): $wishlistItem->setProduct($product); $wishlistItem->setQuantity($quantity); + if ($product->isSimple()) { + $variant = $product->getVariants()->first(); + Assert::isInstanceOf($variant, ProductVariantInterface::class); + $wishlistItem->setVariant($variant); + } + return $wishlistItem; } diff --git a/src/Form/Type/WishlistItemType.php b/src/Form/Type/WishlistItemType.php index 6b3e5eb..e9c8e39 100644 --- a/src/Form/Type/WishlistItemType.php +++ b/src/Form/Type/WishlistItemType.php @@ -9,6 +9,7 @@ use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; use Sylius\Component\Core\Model\ProductVariantInterface; use Symfony\Component\Form\Extension\Core\Type\IntegerType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; @@ -33,15 +34,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void return; } - // todo handle simple products - $form = $event->getForm(); - $form->add('variant', ProductVariantChoiceType::class, [ - 'choice_label' => fn (ProductVariantInterface $variant) => implode(', ', array_map(static fn ($optionValue) => sprintf('%s: %s', (string) $optionValue->getOption()?->getName(), (string) $optionValue->getValue()), $variant->getOptionValues()->toArray())), - 'placeholder' => 'setono_sylius_wishlist.ui.select_variant', - 'product' => $product, - 'expanded' => false, - ]); + + if ($product->isSimple()) { + $form->add('variant', TextType::class, [ + 'disabled' => true, + ]); + } else { + $form->add('variant', ProductVariantChoiceType::class, [ + 'choice_label' => fn (ProductVariantInterface $variant) => implode(', ', array_map(static fn ($optionValue) => sprintf('%s: %s', (string) $optionValue->getOption()?->getName(), (string) $optionValue->getValue()), $variant->getOptionValues()->toArray())), + 'placeholder' => 'setono_sylius_wishlist.ui.select_variant', + 'product' => $product, + 'expanded' => false, + ]); + } }); } diff --git a/src/Provider/CompositeWishlistProvider.php b/src/Provider/CompositeWishlistProvider.php index 91a4feb..6497da9 100644 --- a/src/Provider/CompositeWishlistProvider.php +++ b/src/Provider/CompositeWishlistProvider.php @@ -25,7 +25,13 @@ public function getWishlists(): array public function getPreSelectedWishlists(): array { - // todo implement - return $this->getWishlists(); + foreach ($this->services as $service) { + $wishlists = $service->getPreSelectedWishlists(); + if ([] !== $wishlists) { + return $wishlists; + } + } + + return []; } } diff --git a/src/Resources/config/routes.yaml b/src/Resources/config/routes.yaml index 65ac933..233b9cd 100644 --- a/src/Resources/config/routes.yaml +++ b/src/Resources/config/routes.yaml @@ -3,7 +3,3 @@ setono_sylius_wishlist_shop: prefix: /{_locale} requirements: _locale: ^[A-Za-z]{2,4}(_([A-Za-z]{4}|[0-9]{3}))?(_([A-Za-z]{2}|[0-9]{3}))?$ - -setono_sylius_wishlist_admin: - resource: "@SetonoSyliusWishlistPlugin/Resources/config/routes/admin.yaml" - prefix: /admin diff --git a/src/Resources/config/routes/admin.yaml b/src/Resources/config/routes/admin.yaml deleted file mode 100644 index be4ae0c..0000000 --- a/src/Resources/config/routes/admin.yaml +++ /dev/null @@ -1 +0,0 @@ -# TODO Add your admin routes here \ No newline at end of file diff --git a/src/Resources/config/routes_no_locale.yaml b/src/Resources/config/routes_no_locale.yaml index b8b119a..9787b45 100644 --- a/src/Resources/config/routes_no_locale.yaml +++ b/src/Resources/config/routes_no_locale.yaml @@ -1,10 +1,6 @@ -# By default Sylius uses urls like https://example.com/en_US/, but Sylius also offers the option to disable +# By default, Sylius uses urls like https://example.com/en_US/, but Sylius also offers the option to disable # localized URLs (see https://docs.sylius.com/en/latest/cookbook/shop/disabling-localised-urls.html). # To accommodate this in your plugin, provide a routes file for non localized stores and tell this in the README.md setono_sylius_wishlist_shop: resource: "@SetonoSyliusWishlistPlugin/Resources/config/routes/shop.yaml" - -setono_sylius_wishlist_admin: - resource: "@SetonoSyliusWishlistPlugin/Resources/config/routes/admin.yaml" - prefix: /admin diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 8ec4839..9a7df01 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -33,7 +33,7 @@ - %setono_sylius_wishlist.model.user_wishlist.class% + %setono_sylius_wishlist.model.wishlist.class% diff --git a/src/Resources/config/services/form.xml b/src/Resources/config/services/form.xml index 375737a..737d2b0 100644 --- a/src/Resources/config/services/form.xml +++ b/src/Resources/config/services/form.xml @@ -1,16 +1,26 @@ + + + setono_sylius_wishlist + + + + setono_sylius_wishlist + + - %setono_sylius_wishlist.model.wishlist.class% + %setono_sylius_wishlist.form.type.wishlist.validation_groups% %setono_sylius_wishlist.model.wishlist_item.class% + %setono_sylius_wishlist.form.type.wishlist_item.validation_groups%