Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

php - Magento: adding duties/taxes to a quote during review

I need to make a call to a 3rd party API to get up to date Duties/Taxes for international shipping during the review stage of the checkout process. I have the API call ready to go, however I am missing a way to add the returned duties and taxes to the quote.

Is there a built in way to do this?

im hoping there is something like

$quote->addCostComponent("Duties", 5.0);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to perform such steps:

  1. First of all you need to create attributes for your custom duties/taxes for displaying them in order, not only just add to the grand total. There should be at least two attributes one for value in website currency (used in payment capture and it should have base_ prefix) and one value in displayed currency (used just for displaying amount in desired currency for customer). This attributes should be added to every entity with financial part (quote_address, order, invoice). For instance it should be: base_your_attribute_code and your_attribute_code with decimal type.

  2. Then you need create your total collector model that should be extended from Mage_Sales_Model_Quote_Address_Total_Abstract and implement collect and fetch methods like in this example:

    /**
     * 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. After you the collector model is created you need add it to configuration:

    <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>
    
    • class node contains the class alias of your collector model
    • before and after nodes indicate invocation order of your collector.
  4. You need to add your total attributes to field-sets that will be used for copying calculated data into order or invoice:

    <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. After performing this steps you will be able to see your custom fee in order totals


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...