tavy315/sylius-labels-plugin

Sylius产品标签插件。

安装: 831

依赖项: 0

建议者: 0

安全性: 0

星级: 1

观察者: 3

分支: 4

开放问题: 0

类型:sylius-plugin

This package is auto-updated.

Last update: 2024-09-03 22:29:21 UTC


README

Latest Version Latest Unstable Version Software License Build Status

Sylius标签插件允许您根据特定规则为不同组的产品配置漂亮的徽章。它默认提供一组配置,并且在添加新配置时非常灵活。

仅支持Doctrine ORM驱动程序。

屏幕截图

商店

Screenshot showing labels on product list

管理员

Screenshot showing admin labels list

Screenshot showing admin product labels

安装

步骤 1:安装插件

打开命令行界面,进入您的项目目录,并执行以下命令以下载此插件的最新稳定版本

$ composer require tavy315/sylius-labels-plugin

此命令要求您全局安装了Composer,具体请参阅Composer文档中的安装章节

步骤 2:启用插件

然后,将插件添加到项目中config/bundles.php文件中注册的插件/捆绑包列表中,在SyliusGridBundle之前(!)启用它。

<?php
$bundles = [
    Tavy315\SyliusLabelsPlugin\Tavy315SyliusLabelsPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];

步骤 3:配置插件

# config/packages/tavy315_product_labels.yaml

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

步骤 4:导入路由

# config/routes/tavy315_product_labels.yaml

tavy315_product_labels:
    resource: "@Tavy315SyliusLabelsPlugin/Resources/config/routing.yaml"

步骤 5:自定义模型

有关Sylius模型定制的更多信息,请参阅此处

自定义您的产品模型

Tavy315\SyliusLabelsPlugin\Model\LabelsAwareTrait特质添加到您的App\Entity\Product类中。

  • 如果使用annotations映射

    <?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();
        }
    }
  • 如果使用xml映射

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

如果您还没有这样做,配置sylius_product资源,使其指向您的App\Entity\Product,就像我们在以下示例中做的那样此处

步骤 6:更新您的数据库模式

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

步骤 7:将标签添加到您的产品模板中

将标签添加到您的产品盒模板中。默认情况下,您应该使用templates/bundles/SyliusShopBundle/Product/__mainImage.html.twig路径。查看我们的__mainImage.html.twig文件作为参考。

注意以下行:{% include "@Tavy315SyliusLabelsPlugin/Shop/Product/Label/_labels.html.twig" with {'labels' : product.labels} %}

用法

从现在起,您应该在管理面板中能够添加新的标签。一旦添加,您就可以将其附加到产品上。