Magento2 不为客户保存自定义属性

问题描述

我在 Magento 2 后端为客户添加一个新字段,该字段可见。我使用 mage2gen 创建了这个,但无论我怎么尝试,当我在后端更新它时,新字段都没有被保存。

由于该字段在后端可见,并且视图和 api 都按预期工作,我不知道为什么它不工作。在实际补丁下方,我添加了字段和 xml 配置以使其可见。

我尝试将其添加属性集中,但据我所知,该字段已正确注册并且也在 customer_eav_attribute

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <fieldset name="general">
        <field formElement="text" name="external_reference" sortOrder="20">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">external_reference</item>
                </item>
            </argument>
            <settings>
                <dataType>text</dataType>
                <label translate="true">External reference</label>
                <dataScope>external_reference</dataScope>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">false</rule>
                </validation>
            </settings>
        </field>
    </fieldset>
</form>
<?PHP

declare(strict_types=1);

namespace Me\DirectLogin\Setup\Patch\Data;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class AddExternalReferenceCustomerAttribute implements DataPatchInterface,PatchRevertableInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,'external_reference',[
                'type' => 'varchar','label' => 'External reference','input' => 'text','source' => '','frontend' => '','required' => false,'backend' => '','default' => null,'user_defined' => true,'unique' => true,'group' => 'General','system' => 0,'order' => 1000,'note' => 'Reference to external managed user','is_used_in_grid' => false,'is_visible_in_grid' => true,'is_filterable_in_grid' => true,'is_searchable_in_grid' => true,]
        );

        $eavSetup->addAttributetoSet(
            CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,null,'external_reference'
        );

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY,'external_reference');

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [

        ];
    }
}

希望有人能指出我的解决方

最好的, 皮姆

解决方法

我已经检查过您的代码与实际的 mage2gen 代码略有不同。就像您没有为“used_in_forms”添加数据一样。您可以尝试使用以下代码创建自定义客户属性(如果您想使用 mage2gen
https://mage2gen.com/snippets/customerattribute/

或者您可以尝试使用安装方法。检查以下 Mageplaza 博客网址:
https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatically.html

,

尝试在 Setup/Patch/Data 路径中使用以下内容

<?php

namespace Vendor\Customer\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddCustomerRefId implements DataPatchInterface
{
    const EXTERNAL_REFERENCE = "external_reference";

    /**
     * @var ModuleDataSetupInterface
     */
    protected $moduleDataSetup;

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

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

    /**
     * AddCustomerPhoneNumberAttribute constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,CustomerSetupFactory $customerSetupFactory,AttributeSetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $customerSetup->removeAttribute(
            Customer::ENTITY,self::EXTERNAL_REFERENCE
        );

        $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
            Customer::ENTITY,self::EXTERNAL_REFERENCE,[
                'type' => 'varchar','label' => 'External Reference','required' => false,'user_defined' => true,'sort_order' => 1000,'position' => 1000,'default' => 0,'system' => 0
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(
            Customer::ENTITY,self::EXTERNAL_REFERENCE
        );

        $attribute->addData(
            [
                'attribute_set_id' => $attributeSetId,'attribute_group_id' => $attributeGroupId,'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit']
            ]
        );

        $attribute->save();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...