定制 loevgaard/sylius-barcode-plugin 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

loevgaard/sylius-barcode-plugin

Composer 安装命令:

composer require loevgaard/sylius-barcode-plugin

包简介

Add barcodes to your products using this plugin

README 文档

README

Latest Version on Packagist Software License Build Status Quality Score

Add barcodes to your products.

Installation

Step 1: Download the plugin

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require loevgaard/sylius-barcode-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 the config/bundles.php file of your project:

<?php
// config/bundles.php

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

Note that adding SetonoDoctrineORMBatcherBundle also required.

Step 3: Configure the plugin

# 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

Step 4: Import product variant trait

<?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;
    
    // ...
}

NOTE: If you haven't extended the ProductVariant entity yet, follow the customization instructions first because you need to add a bit more configuration:

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

Step 5: Update your database schema

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

Step 6: Add form widgets to twig templates

You need to override the template displaying the product and product variant form and add a form_row statement with the barcode:

{# 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>

If you haven't overridden the template yet, you can just copy the templates from vendor/loevgaard/sylius-barcode-plugin/src/Resources/views/SyliusAdminBundle to templates/bundles/SyliusAdminBundle/

Step 7: Add validator constraint

Create config/validator/ProductVariant.xml:

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

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/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>

Like it configured at example application.

Step 8: Using asynchronous transport (optional, but recommended)

All commands in this plugin will extend the CommandInterface. Therefore you can route all commands easily by adding this to your Messenger config:

# 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

Usage

Run the check command:

$ php bin/console loevgaard:barcode:check

This will mark all product variant as checked and updated the field barcodeValid to either true or false depending on the result of the check.

loevgaard/sylius-barcode-plugin 适用场景与选型建议

loevgaard/sylius-barcode-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.05k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2018 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 loevgaard/sylius-barcode-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 loevgaard/sylius-barcode-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 loevgaard/sylius-barcode-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 2

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-15