The labels 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.
Shop:
Admin:
Open a command console, enter your project directory and execute the following command to download the latest stable version of this plugin:
$ composer require tavy315/sylius-labels-plugin
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
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 = [
Tavy315\SyliusLabelsPlugin\Tavy315SyliusLabelsPlugin::class => ['all' => true],
Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];
# config/packages/tavy315_product_labels.yaml
imports:
- { resource: "@Tavy315SyliusLabelsPlugin/Resources/config/app/config.yaml" }
# config/routes/tavy315_product_labels.yaml
tavy315_product_labels:
resource: "@Tavy315SyliusLabelsPlugin/Resources/config/routing.yaml"
Read more about Sylius models customization here.
Add a Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait
trait to your App\Entity\Product
class.
-
If you use
annotations
mapping:<?php // src/Entity/Product.php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Core\Model\Product as BaseProduct; use Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait; use Tavy315\SyliusLabelsPlugin\Model\ProductInterface; /** * @ORM\Entity * @ORM\Table(name="sylius_product") */ class Product extends BaseProduct implements ProductInterface { use LabelsAwareTrait { LabelsAwareTrait::__construct as private __labelsTraitConstruct; } public function __construct() { $this->__labelsTraitConstruct(); parent::__construct(); } }
-
If you use
xml
mapping:<?php // src/Model/Product.php namespace App\Model; use Sylius\Component\Core\Model\Product as BaseProduct; use Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait; use Tavy315\SyliusLabelsPlugin\Model\ProductInterface; class Product extends BaseProduct implements ProductInterface { use LabelsAwareTrait { LabelsAwareTrait::__construct as private __labelsTraitConstruct; } public function __construct() { $this->__labelsTraitConstruct(); 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="labels" target-entity="Tavy315\SyliusLabelsPlugin\Model\LabelInterface"> <join-table name="tavy315_sylius_product_labels"> <join-columns> <join-column name="product_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" /> </join-columns> <inverse-join-columns> <join-column name="label_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.
$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate
Add labels to your product box template. By default, you should use templates/bundles/SyliusShopBundle/Product/__mainImage.html.twig
path. Check out our __mainImage.html.twig file for a reference.
Note the line: {% include "@Tavy315SyliusLabelsPlugin/Shop/Product/Label/_labels.html.twig" with {'labels' : product.labels} %}
.
From now on you should be able to add new labels in the admin panel. Once you add one, you can attach it to products.