The callout plugin for Sylius allows you to configure nice callouts/badges for sets of products based on specific rules. It provides a common set of rules by default and is very flexible when it comes to adding new ones.
$ composer require setono/sylius-callout-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\SyliusCalloutPlugin\SetonoSyliusCalloutPlugin::class => ['all' => true],
Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];
# config/packages/setono_sylius_callout.yaml
imports:
- { resource: "@SetonoSyliusCalloutPlugin/Resources/config/app/config.yaml" }
# config/routes/setono_sylius_callout.yaml
setono_sylius_callout:
resource: "@SetonoSyliusCalloutPlugin/Resources/config/routes.yaml"
Add the Setono\SyliusCalloutPlugin\Model\ProductTrait
trait to your App\Entity\Product\Product
class.
<?php
// src/Entity/Product/Product.php
namespace App\Entity;
use Setono\SyliusCalloutPlugin\Model\ProductTrait as CalloutProductTrait;
use Setono\SyliusCalloutPlugin\Model\ProductInterface as CalloutProductInterface;
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 CalloutProductInterface
{
use CalloutProductTrait;
}
$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate
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' : get_callouts(product, 'default') } %}
.
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
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 assign all 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.
$ bin/console assets:install
From now on you should be able to add new callouts in the admin panel. Once you add one, you just need to configure.
- 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\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>