我正在编写一个快速而肮脏的模块,以根据购物车中的产品限制运输方法。例如,如果客户添加了食物,我只希望选择隔夜运输方式。一些商业扩展只是过犹不及,而且有更多我需要的功能。
每个产品都有一个名为"运输类别"的下拉属性。管理员将能够在后台创建这些运输类。他们会给它一个名称,并选择允许的方法。
当需要获取运输报价时,我们将只显示基于运输类的允许方法。
我的主要问题是:如何检索所有运输方法的列表,供管理员在创建这些运输类时选择
第二个问题是,在Mage_Sales_Model_Quote_Address::requestShippingRates中过滤允许的方法有意义吗?(我当然会推翻这种方法)
编辑:
多亏了@BrianVPS,我才想出了下面的代码。它使用optgroup显示运营商的所有单独方法。适用于多选!不过,我不认为它会检查这些方法是否真的启用了。
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中为我编写的运输扩展编写的一块代码。希望这就是你想要的。
至于你的第二个问题,不确定。。。。
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;
}
根据@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
*/
我已经从已经提供的答案中创建了一个函数。这创建了一个所有运输方法的选项组:
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'); ?>
希望这能帮助其他人。