-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate samples when the order is updated through the cart
- Loading branch information
Showing
6 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Form\Extension; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ChannelInterface; | ||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use Sylius\Bundle\OrderBundle\Form\Type\CartType; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Order\Model\OrderInterface; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormError; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CartTypeExtension extends AbstractTypeExtension | ||
{ | ||
public function __construct( | ||
private ChannelContextInterface $channelContext, | ||
private TranslatorInterface $translator, | ||
) { | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
// This listener must run after the validation listener | ||
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void { | ||
/** @var OrderInterface $order */ | ||
$order = $event->getData(); | ||
|
||
/** @var ChannelInterface $channel */ | ||
$channel = $this->channelContext->getChannel(); | ||
|
||
Assert::isInstanceOf($channel, ChannelInterface::class); | ||
|
||
if (null === $channel->getMaxSamplesPerOrder() || 0 === $channel->getMaxSamplesPerOrder()) { | ||
return; | ||
} | ||
|
||
$samples = 0; | ||
|
||
/** @var OrderItemInterface $item */ | ||
foreach ($order->getItems() as $item) { | ||
Assert::isInstanceOf($item->getVariant(), ProductVariantInterface::class); | ||
|
||
if (null === $item->getVariant()->getSampleOf()) { | ||
continue; | ||
} | ||
|
||
$samples += $item->getQuantity(); | ||
} | ||
|
||
if ($samples > $channel->getMaxSamplesPerOrder()) { | ||
$event->getForm()->addError(new FormError( | ||
$this->translator->trans('babdev_sylius_product_samples.order.allowed_samples.plural_max_per_order', ['%count%' => $channel->getMaxSamplesPerOrder()], 'validators'), | ||
null, | ||
['{{ limit }}' => $channel->getMaxSamplesPerOrder()], | ||
$channel->getMaxSamplesPerOrder(), | ||
)); | ||
} | ||
}, -10); | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
return [CartType::class]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters