以编程方式检索所有送货方式的列表

Col*_*ell 17 php magento

我正在编写一个快速而肮脏的模块来限制基于购物车中产品的运输方式.例如,如果客户添加食物,我只想要选择隔夜送货方式.一些商业扩展只是矫枉过正,并且拥有我需要的更多功能.

每个产品都有一个名为"Shipping Class"的下拉属性.管理员将能够在后端创建这些Shipping Classes.他们会给它一个名字,并选择允许哪些方法.

在获得运费报价时,我们只会显示基于运输类的允许方法.

我的主要问题是:如何在创建这些运输类时检索管理员可以选择的所有运送方式的列表?

作为次要问题,在Mage_Sales_Model_Quote_Address :: requestShippingRates中对允许的方法进行过滤是否有意义?(当然,我将重写这种方法)


编辑:

感谢@BrianVPS,我能够提出下面的代码.它使用optgroups显示运营商的所有单独方法.多选用效果很好!我不认为它会检查方法是否实际启用了.

public function getAllShippingMethods()
{
    $methods = Mage::getSingleton('shipping/config')->getActiveCarriers();

    $options = array();

    foreach($methods as $_ccode => $_carrier)
    {
        $_methodOptions = array();
        if($_methods = $_carrier->getAllowedMethods())
        {
            foreach($_methods as $_mcode => $_method)
            {
                $_code = $_ccode . '_' . $_mcode;
                $_methodOptions[] = array('value' => $_code, 'label' => $_method);
            }

            if(!$_title = Mage::getStoreConfig("carriers/$_ccode/title"))
                $_title = $_ccode;

            $options[] = array('value' => $_methodOptions, 'label' => $_title);
        }
    }

    return $options;
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*VPS 24

这是我在source_model中为我编写的送货延期提供的代码块.希望这是你正在寻找的.

......至于你的第二个问题,不确定......

public function toOptionArray($isMultiSelect = false)
{
    $methods = Mage::getSingleton('shipping/config')->getActiveCarriers();

    $options = array();

    foreach($methods as $_code => $_method)
    {
        if(!$_title = Mage::getStoreConfig("carriers/$_code/title"))
            $_title = $_code;

        $options[] = array('value' => $_code, 'label' => $_title . " ($_code)");
    }

    if($isMultiSelect)
    {
        array_unshift($options, array('value'=>'', 'label'=> Mage::helper('adminhtml')->__('--Please Select--')));
    }

    return $options;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这真的很有帮助!你的解决方案只返回了运营商,但获取方法是微不足道的 - 只需要调用` - > getAllowedMethods()`.我编辑了我的问题以包含我的代码,以防任何人遇到这个问题并且需要它.再次感谢您指出我正确的方向! (2认同)

Chr*_*s K 9

以@BrianVPS的答案为例,我正在使用下面的代码段(显示结构)来帮助我的情况,我需要一个简单的人工标签来自运输代码.

$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$shipping = array();
foreach($methods as $_ccode => $_carrier) {
    if($_methods = $_carrier->getAllowedMethods())  {
        if(!$_title = Mage::getStoreConfig("carriers/$_ccode/title"))
            $_title = $_ccode;
        foreach($_methods as $_mcode => $_method)   {
            $_code = $_ccode . '_' . $_mcode;
            $shipping[$_code]=array('title' => $_method,'carrier' => $_title);
        }
    }
}
echo "\n";print_r($shipping);
/*
[flatrate_flatrate] => Array
        [title] => Will-call
        [carrier] => Pickup At Ca Cycleworks
[freeshipping_freeshipping] => Array
        [title] => Economy
        [carrier] => Free Ground Shipping
[ups_11] => Array
        [title] => UPS Standard
        [carrier] => United Parcel Service
[ups_12] => Array
        [title] => UPS Three-Day Select
        [carrier] => United Parcel Service
[ups_54] => Array
        [title] => UPS Worldwide Express Plus
        [carrier] => United Parcel Service
[ups_65] => Array
        [title] => UPS Saver
        [carrier] => United Parcel Service
[ups_01] => Array
        [title] => UPS Next Day Air
        [carrier] => United Parcel Service
[ups_02] => Array
        [title] => UPS Second Day Air
        [carrier] => United Parcel Service
[ups_03] => Array
        [title] => UPS Ground
        [carrier] => United Parcel Service
[ups_07] => Array
        [title] => UPS Worldwide Express
        [carrier] => United Parcel Service
[ups_08] => Array
        [title] => UPS Worldwide Expedited
        [carrier] => United Parcel Service
[customshippingrate_customshippingrate] => Array
        [title] => Custom Shipping Rate
        [carrier] => Custom Shipping Rate
*/
Run Code Online (Sandbox Code Playgroud)