setono / sylius-catalog-promotion-plugin
Catalog promotion plugin for Sylius
Installs: 61 064
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 3
Forks: 8
Open Issues: 6
Type:sylius-plugin
Requires
- php: >=7.4
- beberlei/doctrineextensions: ^1.2
- doctrine/collections: ^1.6
- doctrine/dbal: ^2.13
- doctrine/event-manager: ^1.1
- doctrine/orm: ^2.7
- knplabs/knp-menu: ^3.2
- setono/job-status-bundle: ^0.2.3
- sylius/registry: ^1.6
- sylius/resource-bundle: ^1.6
- symfony/config: ^5.4 || ^6.0
- symfony/console: ^5.4 || ^6.0
- symfony/dependency-injection: ^5.4 || ^6.0
- symfony/form: ^5.4 || ^6.0
- symfony/lock: ^5.4 || ^6.0
- symfony/options-resolver: ^5.4 || ^6.0
- symfony/validator: ^5.4 || ^6.0
- webmozart/assert: ^1.10
Requires (Dev)
- fzaninotto/faker: ^1.9
- phpspec/phpspec: ^7.1
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.16.1
- psalm/plugin-symfony: ^3.0
- setono/code-quality-pack: ^2.1.2
- setono/sylius-behat-pack: ^0.2.1
- sylius/sylius: ~1.7.0
- symfony/debug-bundle: ^5.4 || ^6.0
- symfony/dotenv: ^5.4 || ^6.0
- symfony/intl: ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^5.4 || ^6.0
- weirdan/doctrine-psalm-plugin: ^1.0
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
Register plugin
<?php # config/bundles.php return [ // ... Setono\JobStatusBundle\SetonoJobStatusBundle::class => ['all' => true], 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.promotion.class".
Add config
# config/packages/setono_sylius_catalog_promotion.yaml imports: - { resource: "@SetonoSyliusCatalogPromotionPlugin/Resources/config/app/config.yaml" } # Uncomment if you want to add some catalog promotion fixtures to default suite # - { resource: "@SetonoSyliusCatalogPromotionPlugin/Resources/config/app/fixtures.yaml" }
Add routing
# config/routes/setono_sylius_catalog_promotion.yaml setono_sylius_catalog_promotion_admin: resource: "@SetonoSyliusCatalogPromotionPlugin/Resources/config/admin_routing.yaml" prefix: /admin
Extend core classes
Extend ChannelPricing
<?php declare(strict_types=1); namespace App\Entity\Channel; use Doctrine\ORM\Mapping as ORM; use Setono\SyliusCatalogPromotionPlugin\Model\ChannelPricingInterface as CatalogPromotionChannelPricingInterface; use Setono\SyliusCatalogPromotionPlugin\Model\ChannelPricingTrait as CatalogPromotionChannelPricingTrait; use Sylius\Component\Core\Model\ChannelPricing as BaseChannelPricing; /** * @ORM\Table(name="sylius_channel_pricing") * @ORM\Entity() */ class ChannelPricing extends BaseChannelPricing implements CatalogPromotionChannelPricingInterface { use CatalogPromotionChannelPricingTrait; }
Extend ChannelPricingRepository
<?php declare(strict_types=1); namespace App\Repository; use Setono\SyliusCatalogPromotionPlugin\Doctrine\ORM\ChannelPricingRepositoryTrait as CatalogPromotionChannelPricingRepositoryTrait; use Setono\SyliusCatalogPromotionPlugin\Repository\ChannelPricingRepositoryInterface as CatalogPromotionChannelPricingRepositoryInterface; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; class ChannelPricingRepository extends EntityRepository implements CatalogPromotionChannelPricingRepositoryInterface { use CatalogPromotionChannelPricingRepositoryTrait; }
Extend ProductRepository
<?php declare(strict_types=1); namespace App\Repository; use Setono\SyliusCatalogPromotionPlugin\Doctrine\ORM\ProductRepositoryTrait as CatalogPromotionProductRepositoryTrait; use Setono\SyliusCatalogPromotionPlugin\Repository\ProductRepositoryInterface as CatalogPromotionProductRepositoryInterface; use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository; class ProductRepository extends BaseProductRepository implements CatalogPromotionProductRepositoryInterface { use CatalogPromotionProductRepositoryTrait; }
Extend ProductVariantRepository
<?php declare(strict_types=1); namespace App\Repository; use Setono\SyliusCatalogPromotionPlugin\Doctrine\ORM\ProductVariantRepositoryTrait as CatalogPromotionProductVariantRepositoryTrait; use Setono\SyliusCatalogPromotionPlugin\Repository\ProductVariantRepositoryInterface as CatalogPromotionProductVariantRepositoryInterface; use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductVariantRepository as BaseProductVariantRepository; class ProductVariantRepository extends BaseProductVariantRepository implements CatalogPromotionProductVariantRepositoryInterface { use CatalogPromotionProductVariantRepositoryTrait; }
Update config with extended classes
In your config/packages/_sylius.yaml
file update the configured classes:
# config/packages/_sylius.yaml sylius_core: resources: channel_pricing: classes: model: App\Entity\ChannelPricing repository: App\Repository\ChannelPricingRepository sylius_product: resources: product: classes: repository: App\Repository\ProductRepository product_variant: classes: repository: App\Repository\ProductVariantRepository
Update your schema
Create a migration file:
$ php bin/console doctrine:migrations:diff
If you have existing discounted products you should append this line to the up
method in the migration file:
<?php namespace DoctrineMigrations; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; final class Version20191028134956 extends AbstractMigration { public function up(Schema $schema) : void { // The generated SQL will be here // ... // append this line $this->addSql('UPDATE sylius_channel_pricing SET manually_discounted = 1 WHERE original_price IS NOT NULL AND price != original_price'); } public function down(Schema $schema) : void { // ... } }
Execute migration file:
$ php bin/console doctrine:migrations:migrate
Install assets
bin/console sylius:install:assets
Configure CRON to run next command every minute
$ php bin/console setono:sylius-catalog-promotion:process