如果符合条件,则删除运输选项

时间:2013-02-11 17:30:24

标签: php magento shipping

如果满足特定条件,我想从送货方式中删除标准送货选项productmatrix_Standard。我相信我需要覆盖以下内容:

/**
 * 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;
    }
}

如何从此集合中删除现有的送货方式,或者将其清空并手动重新构建,跳过productmatrix_Standard选项?

2 个答案:

答案 0 :(得分:2)

您应该改为在运输模型中覆盖“function collectRates()” 例如内 /app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php 要么 /app/code/Mage/Usa/Model/Shipping/Carrier/Ups.php

当每个运营商都运行collectRates()时,可以获得有关地址和购物车的所有详细信息。这样可以过滤掉特定费率或更改names/price.

答案 1 :(得分:1)

我最终得到了

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