Remove Shipping Option if Condition is Met

蓝咒 提交于 2019-12-12 01:45:37

问题


I would like to remove the standard shipping option productmatrix_Standard from the shipping methods if a certain condition is met. I believe I need to override the following:

/**
 * One page checkout status
 *
 * @category   Mage
 * @category   Mage
 * @package    Mage_Checkout
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Checkout_Block_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Abstract
{
    protected $_rates;
    protected $_address;

    public function getShippingRates()
    {

        if (empty($this->_rates)) {
            $this->getAddress()->collectShippingRates()->save();

            $groups = $this->getAddress()->getGroupedAllShippingRates();
            /*
            if (!empty($groups)) {
                $ratesFilter = new Varien_Filter_Object_Grid();
                $ratesFilter->addFilter(Mage::app()->getStore()->getPriceFilter(), 'price');

                foreach ($groups as $code => $groupItems) {
                    $groups[$code] = $ratesFilter->filter($groupItems);
                }
            }
            */

            return $this->_rates = $groups;
        }

        return $this->_rates;
    }
}

How can I remove existing shipping methods from this collection, or empty it and re-build it manually, skipping the productmatrix_Standard option?


回答1:


You should instead override "function collectRates()" in the shipping model.
e.g. inside /app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php or /app/code/Mage/Usa/Model/Shipping/Carrier/Ups.php

All of the details about the address and cart are available at the time collectRates() is run on each carrier. This makes it ideal to filter out specific rates, or changing their names/price.




回答2:


I ended up with

<?php

    public function getShippingRates() {

        if (empty($this->_rates)) {

            $this->getAddress()->collectShippingRates()->save();

            $groups = $this->getAddress()->getGroupedAllShippingRates();

            if ($this->someCondition()) {

                $badMethods = array('productmatrix_Standard');

                // Loop through groups (productmatrix)
                // pass by reference for unset later
                foreach ($groups as $code => &$groupItems) {

                    // Lopp through methods (standards, 2day)
                    foreach($groupItems as $key => $item) {

                        // Check for bad shipping methods
                        if (in_array($item->getCode(), $badMethods)) {

                            // Remove the method from the shipping groups
                            unset($groupItems[$key]);
                        }
                    }
                }
            }

            $this->_rates = $groups;
        }

        return $this->_rates;
    }


来源:https://stackoverflow.com/questions/14817516/remove-shipping-option-if-condition-is-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!