Skip to content

Commit

Permalink
Merge pull request #20 from Setono/add-line-error
Browse files Browse the repository at this point in the history
Fix adding order line error
  • Loading branch information
Zales0123 authored Jun 24, 2024
2 parents 7aee0f5 + f6a3873 commit 2cde246
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 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
25 changes: 14 additions & 11 deletions src/Form/Type/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +13,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class OrderItemType extends AbstractResourceType
{
Expand Down Expand Up @@ -44,21 +44,16 @@ 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;
}
/** @var string $currencyCode */
$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,
],
'button_add_label' => 'setono_sylius_order_edit.ui.add_discount',
]);
Expand All @@ -73,4 +68,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$event->setData($orderItem);
});
}

public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

$resolver->setRequired('currency_code');
$resolver->setAllowedTypes('currency_code', 'string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@
{{ money.format(item.total, order.currencyCode) }}
</td>
</tr>
{% set discounts = item.getAdjustments(adminOrderItemDiscountAdjustment) %}
{% if discounts is not empty %}
<tr>
<td colspan="9">
<strong>{{ 'setono_sylius_order_edit.ui.discounts'|trans }}:</strong>
{% for discount in item.getAdjustments(adminOrderItemDiscountAdjustment) %}
{% for discount in discounts %}
{{ money.format(discount.amount, order.currencyCode) }}{% if not loop.last %}, {% endif %}
{% endfor %}
</td>
</tr>
{% endif %}

0 comments on commit 2cde246

Please sign in to comment.