0

我需要为订单添加自定义折扣。我创建了一个观察者,如下所示:

$shippingDiscountValue = 20;
$currentAddress = $quote->getShippingAddress();
$currentDiscount = (-1) * $currentAddress->getDiscountAmount();
$newDiscount = $currentDiscount + $shippingDiscountValue;
$quote->setGrandTotal($quote->getBaseSubtotal() - $shippingDiscountValue)
    ->setBaseGrandTotal($quote->getBaseSubtotal() - $shippingDiscountValue)
    ->setSubtotalWithDiscount($quote->getBaseSubtotal() - $shippingDiscountValue)
    ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $shippingDiscountValue)
    ->save();  

$currentAddress->setSubtotalWithDiscount((float) $currentAddress->getSubtotalWithDiscount() - $shippingDiscountValue);
$currentAddress->setGrandTotal((float) $currentAddress->getGrandTotal() - $shippingDiscountValue);
$currentAddress->setBaseSubtotalWithDiscount((float) $currentAddress->getBaseSubtotalWithDiscount() - $shippingDiscountValue);
$currentAddress->setBaseGrandTotal((float) $currentAddress->getBaseGrandTotal() - $shippingDiscountValue);        

$currentAddress->setBaseDiscountAmount($newDiscount * (-1));
$currentAddress->setDiscountAmount($newDiscount * (-1));

$currentAddress->save();
$quote->save();

在我的购物车发生任何变化(添加、删除、更新数量)之后,我都会调用这个观察者。在购物车详细信息中一切正常 - 折扣计算正确,但在结帐时(按顺序)有基本折扣金额,没有$shippingDiscountValue.

4

0 回答 0