setono/sylius-callout-plugin

Add callouts to your Sylius products

Installs: 48 792

Dependents: 0

Suggesters: 0

Security: 0

Stars: 9

Watchers: 2

Forks: 13

Open Issues: 4

Type:sylius-plugin

v0.5.0-alpha.3 2024-02-05 12:15 UTC

README

Latest Version Software License Build Status Code Coverage

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.

Screenshots

Shop

Screenshot showing callouts on product list

Admin

Screenshot showing admin callout update form Screenshot showing admin callouts list

Installation

Step 1: Download the plugin

$ composer require setono/sylius-callout-plugin

Step 2: 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\SyliusCalloutPlugin\SetonoSyliusCalloutPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];

Step 3: Configure plugin

# config/packages/setono_sylius_callout.yaml

imports:
    - { resource: "@SetonoSyliusCalloutPlugin/Resources/config/app/config.yaml" }

Step 4: Import routing

# config/routes/setono_sylius_callout.yaml

setono_sylius_callout:
    resource: "@SetonoSyliusCalloutPlugin/Resources/config/routes.yaml"

Step 5: Extend entities

Extend Product

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;
}

Step 6: Update your database schema

$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate

Step 7: 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' : get_callouts(product, 'default') } %}.

Step 8: 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 9: 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 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.

Step 10: 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

  1. Configure a new form under App\Form\Type\Rule namespace,
  2. Add a rule checker under App\Checker\Rule namespace and make sure it implements Setono\SyliusCalloutPlugin\Checker\Rule\ProductCalloutRuleCheckerInterface interface and has a public const TYPE set corresponding to the below service configuration
  3. 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>