Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variant field autocomplete + remove item bug fix #28

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"sylius/resource-bundle": "^1.6",
"symfony/config": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/doctrine-bridge": "^5.4 || ^6.3 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.4 || ^7.0",
"symfony/form": "^5.4 || ^6.4 || ^7.0",
"symfony/http-foundation": "^5.4 || ^6.4 || ^7.0",
Expand Down
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, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this search in id, code, and name as stated in the issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It searches by descriptor, which is code or name, as far as I know. Is the ID search a hard requirement? 😅 It's of course possible, but it would require from us to create a new repository function + new repository configuration and trait to implement it in the final application + new route. It's just easier this way and (I believe, correct me if I'm wrong) searching by name or code is much more useful than by id 🖖

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Let's just keep it this way for now :D

'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
Loading