tavy315 / sylius-labels-plugin
Sylius plugin for product labels.
Package info
github.com/tavy315/SyliusLabelsPlugin
Type:sylius-plugin
pkg:composer/tavy315/sylius-labels-plugin
Requires
- sylius/sylius: ^1.10
Requires (Dev)
- ext-json: *
- behat/behat: ^3.7
- behat/mink: ^1.8
- dmore/behat-chrome-extension: ^1.3
- dmore/chrome-mink-driver: ^2.7
- friends-of-behat/mink: ^1.8
- friends-of-behat/mink-browserkit-driver: ^1.4
- friends-of-behat/mink-debug-extension: ^2.0
- friends-of-behat/mink-extension: ^2.4
- friends-of-behat/page-object-extension: ^0.3
- friends-of-behat/symfony-extension: ^2.1
- friends-of-behat/variadic-extension: ^1.3
- phpspec/phpspec: ^7.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-doctrine: ^0.12.33
- phpstan/phpstan-strict-rules: ^0.12.0
- phpstan/phpstan-webmozart-assert: 0.12.12
- phpunit/phpunit: ^9.5
- polishsymfonycommunity/symfony-mocker-container: ^1.0
- sylius-labs/coding-standard: ^4.0
- symfony/browser-kit: ^4.4 || ^5.4 || ^6.0
- symfony/debug-bundle: ^4.4 || ^5.4 || ^6.0
- symfony/dotenv: ^4.4 || ^5.4 || ^6.0
- symfony/intl: ^4.4 || ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^4.4 || ^5.4 || ^6.0
- vimeo/psalm: ^4.6.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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.
Screenshots
Shop:
Admin:
Installation
Step 1: Install 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 tavy315/sylius-labels-plugin
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
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 = [ Tavy315\SyliusLabelsPlugin\Tavy315SyliusLabelsPlugin::class => ['all' => true], Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true], ];
Step 3: Configure plugin
# config/packages/tavy315_product_labels.yaml imports: - { resource: "@Tavy315SyliusLabelsPlugin/Resources/config/app/config.yaml" }
Step 4: Import routing
# config/routes/tavy315_product_labels.yaml tavy315_product_labels: resource: "@Tavy315SyliusLabelsPlugin/Resources/config/routing.yaml"
Step 5: Customize models
Read more about Sylius models customization here.
Customize your Product model
Add a Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait trait to your App\Entity\Product class.
-
If you use
annotationsmapping:<?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
xmlmapping:<?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.
Step 6: Update your database schema
$ php bin/console doctrine:migrations:diff $ php bin/console doctrine:migrations:migrate
Step 7: Add labels to your product templates
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} %}.
Usage
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.


