setono / sylius-callout-plugin
Add callouts to your Sylius products
Installs: 39 659
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 10
Open Issues: 16
Type:sylius-plugin
Requires
- php: >=7.4
- ext-mbstring: *
- doctrine/collections: ^1.6
- doctrine/event-manager: ^1.1
- doctrine/orm: ^2.7
- doctrine/persistence: ^1.3 || ^2.2
- setono/doctrine-orm-batcher: ^0.6
- setono/doctrine-orm-batcher-bundle: ^0.3.1
- sylius/resource-bundle: ^1.6
- symfony/config: ^4.4 || ^5.0
- symfony/console: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/form: ^4.4 || ^5.0
- symfony/http-foundation: ^4.4 || ^5.0.7
- symfony/messenger: ^4.4 || ^5.0
- symfony/options-resolver: ^4.4 || ^5.0
- symfony/routing: ^4.4 || ^5.0
- symfony/translation-contracts: ^1.1 || ^2.4
- symfony/validator: ^4.4 || ^5.0
- thecodingmachine/safe: ^1.0
- twig/twig: ^2.14
- webmozart/assert: ^1.10
Requires (Dev)
- fakerphp/faker: ^1.16
- friendsofsymfony/oauth-server-bundle: >2.0.0-alpha.0 ^2.0@dev
- phpspec/phpspec: ^6.2
- phpunit/phpunit: ^9.3
- psalm/plugin-symfony: ^3.0
- roave/security-advisories: dev-latest
- setono/code-quality-pack: ^2.1.2
- setono/sylius-behat-pack: ^0.1
- sylius/sylius: ~1.9.0
- symfony/asset: ~5.2.0
- symfony/debug-bundle: ^5.1
- symfony/dotenv: ^5.1
- symfony/framework-bundle: ~5.2.0
- symfony/intl: ^4.4 || ^5.0
- symfony/twig-bundle: ~5.2.0
- symfony/web-profiler-bundle: ^5.0
- symfony/web-server-bundle: ^4.4 || ^5.0
- dev-master
- v0.4.0
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.0
- dev-test-orm-batcher
- dev-fix-controller-dependency
- dev-dependabot/composer/lexik/jwt-authentication-bundle-tw-2.18
- dev-dependabot/composer/phpunit/phpunit-tw-9.6
- dev-dependabot/composer/setono/code-quality-pack-tw-2.4
- dev-dependabot/composer/doctrine/orm-tw-2.14
- dev-dependabot/composer/fakerphp/faker-tw-1.21
- dev-dependabot/composer/phpspec/phpspec-tw-7.3
- dev-dependabot/composer/psalm/plugin-phpunit-tw-0.18
- dev-dependabot/composer/psalm/plugin-symfony-tw-4.0
- dev-dependabot/composer/doctrine/event-manager-tw-1.2
- dev-dependabot/composer/doctrine/collections-tw-1.8
- dev-49-fix-callout-eligibility-resolution
This package is auto-updated.
Last update: 2023-05-07 07:09:07 UTC
README
The callout plugin for Sylius allows you to configure nice badges for different set of products based on specific rules. It provides a common set of configuration by default and is very flexible when it comes to adding new ones.
Supports Doctrine ORM driver only.
Screenshots
Shop:
Admin:
Installation
Step 1: Install dependencies
This plugin depends upon the Doctrine ORM Batcher bundle. Install that bundle first.
Step 2: Download the plugin
Open a command console, enter your project directory and execute the following command to download the latest stable version of this plugin:
$ composer require setono/sylius-callout-plugin
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 3: Enable the plugin
Then, enable the plugin by adding it to the list of registered plugins/bundles
in config/bundles.php
file of your project before (!) SyliusGridBundle
:
<?php $bundles = [ Setono\DoctrineORMBatcherBundle\SetonoDoctrineORMBatcherBundle::class => ['all' => true], Setono\SyliusCalloutPlugin\SetonoSyliusCalloutPlugin::class => ['all' => true], Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true], ];
Don't forget to add SetonoDoctrineORMBatcherBundle
as this plugin require bundle to be enabled.
Step 4: Configure plugin
# config/packages/setono_product_callout.yaml imports: - { resource: "@SetonoSyliusCalloutPlugin/Resources/config/app/config.yaml" } # If you want to see Callout column at admin products list - uncomment next line # - { resource: "@SetonoSyliusCalloutPlugin/Resources/config/grids/sylius_admin_product.yaml" } setono_sylius_callout: manual_triggering: false # Enable manual triggering if your store have too much products # That way you can trigger callouts assign process manually when # finish adding all rules # manual_triggering: true no_rules_eligible: false # Set this option to true if you want no rules to be # treated as eligible (e.g. callout without rules will # be applied to all products) # no_rules_eligible: true
Step 5: Import routing
# config/routes/setono_product_callout.yaml setono_product_callout: resource: "@SetonoSyliusCalloutPlugin/Resources/config/routing.yaml"
Step 6: Customize models
Read more about Sylius models customization here.
Customize your Product model
Add a Setono\SyliusCalloutPlugin\Model\CalloutsAwareTrait
trait to your App\Entity\Product
class.
-
If you use
annotations
mapping:<?php // src/Entity/Product.php namespace App\Entity; use Setono\SyliusCalloutPlugin\Model\CalloutsAwareTrait as SetonoSyliusCalloutCalloutsAwareTrait; use Setono\SyliusCalloutPlugin\Model\ProductInterface as SetonoSyliusCalloutProductInterface; use Sylius\Component\Core\Model\Product as BaseProduct; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="sylius_product") */ class Product extends BaseProduct implements SetonoSyliusCalloutProductInterface { use SetonoSyliusCalloutCalloutsAwareTrait { SetonoSyliusCalloutCalloutsAwareTrait::__construct as private __calloutsTraitConstruct; } public function __construct() { $this->__calloutsTraitConstruct(); parent::__construct(); } }
-
If you use
xml
mapping:<?php // src/Model/Product.php namespace App\Model; use Setono\SyliusCalloutPlugin\Model\CalloutsAwareTrait as SetonoSyliusCalloutCalloutsAwareTrait; use Setono\SyliusCalloutPlugin\Model\ProductInterface as SetonoSyliusCalloutProductInterface; use Sylius\Component\Core\Model\Product as BaseProduct; class Product extends BaseProduct implements SetonoSyliusCalloutProductInterface { use SetonoSyliusCalloutCalloutsAwareTrait { SetonoSyliusCalloutCalloutsAwareTrait::__construct as private __calloutsTraitConstruct; } public function __construct() { $this->__calloutsTraitConstruct(); parent::__construct(); } }
<?xml version="1.0" encoding="UTF-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="App\Model\Product" table="sylius_product"> <many-to-many field="callouts" target-entity="Setono\SyliusCalloutPlugin\Model\CalloutInterface"> <join-table name="setono_sylius_callout__product_callouts"> <join-columns> <join-column name="product_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" /> </join-columns> <inverse-join-columns> <join-column name="callout_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" /> </inverse-join-columns> </join-table> </many-to-many> </entity> </doctrine-mapping>
If you haven't done so already, configure the sylius_product
resource to point to your App\Entity\Product
like we
did in an example here.
Step 7: Update your database schema
$ php bin/console doctrine:migrations:diff $ php bin/console doctrine:migrations:migrate
Step 8: Add callouts to your product templates
Add callouts to your product box template. By default, you should use templates/bundles/SyliusShopBundle/Product/_box.html.twig
path. Check out our _box.html.twig file for a reference.
Note the line: {% include "@SetonoSyliusCalloutPlugin/Shop/Product/Callout/_callouts.html.twig" with {'callouts' : product.callouts|setono_callouts} %}
.
Step 9: Using asynchronous transport (optional, but recommended)
All commands in this plugin will extend the CommandInterface. Therefore you can route all commands easily by adding this to your Messenger config:
# config/packages/messenger.yaml framework: messenger: routing: # Route all command messages to the async transport # This presumes that you have already set up an 'async' transport 'Setono\SyliusCalloutPlugin\Message\Command\CommandInterface': async
Step 10: Configure cron job
For the performance reasons, configure a cron job on your production server to execute $ bin/console setono:sylius-callout:assign
command
once in a while in order to rebuild the index for callouts. In most cases it should be done by the resource event listener
triggered anytime you create/update a product or callout, but it is worth to have it covered if something goes wrong.
Example cron configuration (EDITOR=nano sudo crontab -e
) to run command once a day:
0 2 * * * www-data /var/www/html/bin/console setono:sylius-callout:assign --env=prod
Step 11: Install assets
$ bin/console assets:install
Usage
From now on you should be able to add new callouts in the admin panel. Once you add one, you just need to configure.
Customization
Adding a new rule form
- Configure a new form under
App\Form\Type\Rule
namespace, - Add a rule checker under
App\Checker\Rule
namespace and make sure it implementsSetono\SyliusCalloutPlugin\Callout\Checker\Rule\ProductCalloutRuleCheckerInterface
interface and has apublic const TYPE
set corresponding to the below service configuration - Register and tag new services:
<!-- services.xml --> <services> ... <service id="app.callout_rule_checker.is_on_sale" class="Setono\SyliusCalloutPlugin\Callout\Checker\Rule\IsOnSaleRuleChecker"> <argument type="service" id="setono_sylius_callout.checker.product_promotion" /> <tag name="setono_sylius_callout.callout_rule_checker" type="is_on_sale" label="setono_sylius_callout.ui.is_on_sale" form-type="Setono\SyliusCalloutPlugin\Form\Type\Rule\IsOnSaleConfigurationType" /> </service> <service id="app.form.type.rule.is_on_sale" class="Setono\SyliusCalloutPlugin\Form\Type\Rule\IsOnSaleConfigurationType"> <tag name="form.type" /> </service> </services>
Troubleshooting
-
The service "setono_sylius_callout.command_bus.middleware.handle_message" has a dependency on a non-existent service "setono_doctrine_orm_batcher.factory.batcher".
You forgot to add
SetonoDoctrineORMBatcherBundle
to your app'sbundles.php