php 如果满足条件,则删除发货选项

woobm2wo  于 2023-01-16  发布在  PHP
关注(0)|答案(2)|浏览(122)

如果满足特定条件,我希望从发货方式中删除标准发货选项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选项?

kkbh8khc

kkbh8khc1#

相反,您应该覆盖发货模型中的“function collectRates()“。
例如/app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php/app/code/Mage/Usa/Model/Shipping/Carrier/Ups.php内部
在每个运营商上运行collectRates()时,有关地址和购物车的所有详细信息都可用。这使得筛选特定费率或更改其names/price.非常理想

ego6inou

ego6inou2#

最后我得到了

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

相关问题