Plugin for Sylius to define permanent or time-limited promotions for products and automatically update prices.
composer require setono/sylius-catalog-promotion-pluginNOTICE that this plugin uses the twig/string-extra and twig/extra-bundle internally to do string manipulation in Twig.
It should work out of the box with the Symfony Flex recipe, but if you're not using Symfony Flex, you should install the bundle manually.
<?php
# config/bundles.php
return [
// ...
Setono\SyliusCatalogPromotionPlugin\SetonoSyliusCatalogPromotionPlugin::class => ['all' => true],
Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
// ...
];Note, that we MUST define SetonoSyliusCatalogPromotionPlugin BEFORE SyliusGridBundle.
Otherwise, you'll see exception like this:
You have requested a non-existent parameter "setono_sylius_catalog_promotion.model.catalog_promotion.class". # config/routes/setono_sylius_catalog_promotion.yaml
setono_sylius_catalog_promotion:
resource: "@SetonoSyliusCatalogPromotionPlugin/Resources/config/routes.yaml"TODO: Extend Product class
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migratebin/console sylius:install:assets# Will process _all_ catalog promotions for _all_ products
# You can run this once a day as a fallback for events triggering the update process
php bin/console setono:sylius-catalog-promotion:process
# Will prune/remove catalog promotion updates older then the given threshold
php bin/console setono:sylius-catalog-promotion:prune-catalog-promotion-updatesMost likely you need to apply catalog promotions outside a request/response lifecycle at some point. A good example could be the generation of product feeds. To do that you need to set the channel context to the respective channel you are processing.
You do this using the \Setono\SyliusCatalogPromotionPlugin\Context\StaticChannelContext:
<?php
use Setono\SyliusCatalogPromotionPlugin\Context\StaticChannelContext;
use Sylius\Component\Channel\Model\ChannelInterface;
class YourFeedProcessor
{
public function __construct(private readonly StaticChannelContext $staticChannelContext) {
}
public function process(): void
{
/**
* A list of channels you need to process
*
* @var list<ChannelInterface> $channels
*/
$channels = [];
foreach ($channels as $channel) {
$this->staticChannelContext->setChannel($channel);
// do your processing...
}
}
}If you want to check if a product is on sale, e.g. if you want to have a Sale category on your store, we have included
a \Setono\SyliusCatalogPromotionPlugin\Checker\OnSale\OnSaleCheckerInterface service that checks exactly that:
<?php
use Setono\SyliusCatalogPromotionPlugin\Checker\OnSale\OnSaleCheckerInterface;
use Setono\SyliusCatalogPromotionPlugin\Model\ProductInterface;
class YourFeedProcessor
{
public function __construct(private readonly OnSaleCheckerInterface $onSaleChecker) {
}
public function process(): void
{
/**
* A list of products you are processing
*
* @var list<ProductInterface> $products
*/
$products = [];
foreach ($products as $product) {
if($this->onSaleChecker->onSale($product)) {
// the product is on sale
}
}
}
}