Skip to content

Commit

Permalink
Variant autocomplete + remove item bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 27, 2024
1 parent 9e4e68f commit 1a3d954
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/Form/Type/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
])
;

Expand Down
6 changes: 5 additions & 1 deletion src/Resources/public/js/order-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion src/Resources/views/admin/order/update/theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
{% block _sylius_order_items_entry_widget %}
<tr class="item">
<td>{{ form_widget(form.quantity) }}</td>
<td>{{ form_widget(form.variant) }}</td>
<td>
{{ 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')}
) }}
</td>
<td colspan="4" class="right aligned">
<button class="ui red labeled icon button delete-order-item" type="button">
<i class="icon trash"></i> {{ 'sylius.ui.delete'|trans }}
Expand Down
13 changes: 8 additions & 5 deletions src/Setter/OrderDiscountAdjustmentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ public function __construct(

public function set(OrderInterface $order, int $discount): void
{
$items = $order->getItems();
/** @var array<int, OrderItemInterface> $items */
$items = $order->getItems()->getValues();
$orderId = (int) $order->getId();

$distributedPrices = $this->integerDistributor->distribute($discount, $items->count());
$distributedPrices = $this->integerDistributor->distribute($discount, count($items));

/** @var int $distribution */
/**
* @var int $i
* @var int $distribution
*/
foreach ($distributedPrices as $i => $distribution) {
/** @var OrderItemInterface $item */
$item = $items->get($i);
$item = $items[$i];
$this->orderItemDiscountAdjustmentAdder->add(
$item,
AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT,
Expand Down

0 comments on commit 1a3d954

Please sign in to comment.