loevgaard/sylius-barcode-plugin

使用此插件为您的产品添加条形码

安装次数: 22,535

依赖者: 2

建议者: 2

安全性: 0

星标: 8

关注者: 3

分支: 5

开放问题: 8

类型:sylius-plugin


README

Latest Version on Packagist Software License Build Status Quality Score

为您的产品添加条形码。

安装

步骤 1: 下载插件

打开命令行,进入您的项目目录并执行以下命令以下载此捆绑包的最新稳定版本

$ composer require loevgaard/sylius-barcode-plugin

此命令要求您全局安装了Composer,如Composer文档中的安装章节所述。

步骤 2: 启用插件

然后,通过将其添加到项目config/bundles.php文件中注册的插件/捆绑包列表中来启用插件

<?php
// config/bundles.php

return [
    // ...
    Setono\DoctrineORMBatcherBundle\SetonoDoctrineORMBatcherBundle::class => ['all' => true],
    Loevgaard\SyliusBarcodePlugin\LoevgaardSyliusBarcodePlugin::class => ['all' => true],
    // ...
];

注意,添加SetonoDoctrineORMBatcherBundle也要求。

步骤 3: 配置插件

# config/packages/loevgaard_sylius_barcode.yaml
imports:
    # ...
    - { resource: "@LoevgaardSyliusBarcodePlugin/Resources/config/app/config.yaml" }
        
loevgaard_sylius_barcode:
    form:
        require: true # If true the barcode field will be required in the product forms
        require_valid: true # If true the barcode must be valid when entered in the product forms

步骤 4: 导入产品变体特性

<?php
// src/Entity/ProductVariant.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Loevgaard\SyliusBarcodePlugin\Model\ProductVariantInterface as LoevgaardSyliusBarcodePluginProductVariantInterface;
use Loevgaard\SyliusBarcodePlugin\Model\ProductVariantTrait as LoevgaardSyliusBarcodePluginProductVariantTrait;
use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_product_variant")
 */
class ProductVariant extends BaseProductVariant implements LoevgaardSyliusBarcodePluginProductVariantInterface
{
    use LoevgaardSyliusBarcodePluginProductVariantTrait;
    
    // ...
}

注意:如果您尚未扩展ProductVariant实体,请首先遵循自定义说明,因为您需要添加更多配置。

# config/packages/_sylius.yaml
sylius_product:
    resources:
        product_variant:
            classes:
                model: App\Entity\ProductVariant

步骤 5: 更新数据库模式

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

步骤 6: 向twig模板添加表单小部件

您需要覆盖显示产品和产品变体表单的模板,并添加一个包含条形码的form_row语句

{# templates/bundles/SyliusAdminBundle/ProductVariant/Tab/_details.html.twig #}

{# ... #}

<div class="ui segment">
    {{ form_row(form.code) }}
    {{ form_row(form.barcode) }} {# This is the part you should add #}
    <div class="two fields">
        {{ form_row(form.shippingCategory) }}
    </div>
    {{ form_row(form.channelPricings) }}
</div>

{# ... #}
{# templates/bundles/SyliusAdminBundle/Product/Tab/_details.html.twig #}

<div class="ui segment">
    {{ form_row(form.code) }}
    {{ form_row(form.enabled) }}
    {% if product.simple %}
        {{ form_row(form.variant.barcode) }} {# This is the part you should add #}
        {{ form_row(form.variant.onHand) }}
        {{ form_row(form.variant.tracked) }}
        {{ form_row(form.variant.shippingRequired) }}
        {{ form_row(form.variant.version) }}
    {% else %}
        {{ form_row(form.options) }}
        {{ form_row(form.variantSelectionMethod) }}
    {% endif %}

    {# Nothing to see here. #}
    <div class="ui hidden element">
        {% if product.simple %}
            {{ form_row(form.variant.translations) }}
        {% endif %}
        {{ form_row(form.variantSelectionMethod) }}
    </div>
</div>

如果您尚未覆盖模板,您可以直接从vendor/loevgaard/sylius-barcode-plugin/src/Resources/views/SyliusAdminBundle复制模板到templates/bundles/SyliusAdminBundle/

步骤 7: 添加验证器约束

创建config/validator/ProductVariant.xml

<?xml version="1.0" encoding="UTF-8"?>

<constraint-mapping xmlns="https://symfony.com.cn/schema/dic/constraint-mapping"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="https://symfony.com.cn/schema/dic/constraint-mapping https://symfony.com.cn/schema/dic/services/constraint-mapping-1.0.xsd">
    <class name="App\Entity\ProductVariant">
        <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
            <option name="fields">barcode</option>
            <option name="message">loevgaard_sylius_barcode.product_variant.barcode.unique</option>
            <option name="groups">sylius</option>
        </constraint>
    </class>
</constraint-mapping>

如示例应用程序中配置所示。

步骤 8: 使用异步传输(可选,但推荐)

本插件中所有命令都将扩展CommandInterface。因此,您可以通过将以下内容添加到您的信使配置来轻松路由所有命令。

# config/packages/messenger.yaml
framework:
    messenger:
        routing:
            # Route all command messages to the async transport
            # This presumes that you have already set up an 'async' transport
            'Loevgaard\SyliusBarcodePlugin\Message\Command\CommandInterface': async

用法

运行检查命令

$ php bin/console loevgaard:barcode:check

这将标记所有产品变体为已检查,并根据检查结果将字段barcodeValid更新为true或false。