在单页结帐中重新计算商品价格

Recalculate item prices in onepage checkout

我使用的付款方式接受 分期付款 处理付款。使用分期付款时,需要使用 未打折的 价格,因此我在 sales_quote_collect_totals_before 注册了一个观察员,以便在选中分期付款选项时强制重新计算商品价格(下面的代码) .

  1. 完成单页结帐流程
  2. 到达付款信息部分
  3. select支付方式
  4. 选中分期付款选项
  5. 点击继续
  6. 到达购物车评论
  7. 注意价格没有更新(即使观察者已被执行)
  8. 返回付款方式 selection
  9. 再次点击继续
  10. 到达购物车评论
  11. 价格现已更新

谁能解释一下为什么?并可能给出一个解决方案,告诉我如何跳过步骤 6-11 :-) ?


$installments = /* has the option been checked */

/** @var Mage_Sales_Model_Quote_Item $item */
foreach ($quote->getAllItems() as $item) {
    $product = $item->getProduct();
    $qty = $item->getQty();

    $price = $installments ? $product->getPrice() : $product->getFinalPrice($qty);

    if ($price == $item->getOriginalCustomPrice()) {
        continue;
    }

    $item->setPrice($price);
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);

    $item->calcRowTotal();
}

好的,我自己找到了答案。

显然 Mage_Sales_Model_Quote_Payment::importData 在分配新支付数据之前执行 Mage_Sales_Model_Quote::collectTotals,之后 totals_collected_flag 阻止了重新计算。