在很多电商行业中,客户下单后,在结账页面会有一个是否要选择保险等选项。设置保费为产品总价的百分比,而这部分费用是由客户来承担的。讲在Magento上也可以实现客户选择保险的这个功能。
示例: <global> <sales> <quote> <totals> <turnkeye_insurance> <class>turnkeye_insurance/total_screen_quote</class> <after>subtotal</after> <before>tax</before> </turnkeye_insurance> </totals> </quote> <order_invoice> <totals> <turnkeye_insurance> <class>turnkeye_insurance/total_screen_invoice</class> <after>subtotal</after> <before>tax</before> </turnkeye_insurance> </totals> </order_invoice> <order_creditmemo> <totals> <turnkeye_insurance> <class>turnkeye_insurance/total_screen_creditmemo</class> <after>subtotal</after> <before>tax</before> </turnkeye_insurance> </totals> </order_creditmemo> </sales> </global> 在这里,我们有3个Magento系统:1 – 报价,2 – 发票和3 – creditmemo添加到总计中。 因此,总的价格将有更多的报价,发票和creditmemo的的费用(你可以添加相应的类)。 在此配置中我们可以告知的 Magento 顺序将计算我们的总计,所以您可以传送或税计算阶段后计算的东西。例如如果您将需要航运或税务成本在您进一步的计算。然而,它不影响客户前端的总计的顺序。若要更改总数的汇总表中的位置,您需要插入此代码: <default> <sales> <totals_sort> <turnkeye_insurance>15</turnkeye_insurance> </totals_sort> </sales> </default> 第一类是“turnkeye_insurance/total_insurance_quote” class Turnkeye_Insurance_Model_Total_Insurance_Quote extends Mage_Sales_Model_Quote_Address_Total_Abstract { public function __construct() { $this->setCode('turnkeye_insurance'); } /** * Get label * * @return string */ public function getLabel() { return Mage::helper('turnkeye_insurance')->__('Insurance'); } /** * Collect totals information about insurance * * @param Mage_Sales_Model_Quote_Address $address */ public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); if (($address->getAddressType() == 'billing')) { return $this; } $amount = INSURANCE_FEE; if ($amount) { $this->_addAmount($amount); $this->_addBaseAmount($amount); } return $this; } /** * Add giftcard totals information to address object * * @param Mage_Sales_Model_Quote_Address $address */ public function fetch(Mage_Sales_Model_Quote_Address $address) { if (($address->getAddressType() == 'billing')) { $amount = INSURANCE_FEE; if ($amount != 0) { $address->addTotal(array( 'code' => $this->getCode(), 'title' => $this->getLabel(), 'value' => $amount )); } } return $this; } } 为每个地址中的报价单,这就是为什么我们需要添加我们收费,只是如果我们计算出的帐单地址,在其它情况下费用总计将添加两次计算总集合。此外如果您有多币种存储您将需要计算基数金额率和在此之后将此金额添加到基共。 发票和 creditmemo 的总类是非常类似: class Turnkeye_Insurance_Model_Total_Insurance_Creditmemo extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract { public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo) { $order = $creditmemo->getOrder(); $amount = INSURANCE_FEE; if ($amount) { $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $amount); $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $amount); } return $this; } } class Turnkeye_Insurance_Model_Total_Screen_Invoice extends Mage_Sales_Model_Order_Invoice_Total_Abstract { public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $order = $invoice->getOrder(); $amount = INSURANCE_FEE; if ($amount) { $invoice->setGrandTotal($invoice->getGrandTotal() + $amount); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $amount); } return $this; } } 在这里我们需要添加一笔费用,所以总价值将会与顺序相同。有一件事,你应牢记: 发票和 creditmemo 可以是分部的在这种情况下如果你总收费链接项目价格与您将需要计算你费根据开票退还项目。 后将与您的费用,计算的总价格,但你不会看到您的购物车/结帐页以外的任何位置总计行。所以有 3 块 (秩序、 发票、 creditmemo),您需要在其上添加您的总额,我们需要重写它: <blocks> <adminhtml> <rewrite> <sales_order_totals>Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Totals</sales_order_totals> <sales_order_invoice_totals>Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Invoice_Totals</sales_order_invoice_totals> <sales_order_creditmemo_totals>Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Creditmemo_Totals</sales_order_creditmemo_totals> </rewrite> </adminhtml> </blocks> 这些类是非常类似: class Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Totals extends Mage_Adminhtml_Block_Sales_Order_Totals { /** * Initialize order totals array * * @return Mage_Sales_Block_Order_Totals */ protected function _initTotals() { parent::_initTotals(); $amount = INSURANCE_FEE; if ($amount) { $this->addTotalBefore(new Varien_Object(array( 'code' => 'turnkeye_insurance', 'value' => $amount, 'base_value'=> $amount, 'label' => $this->helper('turnkeye_insurance')->__('Insurance'), ), array('shipping', 'tax'))); } return $this; } } class Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Creditmemo_Totals extends Mage_Adminhtml_Block_Sales_Order_Creditmemo_Totals { /** * Initialize order totals array * * @return Mage_Sales_Block_Order_Totals */ protected function _initTotals() { parent::_initTotals(); $amount = INSURANCE_FEE; if ($amount) { $this->addTotalBefore(new Varien_Object(array( 'code' => 'turnkeye_insurance', 'value' => $amount, 'base_value'=> $amount, 'label' => $this->helper('turnkeye_insurance')->__('Insurance'), ), array('shipping', 'tax'))); } return $this; } } class Turnkeye_Insurance_RW_Adminhtml_Block_Sales_Order_Invoice_Totals extends Mage_Adminhtml_Block_Sales_Order_Invoice_Totals { /** * Initialize order totals array * @return Mage_Sales_Block_Order_Totals */ protected function _initTotals() { parent::_initTotals(); $amount = INSURANCE_FEE; if ($amount) { $this->addTotalBefore(new Varien_Object(array( 'code' => 'turnkeye_insurance', 'value' => $amount, 'base_value'=> $amount, 'label' => $this->helper('turnkeye_insurance')->__('Insurance'), ), array('shipping', 'tax'))); } return $this; } } 之后,您将看到与管理领域中的”保险”值总计中的新行。您需要做相同的块体前端: ‘销售/order_totals’、’ 销售/order_invoice_totals’ 和 ‘ 销售/order_creditmemo_totals’。之后,您将看到您行费与前端和后端。 如果您使用 PayPal 付款方法,您需要将您的费添加到 PayPal 请求,在其它情况下您将看到错误总金额不是项价格的总和,您将不能以提交订单。要解决这只添加您为 ‘paypal_prepare_line_items’ 事件的观察员: <events> <paypal_prepare_line_items> <observers> <turnkeye_insurance> <type>singleton</type> <class>turnkeye_insurance/observer</class> <method>addScreenToPayPal</method> </turnkeye_insurance> </observers> </paypal_prepare_line_items> </events> 添加您的费到 PayPal 请求结果的方法是: public function addScreenToPayPal($observer) { $paypal_cart = $observer->getPaypalCart(); if ($paypal_cart && $paypal_cart->getSalesEntity()) { $amount = INSURANCE_FEE; if ($amount) { $paypal_cart->addItem(Mage::helper('turnkeye_insurance')->__('Insurance'), 1, $amount, 'insurance'); } } } 最后,您可以更改 INSURANCE_FEE 在我们的示例向您需要或更好的将其更改为与报价/订单参数的帮助器方法的任何金额。此外我们建议以存储您想要将应用于该数据库中的订单,并使用此值为 creditmemo/发票/订单的费用值。 (责任编辑:好模板) |