Skip to content

Commit

Permalink
Fix adding order item to the order
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 24, 2024
1 parent 4870471 commit 47a3e85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/Form/Extension/OrderTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/OrderItemCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function configureOptions(OptionsResolver $resolver): void
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
]);
}

Expand Down
24 changes: 14 additions & 10 deletions src/Form/Type/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
],
]);
});
Expand All @@ -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');
}
}

0 comments on commit 47a3e85

Please sign in to comment.