应用目录价格规则后,Magento 2 插件 afterGetPrice 不起作用

Magento 2 Plugin afterGetPrice not working when catalog price rule applied

我创建了一个简单的 Magento 2 插件,它增加了价格。

这是我测试过的简单代码

<?php

namespace [Vendor]\[Name]\Plugin\Magento\Catalog\Model;

class Product
{
    protected $objectManager;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
        )
    {
        $this->objectManager = $objectManager;
    }


    public function afterGetPrice(
        \Magento\Catalog\Model\Product $subject,
        $result
    ) {

        return $result + 100;

    }

}

这段代码工作正常,但是当我创建目录价格规则时,系统好像没有使用它,而是从另一个模块获取价格。 有没有人遇到过这个问题并找到了解决方案?我想了解并解决问题。

提前致谢。

我解决了问题。

实践中 module-catalog-rule/Model/Indexer/ProductPriceCalculator:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\CatalogRule\Model\Indexer;

/**
 * Product price calculation according rules settings.
 */
class ProductPriceCalculator
{
    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
     */
    private $priceCurrency;

    /**
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     */
    public function __construct(\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
    {
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * Calculates product price.
     *
     * @param array $ruleData
     * @param null $productData
     * @return float
     */
    public function calculate($ruleData, $productData = null)
    {
        if ($productData !== null && isset($productData['rule_price'])) {
            $productPrice = $productData['rule_price'];
        } else {
            $productPrice = $ruleData['default_price'];
        }

        switch ($ruleData['action_operator']) {
            case 'to_fixed':
                $productPrice = min($ruleData['action_amount'], $productPrice);
                break;
            case 'to_percent':
                $productPrice = $productPrice * $ruleData['action_amount'] / 100;
                break;
            case 'by_fixed':
                $productPrice = max(0, $productPrice - $ruleData['action_amount']);
                break;
            case 'by_percent':
                $productPrice = $productPrice * (1 - $ruleData['action_amount'] / 100);
                break;
            default:
                $productPrice = 0;
        }

        return $this->priceCurrency->round($productPrice);
    }
}

计算函数 ($ruleData, $ProductData = null) 执行目录规则中设置的计算。

真正的问题在于$ruleData,因为它包含的价格信息是直接从product_catalog数据库中获取的,不包括任何旨在修改价格的插件。

我个人不知道,这是否是 Magento 2 的错误,但使模块目录规则起作用的唯一方法就是拦截该变量并修改其内容。

希望对大家有所帮助。 :)

Ps。抱歉我的英语不好