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

时间:2012-02-24 15:46:31

标签: 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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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
*/

答案 2 :(得分:4)

我已经为已经提供的答案创建了一个函数。这将创建所有装运方法的选项组:

function getShippingMethods($_methods, $fieldId, $fieldName, $fieldClass){
    $_shippingHtml = '<select name="' . $fieldName . '" id="' . $fieldId . '" class="' . $fieldClass . '">';
    foreach($_methods as $_carrierCode => $_carrier){
        if($_method = $_carrier->getAllowedMethods())  {
            if(!$_title = Mage::getStoreConfig('carriers/' . $_carrierCode . ' /title')) {
                $_title = $_carrierCode;
            }
            $_shippingHtml .= '<optgroup label="' . $_title . '">';
            foreach($_method as $_mcode => $_m){
                $_code = $_carrierCode . '_' . $_mcode;
                $_shippingHtml .= '<option value="' . $_code . '">' . $_m . '</option>';
            }
            $_shippingHtml .= '</optgroup>';
        }
    }
    $_shippingHtml .= '</select>';
    return $_shippingHtml;
}

$_methods参数是Magento的一个对象:

$_methods = Mage::getSingleton('shipping/config')->getActiveCarriers();

因此,我们可以调用该函数并将$_methods对象传递给它,如下所示:

<?php echo getShippingMethods($_methods, 'shipping_method', 'shipping_method', 'shipping'); ?>

希望能帮助别人。