【问题标题】:Magento Save Selected CountryMagento 保存选定的国家
【发布时间】:2012-07-18 10:38:51
【问题描述】:

我想在客户帐户注册页面中添加另一个国家/地区下拉列表。我试过了

<?php echo $this->getCountryHtmlSelect() ?>

但这不显示下拉菜单。我试过的另一个是

<select name="partner_country" id="partner_country">
    <option value=''>– Please Select –</option>
        <?php foreach($_countries as $_country): ?>
            <option value="<?php echo $_country->getId() ?>"><?php echo $_country->getName() ?></option>
        <?php endforeach; ?>
</select>

这个显示国家列表,但是选择的国家没有出现在后台客户信息页面。

我知道getCountryHtmlSelect() 呈现国家/地区的下拉菜单。我是否在我的模块中创建了类似的方法来保存所选国家/地区?

更新

在通过设置脚本添加此属性时,我已经创建了一个源模型,

$installer->addAttribute('customer_address','partner_country_id',array(
        'type'               => 'varchar',
        'label'              => 'Partner Country',
        'input'              => 'select',
        'source'             => 'wholesale/attribute_source_partnercountry',
        'global' => 1,
        'visible' => 1,
        'required' => 0,
        'visible_on_front' => 1,
        'sort_order'=>220
));

源模型

class Company_Wholesale_Model_Attribute_Source_Partnercountry extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
    public function getAllOptions()
    {
        if (!$this->_options) {
            $this->_options = Mage::getResourceModel('directory/country_collection')
                ->loadByStore($this->getAttribute()->getStoreId())->toOptionArray();
        }
        return $this->_options;
    }

}

config.xml

<config>
    <global>
        <resources>
            <wholesale_setup>
                <setup>
                    <module>Company_Wholesale</module>
                    <class>Company_Wholesale_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </wholesale_setup>
        </resources>
    </global>
</config>

【问题讨论】:

    标签: magento


    【解决方案1】:

    您遇到的问题与您创建的属性类型有关。为了在管理员中选择您的自定义属性,您需要使用“select”和“source_model”类型创建/更新它。为此需要使用客户模块设置模型,因此您需要在设置资源的模块配置中指定它:

    <config>
       <global>
           <resources>
               <your_module_setup>
                   <setup>
                      <class>Mage_Customer_Model_Resource_Setup</class>
                      <module>Your_Module</module>
                   </setup>
               </your_module_setup>
           </resources>
        </global>
    </config>
    

    在您的设置文件中,您需要创建/修改您的属性。 (如果属性存在,当前代码 sn -p 将修改它,而不是创建)。

    <?php
    
    $this->startSetup();
    $this->addAttribute('customer', 'partner_country' , array(
         'type'   => 'varchar',
         'label'  => 'Partner Country',
         'input'  => 'select',
         'source' => 'customer/entity_address_attribute_source_country'
    ));
    $this->endSetup();
    

    更新:

    现在我收到了您的问题,您尚未将属性添加到客户创建帐户表单中,因此您的属性在帐户创建过程中从设置为客户模型的数据中过滤掉了。

    因此,您只需为应该可以保存属性的表单指定客户属性。

    目前有这样的表格可用:

    • customer_account_create - 注册表格
    • customer_account_edit - 更改帐户详细信息表单
    • checkout_register - 在结帐时注册新帐户

    要将您的自定义属性添加到表单,只需再创建一个设置脚本,将带有表单代码和属性 ID 的记录添加到名为 customer/form_attribute 的表中:

    <?php
    $this->startSetup();
    $attributeId = $this->getAttributeId('customer', 'partner_country_id');
    $data = array(
        array('attribute_id' => $attributeId, 'form_code' => 'customer_account_create'),
        array('attribute_id' => $attributeId, 'form_code' => 'customer_account_edit'),
        array('attribute_id' => $attributeId, 'form_code' => 'checkout_register')
    );
    $this->getConnection()->insertMultiple($this->getTable('customer/form_attribute'), $data);
    $this->endSetup();
    

    只需选择退出您不需要的表单。

    【讨论】:

    • 感谢您的指点。但我已经创建了您所说的内容。也更新了我的问题。
    • 也感谢您的意见
    【解决方案2】:

    如果你想使用getCountryHtmlSelect(),那么你应该给它一些参数,以便它可以应用于你的属性,而不是默认country。对于注册表单,它可以给出如下内容:

    echo $this->getCountryHtmlSelect($this->getFormData()->getPartnerCountryId(), 'partner_country_id', 'partner_country', $this->__('Partner Country'))
    

    在第二个示例中,您在选择名称中使用了partner_country,而您创建了一个partner_country_id 属性。

    【讨论】:

    • 感谢@bImage 的投入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2022-11-04
    相关资源
    最近更新 更多