php 以编程方式检索所有运输方式的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9433627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:48:56  来源:igfitidea点击:

Programmatically retrieve list of all shipping methods

phpmagento

提问by Colin O'Dell

I'm writing a quick-and-dirty module to restrict shipping methods based on products in the cart. For example, if the customer adds food, I only want overnight shipping methods to be selected. Some of the commercial extensions are just overkill and have way more functionality that I need.

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

Each product will have a dropdown attribute called "Shipping Class". The admin will be able to create these Shipping Classes in the backend. They will give it a name and choose which methods are allowed.

每个产品都有一个名为“Shipping Class”的下拉属性。管理员将能够在后端创建这些运输类。他们会给它一个名字并选择允许使用哪些方法。

When it comes time to get shipping quotes, we'll only show allowed methods based on the Shipping Class.

当需要获取运输报价时,我们将仅根据运输类别显示允许的方法。

My main question is: how can I retrieve a list of all the shipping methods for the admin to select from when creating these shipping classes?

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

And as a secondary question, does it make sense to do the filtering of allowed methods inside of Mage_Sales_Model_Quote_Address::requestShippingRates? (I will be overriding this method of course)

作为第二个问题,在 Mage_Sales_Model_Quote_Address::requestShippingRates 中过滤允许的方法是否有意义?(我当然会覆盖这个方法)



EDIT:

编辑:

Thanks to @BrianVPS, I was able to come up with the code below. It displays all individual methods from the carriers using optgroups. Works great with multiselect! I don't think it checks if the methods are actually enabled though.

感谢@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;
}

回答by BrianVPS

Here is a block of code I have in a source_modelfor a shipping extension I wrote. Hopefully this is what you're looking for.

这是我在source_model中的一段代码,用于我编写的运输扩展。希望这就是你正在寻找的。

...as for your second question, not sure....

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

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

回答by Chris K

Taking @BrianVPS's answer, I'm using the code segment below (with structure shown) to help in my situation, where I wanted a simple human label from the shipping code.

以@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
*/

回答by Jongosi

I've created a function for this out of the answers already provided. This creates an option group of all the shipping methods:

我已经根据已经提供的答案为此创建了一个函数。这将创建一个包含所有运输方式的选项组:

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

The $_methodsargument is an object from Magento:

$_methods参数是从Magento的一个目的:

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

So, we can call the function and pass the $_methodsobject to it as follows:

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

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

Hope that helps someone else.

希望能帮助别人。