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

Allow Cart Quantity Selection in Multiples of 10 #1096

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions assets/shop/entrypoint.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
// In this file you can import assets like images or stylesheets
console.log('Hello Webpack Encore! Edit me in assets/shop/entrypoint.js');

var quantityInput = document.querySelector(
'.qnty-alert input, input[name="sylius_shop_add_to_cart[cartItem][quantity]"]'
);

quantityInput.addEventListener('change', function() {
var newValue = quantityInput.value;
if(newValue == 70){
alert('Great Choice!');
}
});
98 changes: 98 additions & 0 deletions src/Form/Extension/CartItemTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\CoreBundle\Form\Extension;

use Sylius\Bundle\OrderBundle\Form\Type\CartItemType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantChoiceType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantMatchType;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\ProductInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\Range;

/**
* We extend the item form type a bit, to add a variant select field
* when we're adding product to cart, but not when we edit quantity in cart.
* We'll use simple option for that, passing the product instance required by
* variant choice type.
*/
final class CartItemTypeExtension extends AbstractTypeExtension
{
public function __construct(private readonly int $orderItemQuantityModifierLimit)
{
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$config = [
'attr' => ['min' => 10,'step' => 10],
'label' => 'sylius.ui.quantity',
'constraints' => [
new Range([
'min' => 10,
'max' => $this->orderItemQuantityModifierLimit,
'notInRangeMessage' => 'sylius.cart_item.quantity.not_in_range',
'groups' => 'sylius',
]),
new Expression([
'expression' => 'value % 10 == 0',
'message' => 'sylius.cart_item.quantity.multiple_of_ten',
'groups' => 'sylius',
]),
],
];

if($builder->getName() == 'cartItem'){
$config['data'] = 10;
}

$builder->add('quantity', IntegerType::class, $config);

if (isset($options['product']) && $options['product']->hasVariants() && !$options['product']->isSimple()) {
$type =
Product::VARIANT_SELECTION_CHOICE === $options['product']->getVariantSelectionMethod()
? ProductVariantChoiceType::class
: ProductVariantMatchType::class
;

$builder->add('variant', $type, [
'product' => $options['product'],
]);
}
}

/**
* We need to override this method to allow setting 'product'
* option, by default it will be null so we don't get the variant choice
* when creating full cart form.
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefined([
'product',
])
->setAllowedTypes('product', ProductInterface::class)
;
}

public static function getExtendedTypes(): iterable
{
return [CartItemType::class];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{# Rendered with \Sylius\Bundle\ShopBundle\Twig\Component\Cart\FormComponent #}

{% import '@SyliusShop/shared/messages.html.twig' as messages %}

{% form_theme form '@SyliusShop/form/theme.html.twig' %}

<div {{ attributes }} >
{% if resource.empty %}
{{ messages.info('sylius.ui.your_cart_is_empty') }}
{% else %}
{{ form_start(form, {'action': path('sylius_shop_cart_checkout'), 'attr': {'id': form.vars.id}}) }}
<input type="hidden" name="_method" value="PATCH"/>
{{ form_errors(form) }}
{{ form_widget(form._token) }}

{% hook 'form' with { form } %}

{{ form_end(form, {render_rest: hookable.configuration.render_rest|default(false)}) }}
{% endif %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set form = hookable_metadata.context.form %}

<td>
{{ form_row(form.quantity, sylius_test_form_attribute('cart-item-quantity')|sylius_merge_recursive({label: false, row_attr: {'class': 'mt-3 qnty-alert'}})) }}
</td>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="mb-4">
{{ form_row(hookable_metadata.context.form.cartItem.quantity, sylius_test_form_attribute('quantity')) }}
</div>
4 changes: 4 additions & 0 deletions translations/validators.cn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "数量必须是10的倍数。"
4 changes: 4 additions & 0 deletions translations/validators.de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "Die Menge muss ein Vielfaches von 10 sein."
4 changes: 4 additions & 0 deletions translations/validators.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: Quantity must be a multiple of 10.
4 changes: 4 additions & 0 deletions translations/validators.es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "La cantidad debe ser un múltiplo de 10."
4 changes: 4 additions & 0 deletions translations/validators.fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "La quantité doit être un multiple de 10."
4 changes: 4 additions & 0 deletions translations/validators.pl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "Ilość musi być wielokrotnością 10."
4 changes: 4 additions & 0 deletions translations/validators.pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
cart_item:
quantity:
multiple_of_ten: "A quantidade deve ser um múltiplo de 10."