购物车价格规则 - 适用于任何方式的免费送货

时间:2015-04-18 09:51:25

标签: php magento shipping

我有一个扩展程序,提供不同的运费,此模块在管理面板中有一个选项,如果满足购物车价格规则,您可以选择免费送货方式(如果遵守购物车价格规则要求,则设置这种方法免费) 问题是我只能选择一种方法,但是在同一个国家,我提供2种不同的承运人来交付包裹(不同地区的最低价格),这是一个问题。
扩展程序会将core_config_data表中的值保存在path = carriers/premiumrate/free_method下。我不清楚magento如何访问这些信息,所以我通过SSH运行这些命令:

grep -rnw 'app' -e "carriers/premiumrate/free_method"

没有结果,所以我尝试过:

grep -rnw 'app' -e "free_method"

结果:

app/code/core/Mage/Shipping/Helper/Data.php:159:        $freeMethod = Mage::getStoreConfig('carriers/' . $arr[0] . '/free_method', $storeId);
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:56:    protected $_freeMethod = 'free_method';

下一个搜索:

grep -rnw 'app' -e "freeMethod"

结果:

app/code/core/Mage/Shipping/Helper/Data.php:159:        $freeMethod = Mage::getStoreConfig('carriers/' . $arr[0] . '/free_method', $storeId);
app/code/core/Mage/Shipping/Helper/Data.php:160:        return $freeMethod == $arr[1];
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:343:        $freeMethod = $this->getConfigData($this->_freeMethod);
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:344:        if (!$freeMethod) {
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:351:                if ($item->getMethod() == $freeMethod) {
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:363:            $this->_setFreeMethodRequest($freeMethod);
app/code/core/Mage/Shipping/Model/Carrier/Abstract.php:373:                            && $rate->getMethod() == $freeMethod

所以我打开了app/code/core/Mage/Shipping/Model/Carrier/Abstract.php,这些是我发现有趣的行(370 - 378;我在底部添加了完整的函数):

if (count($rates) > 1) {
    foreach ($rates as $rate) {
        if ($rate instanceof Mage_Shipping_Model_Rate_Result_Method
            && $rate->getMethod() == $freeMethod
        ) {
            $price = $rate->getPrice();
        }
    }
}
  • 这是我必须编辑的正确位置吗?
  • 如果我删除if条件,我会破坏我的网站吗?
  • 如果购物车规则是,我应该如何设置任何免运费 活性/有效?
  • 有没有更好的方法来找出我要编辑的内容 而不是随机的grep?

    完成功能

    protected function _updateFreeMethodQuote($request)
    {
        if ($request->getFreeMethodWeight() == $request->getPackageWeight() || !$request->hasFreeMethodWeight()) {
            return;
        }
    
        $freeMethod = $this->getConfigData($this->_freeMethod);
        if (!$freeMethod) {
            return;
        }
        $freeRateId = false;
    
        if (is_object($this->_result)) {
            foreach ($this->_result->getAllRates() as $i=>$item) {
                if ($item->getMethod() == $freeMethod) {
                    $freeRateId = $i;
                    break;
                }
            }
        }
    
        if ($freeRateId === false) {
            return;
        }
        $price = null;
        if ($request->getFreeMethodWeight() > 0) {
            $this->_setFreeMethodRequest($freeMethod);
    
            $result = $this->_getQuotes();
            if ($result && ($rates = $result->getAllRates()) && count($rates)>0) {
                if ((count($rates) == 1) && ($rates[0] instanceof Mage_Shipping_Model_Rate_Result_Method)) {
                    $price = $rates[0]->getPrice();
                }
                if (count($rates) > 1) {
                    foreach ($rates as $rate) {
                        if ($rate instanceof Mage_Shipping_Model_Rate_Result_Method
                            && $rate->getMethod() == $freeMethod
                        ) {
                            $price = $rate->getPrice();
                        }
                    }
                }
            }
        } else {
            /**
             * if we can apply free shipping for all order we should force price
             * to $0.00 for shipping with out sending second request to carrier
             */
            $price = 0;
        }
    
        /**
         * if we did not get our free shipping method in response we must use its old price
         */
        if (!is_null($price)) {
            $this->_result->getRateById($freeRateId)->setPrice($price);
        }
    }
    

0 个答案:

没有答案