以编程方式检索所有运输方式的列表 [英] Programmatically retrieve list of all shipping methods

查看:56
本文介绍了以编程方式检索所有运输方式的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个快速处理模块,以根据购物车中的产品限制运输方式.例如,如果客户增加了食物,我只想要一夜的送货方式来进行选择.一些商业扩展只是矫kill过正,并拥有我需要的更多功能.

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.

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

和作为次要问题,是否有意义做的允许的方法过滤Mage_Sales_Model_Quote_Address :: requestShippingRates内? (I将被重写当然此方法)

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)

由于@BrianVPS,我能够想出下面的代码.它显示使用optgroups运营商的所有个人的方法.与multiselect一起使用效果很好!我认为它不会检查这些方法是否真正启用.

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

推荐答案

下面是一个代码块我有一个的 source_model 作为一个航运扩展我写的.希望这是你在找什么.

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

这篇关于以编程方式检索所有运输方式的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆