Sylius中的重写PriceHelper

时间:2018-08-20 05:23:12

标签: symfony sylius

我试图通过添加private static void waitDevice(String path, int pollIntervalMS, boolean invert) { try { while (invert ^ !pollDevice(path)) { System.out.println("Waiting..."); Thread.sleep(pollIntervalMS); } } catch (InterruptedException e) {} } private static Boolean pollDevice(String path) { FileReader f = null; try { f = new FileReader(path); System.out.println(f.read()); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (f != null) { try { f.close(); } catch (IOException e) {System.out.println("Error Closing Device!!!");} } } return false; } 函数来覆盖PriceHelper类,但是由于该服务未注册,因此我在getOriginalPrice上输入:

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) }}

有什么主意吗?

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

在以下文件中配置了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'