Sylius Promotion Component, o cómo inspirarse en un proyecto Open Source

Poniendo en valor las experiencias pasadas

Sylius Promotion Component

  • Promociones acotadas en el tiempo
  • Descuentos fijos y porcentuales
  • Soporte para definir y extender acciones y reglas personalizadas
  • Sistema de cupones
Interfaz que nos ofrece el Backoffice de Sylius para gestionar las promociones.

Show me the code

/** @var PromotionInterface $promotion */
$promotion = $this->container->get('sylius.factory.promotion')->createNew();

$promotion->setCode('discount_10%');
$promotion->setName('10% discount');

/** @var PromotionActionFactoryInterface $actionFactory */
$actionFactory = $this->container->get('sylius.factory.promotion_action');

$action = $actionFactory->createPercentageDiscount(10);

$promotion->addAction($action);

$this->container->get('sylius.repository.promotion')->add($promotion);

// and now get the PromotionApplicator and use it on an Order (assuming that you have one)
$this->container->get('sylius.promotion_applicator')->apply($order, $promotion);

Punto de entrada

  • PreQualifiedPromotionsProviderInterface, se encarga de proveer promociones candidatas a ser aplicables
  • PromotionEligibilityChecker, se ocupa de evaluar la satisfacibilidad del subject con el conjunto de reglas de la promoción
  • PromotionApplicatorInterface, se responsabiliza de poder aplicar y revertir promociones
<?php/** This file is part of the Sylius package.** (c) Paweł Jędrzejewski** 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\Component\Promotion\Processor;use Sylius\Component\Promotion\Action\PromotionApplicatorInterface;use Sylius\Component\Promotion\Checker\Eligibility\PromotionEligibilityCheckerInterface;use Sylius\Component\Promotion\Model\PromotionSubjectInterface;use Sylius\Component\Promotion\Provider\PreQualifiedPromotionsProviderInterface;final class PromotionProcessor implements PromotionProcessorInterface{    /** @var PreQualifiedPromotionsProviderInterface */    private $preQualifiedPromotionsProvider;    /** @var PromotionEligibilityCheckerInterface */    private $promotionEligibilityChecker;    /** @var PromotionApplicatorInterface */    private $promotionApplicator;    public function __construct(        PreQualifiedPromotionsProviderInterface       $preQualifiedPromotionsProvider,        PromotionEligibilityCheckerInterface $promotionEligibilityChecker,       PromotionApplicatorInterface $promotionApplicator) {     $this->preQualifiedPromotionsProvider = $preQualifiedPromotionsProvider;     $this->promotionEligibilityChecker = $promotionEligibilityChecker;     $this->promotionApplicator = $promotionApplicator;}public function process(PromotionSubjectInterface $subject): void{    foreach ($subject->getPromotions() as $promotion) {        $this->promotionApplicator->revert($subject, $promotion);    }    $preQualifiedPromotions = $this->preQualifiedPromotionsProvider->getPromotions($subject);    foreach ($preQualifiedPromotions as $promotion) {    if ($promotion->isExclusive() && $this->promotionEligibilityChecker->isEligible($subject, $promotion)) {        $this->promotionApplicator->apply($subject, $promotion);        return;     }}    foreach ($preQualifiedPromotions as $promotion) {         if (!$promotion->isExclusive() && $this-    >promotionEligibilityChecker->isEligible($subject, $promotion)) {             $this->promotionApplicator->apply($subject, $promotion);        }    }  }}

Acciones y reglas

final class ItemTotalRuleChecker implements RuleCheckerInterface{public const TYPE = 'item_total';public function isEligible(PromotionSubjectInterface $subject, array $configuration): bool{    return $subject->getPromotionSubjectTotal() >= $configuration['amount'];}}
use Sylius\Component\Promotion\Model\PromotionInterface;use Sylius\Component\Promotion\Model\PromotionSubjectInterface;interface PromotionActionCommandInterface{    public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion): bool;    public function revert(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion): void;}

Conclusión

Documentación

--

--

Full Stack Web Developer — adrianalonso.es

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store