setono / sylius-catalog-promotion-plugin
Catalog promotion plugin for Sylius
Fund package maintenance!
Setono
Installs: 63 574
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 3
Forks: 8
Open Issues: 2
Type:sylius-plugin
Requires
- php: >=8.1
- doctrine/collections: ^1.8
- doctrine/orm: ^2.10
- doctrine/persistence: ^2.5 || ^3.4
- eventsauce/backoff: ^1.2
- knplabs/knp-menu: ^3.0
- ocramius/doctrine-batch-utils: ^2.4
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- setono/composite-compiler-pass: ^1.2
- setono/doctrine-orm-trait: ^1.1
- sylius/channel: ^1.0
- sylius/channel-bundle: ^1.0
- sylius/core: ^1.10.8
- sylius/core-bundle: ^1.0
- sylius/product-bundle: ^1.0
- sylius/promotion-bundle: ^1.0
- sylius/registry: ^1.6
- sylius/resource-bundle: ^1.8
- sylius/taxonomy-bundle: ^1.0
- sylius/ui-bundle: ^1.0
- symfony/config: ^5.4 || ^6.4 || ^7.0
- symfony/console: ^5.4 || ^6.4 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.4 || ^7.0
- symfony/event-dispatcher: ^5.4 || ^6.4 || ^7.0
- symfony/form: ^5.4 || ^6.4 || ^7.0
- symfony/http-foundation: ^5.4 || ^6.4 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.4 || ^7.0
- symfony/messenger: ^5.4 || ^6.4 || ^7.0
- symfony/options-resolver: ^5.4 || ^6.4 || ^7.0
- symfony/routing: ^5.4 || ^6.4 || ^7.0
- symfony/uid: ^5.4 || ^6.4 || ^7.0
- symfony/validator: ^5.4 || ^6.4 || ^7.0
- symfony/workflow: ^5.4 || ^6.4 || ^7.0
- twig/extra-bundle: ^2.12 || ^3.0
- twig/string-extra: ^2.12 || ^3.0
- twig/twig: ^2.12 || ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- api-platform/core: ^2.7.16
- babdev/pagerfanta-bundle: ^3.8
- behat/behat: ^3.14
- doctrine/doctrine-bundle: ^2.11
- infection/infection: ^0.27.11
- jms/serializer-bundle: ^4.2
- lexik/jwt-authentication-bundle: ^2.17
- matthiasnoback/symfony-config-test: ^4.3 || ^5.1
- matthiasnoback/symfony-dependency-injection-test: ^4.3 || ^5.1
- phpspec/prophecy-phpunit: ^2.3
- phpunit/phpunit: ^9.6.20
- psalm/plugin-phpunit: ^0.18.4
- psalm/plugin-symfony: ^5.2
- setono/code-quality-pack: ^2.8.1
- shipmonk/composer-dependency-analyser: ^1.6
- sylius/sylius: ~1.12.19
- symfony/debug-bundle: ^5.4 || ^6.4 || ^7.0
- symfony/dotenv: ^5.4 || ^6.4 || ^7.0
- symfony/intl: ^5.4 || ^6.4 || ^7.0
- symfony/property-info: ^5.4 || ^6.4 || ^7.0
- symfony/serializer: ^5.4 || ^6.4 || ^7.0
- symfony/web-profiler-bundle: ^5.4 || ^6.4 || ^7.0
- symfony/webpack-encore-bundle: ^1.17.2
- willdurand/negotiation: ^3.1
This package is auto-updated.
Last update: 2025-02-11 10:07:30 UTC
README
Plugin for Sylius to define permanent or time-limited promotions for products and automatically update prices.
Install
Add plugin to composer.json
composer require setono/sylius-catalog-promotion-plugin
NOTICE 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.
Register plugin
<?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".
Add routing
# config/routes/setono_sylius_catalog_promotion.yaml setono_sylius_catalog_promotion: resource: "@SetonoSyliusCatalogPromotionPlugin/Resources/config/routes.yaml"
Extend core classes
TODO: Extend Product
class
Create migration
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
Install assets
bin/console sylius:install:assets
Configure cron (optional)
# 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-updates
Apply catalog promotions outside request/response life cycle
Most 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... } } }
Check if a product is on sale
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 } } } }