tavy315/sylius-labels-plugin

Sylius plugin for product labels.

Installs: 833

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 4

Open Issues: 0

Type:sylius-plugin

This package is auto-updated.

Last update: 2024-11-03 22:48:41 UTC


README

Latest Version Latest Unstable Version Software License Build Status

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:

Screenshot showing labels on product list

Admin:

Screenshot showing admin labels list

Screenshot showing admin product labels

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 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.

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.