Magento:在审核期间向报价添加关税/税金

时间:2011-02-02 17:04:32

标签: php magento

我需要拨打第三方API,以便在结帐流程的审核阶段获取国际运费的最新职责/税收。我准备好了API调用,但是我错过了将返回的职责和税收添加到报价中的方法。

是否有内置方法可以做到这一点?

我希望有类似

的东西
$quote->addCostComponent("Duties", 5.0);

1 个答案:

答案 0 :(得分:15)

您需要执行以下步骤:

  1. 首先,您需要为自定义职责/税收创建属性,以便按顺序显示它们,而不仅仅是添加到总计。应该至少有两个属性,一个用于网站货币的值(用于支付捕获,它应该有base_前缀)和一个显示货币的值(用于显示客户所需货币的金额)。应将此属性添加到具有财务部分的每个实体(quote_address,order,invoice)。例如,它应该是:base_your_attribute_codeyour_attribute_code,带小数类型。

  2. 然后,您需要创建应从Mage_Sales_Model_Quote_Address_Total_Abstract扩展的总收集器模型,并实现收集和获取方法,如下例所示:

    /**
     * Your custom total model
     *
     */
    class Your_Module_Model_Total_Custom extends Mage_Sales_Model_Quote_Address_Total_Abstract
    {
        /** 
         * Constructor that should initiaze 
         */
        public function __construct()
        {
            $this->setCode('your_attribute_code');
        }
    
        /**
         * Used each time when collectTotals is invoked
         * 
         * @param Mage_Sales_Model_Quote_Address $address
         * @return Your_Module_Model_Total_Custom
         */
        public function collect(Mage_Sales_Model_Quote_Address $address)
        {
            parent::collect($address);
    
            // ... Some your api calls to retrive amount ...
    
            // Set base amount of your custom fee
            $this->_setBaseAmount($calculatedAmount);
    
            // Set amount of your custom fee in displayed currency
            $this->_setAmount(
                $address->getQuote()->getStore()->convertPrice($calculatedAmount, false)
            );
    
            return $this;
        }
    
        /**
         * Used each time when totals are displayed
         * 
         * @param Mage_Sales_Model_Quote_Address $address
         * @return Your_Module_Model_Total_Custom
         */
        public function fetch(Mage_Sales_Model_Quote_Address $address)
        {
            // Display total only if it is not zero
            if ($address->getYourAttributeCode() != 0) {
                $address->addTotal(array(
                    'code' => $this->getCode(),
                    'title' => 'My Custom Duty',
                    'value' => $address->getYourAttributeCode()
                ));
            }
        }
    }
    
  3. 创建收集器模型后,需要将其添加到配置中:

    <config>
        <global>
            <sales>
                <quote>
                    <totals>
                        <your_total_code>
                            <class>your_module/total_custom</class>
                            <before>grand_total</before>
                            <after>shipping</after>
                        </your_total_code>
                    </totals>
                </quote>
            </sales>
        </global>
    </config>
    
    • 节点包含收集器模型的类别名
    • 之前之后节点表示收藏家的调用顺序。
  4. 您需要将总属性添加到字段集中,用于将计算数据复制到订单或发票中:

    <config>
        <global>
            <fieldsets>
                <!-- copies data from quote address to order during the order placement -->
                <sales_convert_quote_address>
                    <base_your_attribute_code><to_order>*</to_order></base_your_attribute_code>
                    <your_attribute_code><to_order>*</to_order></your_attribute_code>
                </sales_convert_quote_address>
    
                <!-- copies data from order to invoice/shipment/creditmemo during their creation -->
                <sales_convert_order>
                    <base_your_attribute_code><to_invoice>*</to_invoice><to_shipment>*</to_shipment><to_cm>*</to_cm></base_your_attribute_code>
                    <your_attribute_code><to_invoice>*</to_invoice><to_shipment>*</to_shipment><to_cm>*</to_cm></your_attribute_code>
                </sales_convert_order>
    
            </fieldsets>
        </global>
    </config>
    
  5. 执行此步骤后,您将能够按订单总额查看自定义费用