From 47a3e8595dc8fd2aaaf485515907ff191e4f8fc3 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Mon, 24 Jun 2024 14:31:04 +0200 Subject: [PATCH 1/3] Fix adding order item to the order --- src/Form/Extension/OrderTypeExtension.php | 10 ++++++---- src/Form/Type/OrderItemCollectionType.php | 1 + src/Form/Type/OrderItemType.php | 24 +++++++++++++---------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Form/Extension/OrderTypeExtension.php b/src/Form/Extension/OrderTypeExtension.php index 8ead7da..ecd22af 100644 --- a/src/Form/Extension/OrderTypeExtension.php +++ b/src/Form/Extension/OrderTypeExtension.php @@ -17,15 +17,17 @@ final class OrderTypeExtension extends AbstractTypeExtension { public function buildForm(FormBuilderInterface $builder, array $options): void { - $builder - ->add('items', OrderItemCollectionType::class) - ; - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { $form = $event->getForm(); /** @var OrderInterface $order */ $order = $event->getData(); + $form + ->add('items', OrderItemCollectionType::class, [ + 'entry_options' => ['currency_code' => $order->getCurrencyCode()] + ]) + ; + $form->add('discounts', OrderDiscountCollectionType::class, [ 'property_path' => 'adjustments', 'entry_options' => [ diff --git a/src/Form/Type/OrderItemCollectionType.php b/src/Form/Type/OrderItemCollectionType.php index 9280f8a..90d1f6d 100644 --- a/src/Form/Type/OrderItemCollectionType.php +++ b/src/Form/Type/OrderItemCollectionType.php @@ -17,6 +17,7 @@ public function configureOptions(OptionsResolver $resolver): void 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, + 'prototype' => true, ]); } diff --git a/src/Form/Type/OrderItemType.php b/src/Form/Type/OrderItemType.php index 976580e..11b0d72 100644 --- a/src/Form/Type/OrderItemType.php +++ b/src/Form/Type/OrderItemType.php @@ -14,6 +14,9 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\OptionsResolver; final class OrderItemType extends AbstractResourceType { @@ -44,21 +47,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]) ; - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { - $form = $event->getForm(); - /** @var OrderItemInterface|null $orderItem */ - $orderItem = $event->getData(); - if ($orderItem === null) { - return; - } + $currencyCode = $options['currency_code']; - /** @var OrderInterface $order */ - $order = $orderItem->getOrder(); + $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($currencyCode): void { + $form = $event->getForm(); $form->add('discounts', OrderItemDiscountCollectionType::class, [ 'property_path' => 'adjustments', 'entry_options' => [ - 'currency' => $order->getCurrencyCode(), + 'currency' => $currencyCode, ], ]); }); @@ -72,4 +69,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $event->setData($orderItem); }); } + + public function configureOptions(OptionsResolver $resolver): void + { + parent::configureOptions($resolver); + + $resolver->setRequired('currency_code'); + } } From 53afe17a4896eb7eb666f59f084496eee3a0706c Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Mon, 24 Jun 2024 14:33:55 +0200 Subject: [PATCH 2/3] Do not display discounts on show if they do not exist --- .../SyliusAdminBundle/Order/Show/Summary/_item.html.twig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Application/templates/bundles/SyliusAdminBundle/Order/Show/Summary/_item.html.twig b/tests/Application/templates/bundles/SyliusAdminBundle/Order/Show/Summary/_item.html.twig index 89b0cb7..daf5aa7 100644 --- a/tests/Application/templates/bundles/SyliusAdminBundle/Order/Show/Summary/_item.html.twig +++ b/tests/Application/templates/bundles/SyliusAdminBundle/Order/Show/Summary/_item.html.twig @@ -49,11 +49,14 @@ {{ money.format(item.total, order.currencyCode) }} +{% set discounts = item.getAdjustments(adminOrderItemDiscountAdjustment) %} +{% if discounts is not empty %} {{ 'setono_sylius_order_edit.ui.discounts'|trans }}: - {% for discount in item.getAdjustments(adminOrderItemDiscountAdjustment) %} + {% for discount in discounts %} {{ money.format(discount.amount, order.currencyCode) }}{% if not loop.last %}, {% endif %} {% endfor %} +{% endif %} From f6a38739f477987bf54d2dbf6801759d58f07f48 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Mon, 24 Jun 2024 14:35:41 +0200 Subject: [PATCH 3/3] CS fixes --- src/Form/Extension/OrderTypeExtension.php | 2 +- src/Form/Type/OrderItemType.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Form/Extension/OrderTypeExtension.php b/src/Form/Extension/OrderTypeExtension.php index ecd22af..a38c9a5 100644 --- a/src/Form/Extension/OrderTypeExtension.php +++ b/src/Form/Extension/OrderTypeExtension.php @@ -24,7 +24,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $form ->add('items', OrderItemCollectionType::class, [ - 'entry_options' => ['currency_code' => $order->getCurrencyCode()] + 'entry_options' => ['currency_code' => $order->getCurrencyCode()], ]) ; diff --git a/src/Form/Type/OrderItemType.php b/src/Form/Type/OrderItemType.php index 11b0d72..6b09406 100644 --- a/src/Form/Type/OrderItemType.php +++ b/src/Form/Type/OrderItemType.php @@ -5,7 +5,6 @@ namespace Setono\SyliusOrderEditPlugin\Form\Type; use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; -use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\OrderItemInterface; use Sylius\Component\Core\Model\ProductVariant; use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; @@ -14,8 +13,6 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; final class OrderItemType extends AbstractResourceType @@ -47,6 +44,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]) ; + /** @var string $currencyCode */ $currencyCode = $options['currency_code']; $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($currencyCode): void { @@ -75,5 +73,6 @@ public function configureOptions(OptionsResolver $resolver): void parent::configureOptions($resolver); $resolver->setRequired('currency_code'); + $resolver->setAllowedTypes('currency_code', 'string'); } }