如何在magento 2的正面的客户编辑页面中显示客户自定义字段旧值

问题描述 投票:0回答:1

我以编程方式在magento 2中为客户添加了一些自定义字段。它们显示在注册页面中,其值可以在管理面板中检索。现在我需要在客户编辑页面中显示它们的价值。我该如何阅读自定义字段值?

这是我的代码:

Ibnab - > CustomerPut - > etc - > module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ibnab_CustomerPut" setup_version="1.0.4">
    <sequence>
        <module name="Magento_Customer"/>
    </sequence>
</module>

Ibnab - > CustomerPut - > registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Ibnab_CustomerPut',
__DIR__
);

Ibnab - > CustomerPut - > Setup - > InstallData.php

<?php
namespace Ibnab\CustomerPut\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
/**
 * Customer setup factory
 *
 * @var \Magento\Customer\Setup\CustomerSetupFactory
 */
private $customerSetupFactory;
/**
 * Init
 *
 * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
 */
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
    $this->customerSetupFactory = $customerSetupFactory;
}
/**
 * Installs DB schema for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $installer = $setup;
    $installer->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
    $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code");

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Promotion Code",
        "input"    => "text",
        "source"   => "",
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

    ));

    $promotion_code   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code");

    $promotion_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'promotion_code');
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $promotion_code->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);

    $promotion_code->save();

    $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion2_code");

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion2_code",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Promotion2 Code",
        "input"    => "text",
        "source"   => "",
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

    ));

    $promotion2_code   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion2_code");

    $promotion2_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'promotion2_code');
    $promotion2_code->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);

    $promotion2_code->save();


    $installer->endSetup();
}
}

Ibnab - > CustomerPut - > view - > frontend - > layout - > customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customerput" template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body>

Ibnab - > CustomerPut - > view - > frontend - > layout - > customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
     <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template"  template="Ibnab_CustomerPut::additionalinfocustomeredit.phtml"/>
     </referenceContainer>

    </referenceContainer>

</body>

Ibnab - > CustomerPut - > view - > frontend - > templates - > additionalinfocustomer.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br>

    <div class="field promotion_code required">
        <label for="promotion_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion_code" id="promotion_code" title="<?php /* @escapeNotVerified */ echo __('Promotion Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>

    <div class="field promotion2_code required">
        <label for="promotion2_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion2_code" id="promotion2_code" title="<?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>
</fieldset>

Ibnab - > CustomerPut - > view - > frontend - > templates - > additionalinfocustomeredit.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br>

    <div class="field promotion_code required">
        <label for="promotion_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion_code" id="promotion_code" title="<?php /* @escapeNotVerified */ echo __('Promotion Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>

    <div class="field promotion2_code required">
        <label for="promotion2_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion2_code" id="promotion2_code" title="<?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>
</fieldset>
magento2 custom-fields magento2.1
1个回答
0
投票

请更新module.xml,如Ibnab - > CustomerPut - > etc - > module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ibnab_CustomerPut" setup_version="1.0.4">
    <sequence>
        <module name="Amazon_Payment"/>
    </sequence>
</module>

现在,你的代码应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.