Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

php - Programmatically retrieve list of all shipping methods

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.

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)


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.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...