【问题标题】:Overriding PriceHelper in Sylius在 Sylius 中覆盖 PriceHelper
【发布时间】:2018-08-20 05:23:11
【问题描述】:

我试图通过添加getOriginalPrice 函数来覆盖PriceHelper 类,但由于服务未注册,在service.yml 上,我输入:

services:
    AppBundle\Helper\PriceHelper\:
        class: AppBundle\Helper\PriceHelper
        arguments:
            - "@sylius.calculator.product_variant_price"
        tags:
            - { name: templating.helper, event: sylius.templating.helper.price, method: getOriginalPrice, alias: sylius_calculate_original_price }

在树枝上,我补充说:

{%- macro calculateOriginalPrice(variant) -%}
  {% from _self import convertAndFormat %}
  {{- convertAndFormat(variant|sylius_calculate_original_price({'channel': sylius.channel})) }}
{%- endmacro -%}

并替换:

{{ money.calculatePrice(product|sylius_resolve_variant) }}

{{ money.calculateOriginalPrice(product|sylius_resolve_variant) }}

错误:

Unknown "sylius_calculate_original_price" filter. Did you mean 
"sylius_calculate_price"?

有什么想法吗?

【问题讨论】:

    标签: symfony sylius


    【解决方案1】:

    问题解决了。 services.yml 实际上很好。只需在 config.yml 中添加以下内容:

    twig:
        globals:
            sylius_calculate_original_price: "@app.templating.helper.price"
    

    在树枝上:

    {{ sylius_calculate_original_price.getOriginalPrice(variant,{'channel': sylius.channel}) }}
    

    services.yml 可以缩短为:

    app.templating.helper.price:
        decorates: sylius.templating.helper.price
        class: AppBundle\Helper\PriceHelper
        arguments:
            - "@sylius.calculator.product_variant_price"
    

    【讨论】:

      【解决方案2】:

      PriceHelper 服务在这个文件中配置:https://github.com/Sylius/Sylius/blob/f7d42d2ce64288407372775e0ed421debcd50cd3/src/Sylius/Bundle/CoreBundle/Resources/config/services/templating.xml

      但是,不要像以前那样更换服务,而应该装饰它。使用新类扩展 PriceHelper 类并添加您需要的功能,然后为您的新服务添加配置,如以下链接中所述,以装饰 PriceHelper 服务:http://symfony.com/doc/current/service_container/service_decoration.html

      在您的具体情况下,您必须使用此配置:

      AppBundle\Helper\PriceHelper:
          decorates: '@sylius.templating.helper.price'
          arguments:
              $productVariantPriceCalculator: '@sylius.calculator.product_variant_price'
      

      【讨论】:

      • 我更新如下:services: AppBundle\Helper\PriceHelper\: decorates: Sylius\Bundle\CoreBundle\Templating\Helper\PriceHelper class: AppBundle\Helper\PriceHelper arguments: - "@sylius.calculator.product_variant_price" tags: - { name: templating.helper, event: sylius.templating.helper.price, method: getOriginalPrice, alias: sylius_calculate_original_price } 错误显示:You have requested a non-existent service "Sylius\Bundle\CoreBundle\Templating\Helper\PriceHelper".
      • 它在哪里抛出该错误?如果可能,请发布整个堆栈跟踪或错误消息上下文。
      • 当页面调用 calculateOriginalPrice 函数时显示错误。完整日志:(1/1) ServiceNotFoundException You have requested a non-existent service "Sylius\Bundle\CoreBundle\Templating\Helper\PriceHelper". in ContainerBuilder.php line 1043 at ContainerBuilder->getDefinition('Sylius\\Bundle\\CoreBundle\\Templating\\Helper\\PriceHelper') in DecoratorServicePass.php line 55 at DecoratorServicePass->process(object(ContainerBuilder)) in Compiler.php line 141 at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 778 at ContainerBuilder->compile()
      • 如果我将装饰更改为:decorates: sylius.templating.helper.price 并删除了参数,则改为以下错误抛出:Cannot autowire service "AppBundle\Helper\PriceHelper\": argument "$productVariantPriceCalculator" of method "Sylius\Bundle\CoreBundle\Templating\Helper\PriceHelper::__construct()" references interface "Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface" but no such service exists. You should maybe alias this interface to the existing "sylius.calculator.product_variant_price" service.
      • 尝试如下服务配置:AppBundle\Helper\PriceHelper: decorates: sylius.templating.helper.price arguments: $productVariantPriceCalculator: '@sylius.calculator.product_variant_price'
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      相关资源
      最近更新 更多