1

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

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

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

4

3 回答 3

2

这实际上更容易:

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

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

这会在您的管理后端添加一个不错的编辑器。

于 2011-10-13T17:03:38.327 回答
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”。

之后,我创建了一个订阅“用户注册成功”事件的类。Magento 内置了一个非常可靠的事件/观察者机制。

为此,您必须有一个自定义模块。在模块 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:

模型/观察者.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 中(确保创建此文件夹并使其可写!)。它还将文件名保存在自定义客户属性中。

于 2011-09-30T09:40:01.147 回答
0

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

较新版本的 Magento 需要一个额外的部分(不确定从何时开始,但从 Magento 社区版 1.6 及更高版本开始确实如此)。

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

已知值在这个问题的答案中:Can no longer add registration fields in Magento 1.4.2.0 (The answer by 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();

另一种可能的类型是“图像”,它完全呈现为“文件”,除了它在预览框中显示图像(一个小框)。也许对客户照片有好处?

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

希望这可以帮助 !

于 2012-05-17T12:52:52.433 回答