如何在Magento中向配送方式添加费用

如何在Magento中向配送方式添加费用,magento,payment,Magento,Payment,我遵循了本教程中创建付款方法的步骤()一切正常,但我需要一个附加功能 如果选择了付款方式,则在总额中额外收取6%的费用 我也使用这个模块,但我需要2个条件。这就是为什么我创建了新的付款方式 第一种付款方式-6%费用 第二种付款方式-2%费用 提前谢谢 您很可能只想创建一个观察者来做您需要的事情: 您需要环顾四周,寻找合适的观察者事件,但下面是一个观察者方法示例: public function updateShippingAmount( $observer ) { $MyPayme

我遵循了本教程中创建付款方法的步骤()一切正常,但我需要一个附加功能

  • 如果选择了付款方式,则在总额中额外收取6%的费用
我也使用这个模块,但我需要2个条件。这就是为什么我创建了新的付款方式

  • 第一种付款方式-6%费用
  • 第二种付款方式-2%费用

提前谢谢

您很可能只想创建一个观察者来做您需要的事情:

您需要环顾四周,寻找合适的观察者事件,但下面是一个观察者方法示例:

public function updateShippingAmount( $observer )
{
   $MyPaymentMethod = Mage::getSingleton('namespace/mypaymentmethod');

   $order = $observer->getEvent()->getOrder();
   $payment = $order->getPayment()->getData();

   if( $payment['method'] == $MyPaymentMethod->getCode() )
   {
       $shipping_amount =  $order->getShippingAmount();
       $order->setShippingAmount( $shipping_amount + $MyPaymentMethod->getPostHandlingCost() );
   }
}
摘自本文:

更多关于如何创建观察者的阅读:


最近,我有相同的需求,我通过实现事件观察者方法解决了这个问题。
事实上,您可以在任何条件下,通过在
之前执行名为
sales\u quote\u collect\u totals\u的事件,将任何额外的运输成本添加到任何运输方法中
观察者模型方法(尽管是伪代码)如下所示:

public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
    {
        /**@var Mage_Sales_Model_Quote $quote */
        $quote = $observer->getQuote();
        $someConditions = true; //this can be any condition based on your requirements
        $newHandlingFee = 15;
        $store    = Mage::app()>getStore($quote>getStoreId());
        $carriers = Mage::getStoreConfig('carriers', $store);
        foreach ($carriers as $carrierCode => $carrierConfig) {
            if($carrierCode == 'fedex'){
                if($someConditions){
                    Mage::log('Handling Fee(Before):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
                    $store->setConfig("carriers/{$carrierCode}/handling_type", 'F'); #F - Fixed, P - Percentage                  
                    $store->setConfig("carriers/{$carrierCode}/handling_fee", $newHandlingFee);

                    ###If you want to set the price instead of handling fee you can simply use as:
                    #$store->setConfig("carriers/{$carrierCode}/price", $newPrice);

                    Mage::log('Handling Fee(After):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
                }
            }
        }
    }

通过以这种方式设置store config值,您是否注意到任何副作用?