問題描述
我需要調用第 3 方 API,以便在結帳流程的審核階段了解國際運輸的最新關稅/稅費.我已準備好 API 調用,但是我缺少將退回的關稅和稅款添加到報價中的方法.
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?
我希望有類似的東西
$quote->addCostComponent("Duties", 5.0);
推薦答案
您需要執行以下步驟:
首先,您需要為您的關稅/稅款創建屬性以按順序顯示它們,而不僅僅是添加到總計中.應該至少有兩個屬性,一個是網站貨幣的價值(用于支付捕獲,它應該有
base_
前綴)和一個顯示貨幣的價值(僅用于顯示客戶所需貨幣的金額).應將此屬性添加到具有財務部分(quote_address、order、invoice)的每個實體.例如它應該是:base_your_attribute_code
和your_attribute_code
十進制類型.
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
andyour_attribute_code
with decimal type.
然后你需要創建你的總收集器模型,它應該從 Mage_Sales_Model_Quote_Address_Total_Abstract 擴展,并實現像這個例子中的收集和獲取方法:
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()
));
}
}
}
收集器模型創建后,您需要將其添加到配置中:
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 節點包含收集器模型的類別名
- before 和 after 節點表示收集器的調用順序.
- class node contains the class alias of your collector model
- before and after nodes indicate invocation order of your collector.
您需要將您的總屬性添加到將用于將計算數據復制到訂單或發票中的字段集:
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>
執行此步驟后,您將能夠在訂單總額中看到自定義費用
After performing this steps you will be able to see your custom fee in order totals
這篇關于Magento:在審查期間向報價添加關稅/稅款的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!