umanit/sylius-product-variant-attribute-plugin

Plugin for Sylius to add attributes on product variants.


README

68747470733a2f2f64656d6f2e73796c6975732e636f6d2f6173736574732f73686f702f696d672f6c6f676f2e706e67

UmanIT

Sylius Product Variant Attribute Plugin

Add attributes on your products variants.

Install

Install the plugin with composer

$ composer require umanit/sylius-product-variant-attribute-plugin

Register the plugin to your config/bundles.php

<?php

return [
    // ...
    Umanit\SyliusProductVariantAttributePlugin\UmanitSyliusProductVariantAttributePlugin::class => ['all' => true],
];

Import the configuration file, for example in config/packages/umanit_sylius_product_variant_attribute_plugin.yaml

imports:
    - { resource: '@UmanitSyliusProductVariantAttributePlugin/Resources/config/config.yaml' }

Import the routing file, for example in config/routes/sylius_admin.yaml

umanit_sylius_product_variant_attribute_plugin:
    resource: '@UmanitSyliusProductVariantAttributePlugin/Resources/config/admin_routing.yaml'
    prefix: /admin

Update your ProductVariant entity by implementing the ProductVariantInterface and using the ProductVariantTrait

<?php

declare(strict_types=1);

namespace App\Entity\Product;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
use Sylius\Component\Product\Model\ProductVariantTranslationInterface;
use Umanit\SyliusProductVariantAttributePlugin\Entity\ProductVariantInterface;
use Umanit\SyliusProductVariantAttributePlugin\Entity\ProductVariantTrait;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_product_variant")
 */
class ProductVariant extends BaseProductVariant implements ProductVariantInterface
{
    use ProductVariantTrait {
        __construct as attributesConstruct;
    }

    public function __construct()
    {
        parent::__construct();

        $this->attributesConstruct();
    }

    protected function createTranslation(): ProductVariantTranslationInterface
    {
        return new ProductVariantTranslation();
    }
}

Finally, don't forget to update your database!

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

Usage

Like in product edition, variants now has an Attributes tab in which you can add attributes. The operation and possibilities are the same as with the existing attributes.

The variant attributes list differ from the existing one used for products. A new entry is added to the Catalog menu in order to manage this new list.

By default, the existing entry Attributes is renamed to Products attributes. You can change this behaviour by defining the following configuration:

umanit_sylius_product_variant_attribute_plugin:
    rename_product_attribute_menu_entry: false