Atribut Pelanggan tidak ditampilkan dalam formulir akun pelanggan adminhtml di Magento Enterprise 2.2.0


11

Saya telah membuat modul "Wgac_Subscription". Saya ingin membuat atribut pelanggan khusus. Itu terdaftar di admin seperti yang ditunjukkan pada gambar di bawah, tetapi tidak muncul dalam bentuk adminhtml pelanggan.

Wgac / Berlangganan / Pengaturan / InstallData.php

<?php
namespace Wgac\Subscription\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;



    public function __construct(
        EavSetupFactory $eavSetupFactory,
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    )
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;

    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    /*
        **
        * Create  Customer Attribute "customer_chargify_id"
        ** ==== START ====
        */

         /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'customer_chargify_id', [
            'type' => 'varchar',
            'label' => 'Customer Chargify Id',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            "unique"  => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_chargify_id')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);

        $attribute->save();

        /*        
        *   === END ===       
        */



    }
}

Wgac / Berlangganan / view / base / ui_component / customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_con
figuration.xsd">
    <fieldset name="customer">
        <field name="customer_chargify_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement"
                    xsi:type="string">input</item>
                    <item name="source"
                    xsi:type="string">customer</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

masukkan deskripsi gambar di sini

Mohon saran saya jika saya kehilangan sesuatu.


1
Saya memiliki masalah yang sama. Saya tidak tahu apa yang menyebabkan ini. Apakah Anda dapat menyelesaikannya dan menampilkan atribut pada akun pelanggan di admin?
Radu

Jawaban:


4

Nilai 'used_in_forms' harus ['adminhtml_customer', 'customer_account_edit'] . Jika Anda tidak ingin atribut ini ditampilkan untuk seorang pelanggan, Anda harus menetapkan terlihat = salah . Anda dapat memperbarui InstallData Anda seperti di bawah ini:

$customerSetup->removeAttribute(Customer::ENTITY, 'customer_chargify_id');
$customerSetup->addAttribute(Customer::ENTITY, 'customer_chargify_id', [
            'type' => 'varchar',
            'label' => 'Customer Chargify Id',
            'input' => 'text',
            'required' => false,
            'visible' => false,
            "unique" => true,
            'user_defined' => true,
            'position' => 999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_chargify_id')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'],
            ]);

Dan pastikan Anda telah menghapus YourVendor_YourModule di tabel setup_module jika Anda ingin menjalankan kembali skrip InstallData

Semoga ini bisa membantu Anda

Salam


1
Satu koreksi: Agar atribut terlihat oleh admin tetapi tidak di frontend, Anda perlu customer_eav_attribute.is_visible=1( visiblebenar) tetapi eav_attribute.is_user_defined=0( user_definedsalah).
Ryan Hoerr
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.