Magento - 注册期间上传文件

时间:2011-09-27 13:36:42

标签: php magento custom-fields

我希望每个在我的Magento实例上注册的用户上传证明他注册了公司的证书。

我已在模板中添加了字段。但是,如何获取文件并将文件名/内容保存在客户记录中?

有没有办法扩展控制器中的功能?

3 个答案:

答案 0 :(得分:2)

这实际上更容易:

只需确保在config.xml上设置这些参数:

            'attributes' => array(
                'prooffile' => array(
                    'type'          => 'text',
                    'input'         => 'file',
                    'label'         => 'Proof file',
                    'visible'       => true,
                    'required'      => false,
                    'position'      => 100,
                    "user_defined" => false,
                ),

这会在您的管理员后端添加一个漂亮的编辑器。

答案 1 :(得分:0)

我这样做的方式:

我将文件字段添加到注册表单中:

<li class="fields">
                <div class="field">
                                <div class="input-box">
                                                <label for="prooffile"><?php echo $this->__('Proof of business registration') ?><span class="required">*</span></label><br />
                                                <input type="file" name="prooffile" id="prooffile" title="<?php echo $this->__('Proof of business registration') ?>" class="required-entry input-text" />
                                </div>
                </div>
</li>

另外,请确保将表单enctype设置为“multipart / form-data”。

之后,我创建了一个订阅“user-register-success”事件的类。 Magento内置了非常可靠的Event / Observer机制。

为此,您必须拥有自定义模块。在模块etc / config.xml中,为事件侦听器添加以下行:

    <events>
            <customer_register_success> <!-- The name of the Event -->
                    <observers>
                            <customfields> <!-- Your module name -->
                                    <type>singleton</type>
                                    <class>customfields/observer</class> <!-- The class name, that holds your callback function -->
                                    <method>handle_file_upload</method>
                            </customfields>
                    </observers>
            </customer_register_success>
    </events>

这会为事件customer_register_success注册一个事件处理程序。确保在模块Model文件夹中创建文件Observer.php:

型号/ Observer.php:

<?php

class Komola_Customfields_Model_Observer
{
        public function __construct()
        {

        }

        public function handle_file_upload($observer)
        {
                $customer = $observer->getCustomer();
                if (isset($_FILES['prooffile']['name']) && $_FILES['prooffile']['name'] != "") {
                                $uploader = new Varien_File_Uploader("prooffile");
                                $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'pdf'));
                                $uploader->setAllowRenameFiles(false);
                                $uploader->setFilesDispersion(false);
                                $path = Mage::getBaseDir("media") . DS . "customer" . DS;
                                $logoName = $customer->getId() . "." . pathinfo($_FILES['prooffile']['name'], PATHINFO_EXTENSION);
                                $uploader->save($path, $logoName);
                                $customer->setProoffile($logoName);
                                $customer->save();
                }
        }
}

这将获取上传的文件,并将文件保存在media / customer文件夹中(确保创建此文件夹并使其可写!)。它还将文件名保存在自定义客户属性中。

答案 2 :(得分:0)

在模块安装程序文件中,创建这样的属性,它将显示在客户后端中。

Magento的新版本还需要一个额外的部分(从确切的时候开始就不确定,但从Magento Community Edition 1.6开始就是如此)。

“used_in_forms”键不能直接传递给addAttribute调用的数组(不起作用)。它可能包含客户模型将接受值的表单名称,并且在保存时不会忽略它们。

已知值是这个问题的答案:Can no longer add registration fields in Magento 1.4.2.0(Folker Schellenberg的回答)

我认为这是呈现表单的控制器和操作的名称。此名称也是页面的主要布局句柄名称(例如:customer_account_edit)。

应该注意的是,前端的客户表单是基于HTML的。它不会像后端表单那样动态呈现属性的输入。这意味着如果用户输入这些属性,则需要修改模板以包含正确的输入标记(并在used_in_forms数组中添加适当的值)。

$attributeCode = "uploaded_file";
$attributeLabel = "Uploaded file";

$installer->addAttribute('customer', $attributeCode, array(
    'type' => 'text',
    'input' => 'file',
    'label' => $attributeLabel,
    'global' => true,
    'visible' => true,
    'required' => false,
    'user_defined' => false
));

// For newer versions of Magento, otherwise won't show up.
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer', $attributeCode);
$attribute->setData('used_in_forms', array('customer_account_create', 'adminhtml_customer'));
$attribute->setData('sort_order', 200);
$attribute->save();

另一种可能的类型是'image',它完全呈现为'file',除了它在预览框(一个小的)中显示图像。也许对顾客合影?

另外,值得注意的是,这是客户表单特有的(处理此类属性的类是:Mage_Adminhtml_Block_Customer_Form_Element_File和Mage_Adminhtml_Block_Customer_Form_Element_Image),因此如果没有自定义工作,这将无法在产品属性中使用。

希望这有帮助!

相关问题