You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2:切换配送方式时如何在collect方法中获取正确配送金额

问题解决:切换配送方式时Collect方法获取正确配送金额

需求:开发基于配送金额向订单收取自定义费用的功能。
问题:切换配送方式时,fetch方法能获取正确的配送金额,但collect方法中获取的金额错误,如何获取选中配送方式的正确金额?

问题原因

在Magento的总额计算流程中,collect方法执行阶段,$quote->getShippingAddress()->getShippingAmount()可能尚未同步最新选中的配送方式金额。而collect方法的$shippingAssignment参数包含了当前活跃的配送信息,是实时更新的,应该从这里获取正确的配送金额。

修改方案

修改collect方法,从$shippingAssignment中提取当前选中的配送方式金额,替换原有的直接从shipping address获取的逻辑:

修改后的完整代码

<?php

namespace Nikunj\CustomFee\Model\Quote;

use Magento\Quote\Api\Data\ShippingAssignmentInterface as ShippingAssignmentInterfaceAlias;
use Magento\Quote\Model\Quote\Address\Total;

class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
    /**
     * Collect grand total address amount
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param ShippingAssignmentInterfaceAlias $shippingAssignment
     * @param Total $total
     * @return $this
     */
    protected $quoteValidator = null;

    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
    {
        $this->quoteValidator = $quoteValidator;
    }

    public function collect(
        \Magento\Quote\Model\Quote               $quote,
        ShippingAssignmentInterfaceAlias         $shippingAssignment,
        Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);

        // 跳过无配送项的情况
        $shippingItems = $shippingAssignment->getItems();
        if (!count($shippingItems)) {
            return $this;
        }

        // 从shippingAssignment获取当前选中的配送金额
        $shippingAmount = $shippingAssignment->getShipping()->getAmount();
        $exist_amount = 0; //$quote->getCustomfee();
        $customfee = ($shippingAmount + 15);
        $balance = $customfee - $exist_amount;//final amount

        $total->setTotalAmount('customfee', $balance);
        $total->setBaseTotalAmount('customfee', $balance);

        $total->setCustomfee($balance);
        $total->setBaseCustomfee($balance);

        $total->setGrandTotal($total->getGrandTotal() + $balance);
        $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);

        return $this;
    }

    protected function clearValues(Total $total)
    {
        $total->setTotalAmount('subtotal', 0);
        $total->setBaseTotalAmount('subtotal', 0);
        $total->setTotalAmount('tax', 0);
        $total->setBaseTotalAmount('tax', 0);
        $total->setTotalAmount('discount_tax_compensation', 0);
        $total->setBaseTotalAmount('discount_tax_compensation', 0);
        $total->setTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setSubtotalInclTax(0);
        $total->setBaseSubtotalInclTax(0);
    }

    /**
     * Assign subtotal amount and label to address object
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param Total $total
     * @return array
     */
    public function fetch(\Magento\Quote\Model\Quote $quote, Total $total)
    {
        $fee = ($quote->getShippingAddress()->getShippingAmount()+15);
        return [
            'code' => 'customfee',
            'title' => 'Custom Fee',
            'value' => $fee
        ];
    }

    /**
     * Get Subtotal label
     *
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('Custom Fee');
    }
}

关键修改点

  1. 添加无配送项判断:避免在未选择配送方式时出现计算错误。
  2. 从shippingAssignment获取配送金额:通过$shippingAssignment->getShipping()->getAmount()获取当前选中配送方式的实时金额,确保切换配送方式时金额同步更新。

内容的提问来源于stack exchange,提问作者Nikunj Rawat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 02:25:17