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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?