如何在 Shopware6 中重新定义计算价格?

问题描述

我正在尝试在 Shopware6 中编写插件,它必须向产品行项目添加一些自定义值(如存款)。所以我写了我的课

class DepositCalculatedPrice extends CalculatedPrice
{
    /** @var float $deposit */
    protected $deposit;

    public function __construct(
        float $unitPrice,float $totalPrice,CalculatedTaxCollection $calculatedTaxes,CalculatedTaxCollection $calculatedTaxesNoDeposit,TaxRuleCollection $taxRules,int $quantity = 1,?ReferencePrice $referencePrice = null,?ListPrice $listPrice = null,float $deposit = 0,) {
        parent::__construct(
            $unitPrice,$totalPrice,$calculatedTaxes,$taxRules,$quantity,$referencePrice,$listPrice
        );
        $this->deposit = $deposit;
    }

    public function getDeposit(): float
    {
        return FloatComparator::cast($this->deposit);
    }
}

然后我在我的计算器和价格处理器中使用它。一切顺利,直到我尝试提交订单,但随后 Shopware6 检查 Checkout/Order/Aggregate/OrderLineItem/OrderLineItemDeFinition.PHP 中的字段定义类,并根据计算价格检查价格 json 字段,而不是存款计算价格。 那么有没有办法解决呢?也许我可以在某个地方使用 OrderLineItemDeFinition.PHP 的某个后代?或者让它不检查字段定义?

解决方法

我认为您需要遵循该文档 https://docs.shopware.com/en/shopware-platform-dev-en/how-to/cart-change-price

我还需要实现从外部系统应用的自定义价格,我完全按照该文档进行了操作,因此您需要在购物车处理期间准确设置价格,您可以使用您的 DepositCalculatedPrice 值来完成。