From 1a3d9543dd89dae06f1edde26aa5b4770350b123 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Thu, 27 Jun 2024 12:50:42 +0200 Subject: [PATCH] Variant autocomplete + remove item bug fix --- src/Form/Type/OrderItemType.php | 14 ++++++++------ src/Resources/public/js/order-edit.js | 6 +++++- .../views/admin/order/update/theme.html.twig | 9 ++++++++- src/Setter/OrderDiscountAdjustmentSetter.php | 13 ++++++++----- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Form/Type/OrderItemType.php b/src/Form/Type/OrderItemType.php index e251d2d..a935463 100644 --- a/src/Form/Type/OrderItemType.php +++ b/src/Form/Type/OrderItemType.php @@ -5,10 +5,9 @@ namespace Setono\SyliusOrderEditPlugin\Form\Type; use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; +use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType; use Sylius\Component\Core\Model\OrderItemInterface; -use Sylius\Component\Core\Model\ProductVariant; use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; @@ -37,10 +36,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $this->orderItemQuantityModifier->modify($orderItem, $quantity); }, ]) - // TODO: change to autocomplete type for product variant - ->add('variant', EntityType::class, [ - 'class' => ProductVariant::class, - 'choice_label' => 'code', + ->add('variant', ResourceAutocompleteChoiceType::class, [ + 'label' => false, + 'multiple' => false, + 'required' => true, + 'choice_name' => 'descriptor', + 'choice_value' => 'id', + 'resource' => 'sylius.product_variant', ]) ; diff --git a/src/Resources/public/js/order-edit.js b/src/Resources/public/js/order-edit.js index 1898c05..437e4d0 100644 --- a/src/Resources/public/js/order-edit.js +++ b/src/Resources/public/js/order-edit.js @@ -23,7 +23,11 @@ document.querySelector('button.add-order-item').addEventListener('click', (event orderItemTable.dataset.index++; var rows = orderItemTable.querySelectorAll('form[name="sylius_order"] tbody tr'); - var lastItemRowDeleteButton = rows[rows.length - 2].querySelector('button.delete-order-item'); + var lastItemRow = rows[rows.length - 2]; + var lastItemRowDeleteButton = lastItemRow.querySelector('button.delete-order-item'); + + $(lastItemRow).find('.sylius-autocomplete').autoComplete(); + lastItemRowDeleteButton.addEventListener('click', (event) => { var row = event.currentTarget.closest('tr'); row.nextElementSibling.remove(); diff --git a/src/Resources/views/admin/order/update/theme.html.twig b/src/Resources/views/admin/order/update/theme.html.twig index 3ec8e54..b6cb8cb 100644 --- a/src/Resources/views/admin/order/update/theme.html.twig +++ b/src/Resources/views/admin/order/update/theme.html.twig @@ -3,7 +3,14 @@ {% block _sylius_order_items_entry_widget %} {{ form_widget(form.quantity) }} - {{ form_widget(form.variant) }} + + {{ form_row(form.variant, { + 'remote_url': path('sylius_admin_ajax_all_product_variants_by_phrase'), + 'remote_criteria_type': 'contains', + 'remote_criteria_name': 'phrase', + 'load_edit_url': path('sylius_admin_ajax_all_product_variants_by_codes')} + ) }} +