如何根据在magento产品表单中选择的国家/地区添加状态下拉列表

Sun*_*nel 2 magento magento-1.7

嗨我想根据国家选择在管理员的magento产品中显示状态下拉,任何人请知道如何做到这一点.我用Google搜索但是徒劳无功.

Man*_*nju 7

Sunel.please检查它可以帮助你的休闲解决方案.

打开您的表单,Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php然后在下面的字段中添加

$country = $fieldset->addField('country', 'select', array(
            'name'  => 'country',
            'label'     => 'Country',
            'values'    => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),
            'onchange' => 'getstate(this)',
        ));

$fieldset->addField('state', 'select', array(
            'name'  => 'state',
            'label'     => 'State',
            'values'    => Mage::getModel('modulename/modulename')->getstate('AU'),
        ));

         /*
         * Add Ajax to the Country select box html output
         */
        $country->setAfterElementHtml("<script type=\"text/javascript\">
            function getstate(selectElement){
                var reloadurl = '". $this->getUrl('modulename/adminhtml_modulename/state') . "country/' + selectElement.value;
                new Ajax.Request(reloadurl, {
                    method: 'get',
                    onLoading: function (stateform) {
                        $('state').update('Searching...');
                    },
                    onComplete: function(stateform) {
                        $('state').update(stateform.responseText);
                    }
                });
            }
        </script>");
Run Code Online (Sandbox Code Playgroud)

现在在modulenamecontroller.php文件中创建状态操作,就像这样

public function stateAction() {
    $countrycode = $this->getRequest()->getParam('country');
    $state = "<option value=''>Please Select</option>";
    if ($countrycode != '') {
        $statearray = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter($countrycode)->load();
        foreach ($statearray as $_state) {
            $state .= "<option value='" . $_state->getCode() . "'>" .  $_state->getDefaultName() . "</option>";
        }
    }
    echo $state;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.