我正在使用Magento 1.11.2.0版本,我想为客户添加选项,以便在我的帐户页面上传他们的图像.
我在管理员中添加了一个新的图像文件类型的客户属性,这很好用.但它只有图像的最大图像宽度,最大图像高度选项.我想添加另外两个输入,这样我就可以指定宽度和高度,以便在上传头像时调整图像大小.
有没有办法做到这一点?我也很好奇什么模块/类用于客户的上传图像属性.
这个过程有几个步骤.首先,您需要创建一个属性并将其添加到默认组和属性集.这里有一些代码可以添加到设置脚本中:
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$installer->addAttribute('customer', 'avatar', array(
'label' => 'Avatar Image',
'input' => 'file',
'type' => 'varchar',
'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
'required' => 0,
'user_defined' => 1,
));
$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, 'avatar', 0);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'avatar');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();
$installer->endSetup();
Run Code Online (Sandbox Code Playgroud)
关键是要设置input为file.这会导致系统在后端显示文件上传器,并在处理表单时查找上载的文件.的type是varchar,因为一个varchar属性用于存储文件的名称.
创建属性后,您需要向persistent/customer/form/register.phtml模板添加输入元素.一些示例代码如下:
<label for="avatar"><?php echo $this->__('Avatar') ?></label>
<div class="input-box">
<input type="file" name="avatar" title="<?php echo $this->__('Avatar') ?>" id="avatar" class="input-text" />
</div>
Run Code Online (Sandbox Code Playgroud)
这里要注意的主要事项是字段的id和名称应该与属性的代码相同.另外,不要忘记添加 enctype="multipart/form-data"到<form>标签.
这将允许用户在注册时上传头像图像.随后,当显示图像时,您需要将其大小调整为适合您网站的尺寸.代码Magento图像助手设计用于处理产品图像,但是这篇博文将向您展示如何创建可以调整图像大小的辅助函数,以及缓存已调整大小的文件.我之前使用过这些说明来调整类别图像的大小并且效果很好.