setono/sylius-callout-plugin

为您的Sylius产品添加呼出功能

安装: 48 000

依赖关系: 0

建议者: 0

安全性: 0

星标: 9

关注者: 2

分支: 13

开放问题: 4

类型:sylius-plugin

v0.5.0-alpha.3 2024-02-05 12:15 UTC

README

Latest Version Software License Build Status Code Coverage

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

截图

商店

Screenshot showing callouts on product list

管理员

Screenshot showing admin callout update form Screenshot showing admin callouts list

安装

步骤 1: 下载插件

$ composer require setono/sylius-callout-plugin

步骤 2: 启用插件

然后,通过将其添加到项目中config/bundles.php文件中已注册插件/包列表来启用插件(在SyliusGridBundle之前)

<?php
$bundles = [
    Setono\SyliusCalloutPlugin\SetonoSyliusCalloutPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];

步骤 3: 配置插件

# config/packages/setono_sylius_callout.yaml

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

步骤 4: 导入路由

# config/routes/setono_sylius_callout.yaml

setono_sylius_callout:
    resource: "@SetonoSyliusCalloutPlugin/Resources/config/routes.yaml"

步骤 5: 扩展实体

扩展Product

Setono\SyliusCalloutPlugin\Model\ProductTrait特性添加到您的App\Entity\Product\Product类中。

<?php 
// src/Entity/Product/Product.php

namespace App\Entity;

use Setono\SyliusCalloutPlugin\Model\ProductTrait as CalloutProductTrait;
use Setono\SyliusCalloutPlugin\Model\ProductInterface as CalloutProductInterface;
use Sylius\Component\Core\Model\Product as BaseProduct;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_product")
 */
class Product extends BaseProduct implements CalloutProductInterface
{
    use CalloutProductTrait;
}

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

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

步骤 7: 将呼出功能添加到您的产品模板

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

注意以下行:{% include "@SetonoSyliusCalloutPlugin/Shop/Product/Callout/_callouts.html.twig" with { 'callouts' : get_callouts(product, 'default') } %}

步骤 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
            'Setono\SyliusCalloutPlugin\Message\Command\CommandInterface': async

步骤 9: 配置cron作业

出于性能原因,在您的生产服务器上配置cron作业以定期执行$ bin/console setono:sylius-callout:assign命令,以分配所有呼出功能。在大多数情况下,它应该通过资源事件监听器在创建/更新产品或呼出时触发,但如果出现问题,值得覆盖这一点。

步骤 10: 安装资源

$ bin/console assets:install

使用方法

从现在开始,您应该能够在管理员面板中添加新的呼出功能。一旦添加,只需进行配置即可。

自定义

添加新的规则表单

  1. App\Form\Type\Rule命名空间下配置新的表单,
  2. App\Checker\Rule命名空间下添加规则检查器,并确保它实现了Setono\SyliusCalloutPlugin\Checker\Rule\ProductCalloutRuleCheckerInterface接口,并设置了一个public const TYPE与以下服务配置相对应
  3. 注册和标记新的服务
<!-- services.xml -->
<services>
    ...
    
    <service id="app.callout_rule_checker.is_on_sale" class="Setono\SyliusCalloutPlugin\Callout\Checker\Rule\IsOnSaleRuleChecker">
        <argument type="service" id="setono_sylius_callout.checker.product_promotion" />
        <tag name="setono_sylius_callout.callout_rule_checker" type="is_on_sale" label="setono_sylius_callout.ui.is_on_sale" form-type="Setono\SyliusCalloutPlugin\Form\Type\Rule\IsOnSaleConfigurationType" />
    </service>
    
    <service id="app.form.type.rule.is_on_sale" class="Setono\SyliusCalloutPlugin\Form\Type\Rule\IsOnSaleConfigurationType">
        <tag name="form.type" />
    </service>
</services>