douh9817 2015-01-21 09:40
浏览 50
已采纳

在magento结账时如何在运输方式中添加额外的价格

Hello friends i want to add 10% of shipping price in magento shipping method during checkout :- i follow this tutorial but it's not work for me :- http://www.blog.magepsycho.com/change-shipping-price-handling-fee-fly-magento/ and my code is here :- Config.xml :-

<?xml version="1.0"?>
<config>
  <modules>
    <Ab_Extrashipcost>
      <version>0.1.0</version>
    </Ab_Extrashipcost>
  </modules>
  <global>
  <models>
            <extrashipcost>
            <class>Ab_Extrashipcost_Model</class>
            </extrashipcost>
        </models>

  <events>
    <sales_quote_collect_totals_after>
        <observers>
            <ab_extrashipcost>
                <type>singleton</type>
                <class>extrashipcost/observer</class>
                <method>salesQuoteCollectTotalsBefore</method>
            </ab_extrashipcost>
        </observers>
    </sales_quote_collect_totals_after>
</events>
      </global>
</config>   

Observer file:-

<?php
class Ab_Extrashipcost_Model_Observer
{
 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 == 'flatrate_flatrate'){
                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');
                }
            }
        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doudizhi947129 2015-01-21 10:07
    关注

    Your config.xml code should be :-

    <?xml version="1.0"?>
    <config>
      <modules>
        <Ab_Extrashipcost>
          <version>0.1.0</version>
        </Ab_Extrashipcost>
      </modules>
      <global>
      <models>
                <extrashipcost>
                <class>Ab_Extrashipcost_Model</class>
                </extrashipcost>
            </models>
          </global>
     <frontend>
      <events>
        <sales_quote_collect_totals_before>
            <observers>
                <ab_extrashipcost>
                    <type>singleton</type>
                    <class>extrashipcost/observer</class>
                    <method>salesQuoteCollectTotalsBefore</method>
                </ab_extrashipcost>
            </observers>
        </sales_quote_collect_totals_before>
    </events>
    </frontend>
    </config> 
    

    And Observer.php code should be :-

    <?php
    class Ab_Extrashipcost_Model_Observer
    {
     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 == 'flatrate'){
                    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');
                   }
                }
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用