Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornobils/0dcfa484345af3ca6b7e582ce79dbda3 to your computer and use it in GitHub Desktop.
Save cornobils/0dcfa484345af3ca6b7e582ce79dbda3 to your computer and use it in GitHub Desktop.
[Sylius] Order total amount promotion rule
<?php
declare(strict_types=1);
namespace AppBundle\Form\Admin;
use Sylius\Bundle\CoreBundle\Form\Type\ChannelCollectionType;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class ChannelBasedMinimumPriceConfigurationType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'entry_type' => PriceConfigurationType::class,
'entry_options' => function (ChannelInterface $channel): array {
return [
'label' => $channel->getName(),
'currency' => $channel->getBaseCurrency()->getCode(),
];
}
]);
}
/**
* {@inheritdoc}
*/
public function getParent(): string
{
return ChannelCollectionType::class;
}
}
<?php
namespace AppBundle\Promotion\Checker\Rule;
use AppBundle\Form\Admin\ChannelBasedMinimumPriceConfigurationType;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
final class CurrentOrderTotalRuleChecker implements RuleCheckerInterface
{
const TYPE = 'current_order_total';
/**
* {@inheritdoc}
*/
public function isEligible(PromotionSubjectInterface $subject, array $configuration): bool
{
if (!$subject instanceof OrderInterface) {
throw new UnsupportedTypeException($subject, OrderInterface::class);
}
if (!isset($configuration['default']['amount']) || !is_int($configuration['default']['amount'])) {
return false;
}
return $subject->getTotal() >= $configuration['default']['amount'];
}
/**
* {@inheritdoc}
*/
public function getConfigurationFormType()
{
return ChannelBasedMinimumPriceConfigurationType::class;
}
}
<?php
declare(strict_types=1);
namespace AppBundle\Form\Admin;
use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Type;
final class PriceConfigurationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('amount', MoneyType::class, [
'constraints' => [
new Type(['type' => 'numeric', 'groups' => ['sylius']]),
],
'currency' => $options['currency']
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setRequired('currency')
->setAllowedTypes('currency', 'string')
;
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix(): string
{
return 'sylius_promotion_rule_price_configuration';
}
}
app.promotion_rule_checker.order_total:
class: AppBundle\Promotion\Checker\Rule\CurrentOrderTotalRuleChecker
tags:
- { name: sylius.promotion_rule_checker, type: current_order_total, label: Current order total, form_type: AppBundle\Form\Admin\ChannelBasedMinimumPriceConfigurationType }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment