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

时间:2015-01-21 09:40:46

标签: php mysql magento

朋友您好,我希望在结账时加入magento运费方式的10%运费: - 我按照本教程,但它对我不起作用: - http://www.blog.magepsycho.com/change-shipping-price-handling-fee-fly-magento/ 我的代码在这里: - 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>   

观察员文件: -

<?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 个答案:

答案 0 :(得分:0)

确保将模块配置文件放在app / etc / modules文件夹下。

答案 1 :(得分:0)

您的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>
      </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> 

Observer.php代码应为: -

<?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');
               }
            }
        }
    }
}
?>