magento2 添加客户属性

magento2 adding customer attributes

使用 Magento 2.3.0 每当试图挽救客户时,我都会收到错误消息,提示需要新创建的属性,即使我设置了它们的值也是如此。

etc/extend_attributes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="customershipping_enabled" type="string" />
        <attribute code="customershipping_price" type="string" />
    </extension_attributes>
</config>

Setup/InstallData.php

<?php
namespace <vendor>\<module_name>\Setup;

use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

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

class InstallData implements InstallDataInterface {
    private $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $customerSetup =$this->customerSetupFactory->create(['setup'=>$setup]);

        $setup->startSetup();

        $customerSetup->addAttribute('customer', 'customershipping_enabled', [
            'label'=>'Customer Shipping Enabled',
            'type' => 'int',
            'input' => 'select',
            'source' => Boolean::class,
            'required'=>true,
            'visible'=>true,
            'default' => 0,
            'position' => 198,
        ]);

        $customerSetup->addAttribute('customer', 'customershipping_price', [
            'label'=>'Customer Shipping Price',
            'type'=>'decimal',
            'input' => 'text',
            'required'=>true,
            'visible'=>true,
            'default' => 0,
            'position' => 199,
        ]);

        $enabledAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customershipping_enabled');
        $enabledAttribute->setData('used_in_forms', ['adminhtml_customer']);
        $enabledAttribute->save();

        $priceAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'customershipping_price');
        $priceAttribute->setData('used_in_forms', ['adminhtml_customer']);
        $priceAttribute->save();

        $setup->endSetup();
    }
}

我已经阅读了很多这方面的教程和文档,我相信这应该可以正常工作,我是不是遗漏了什么? 每当我尝试添加新客户或更新现有客户时,它都会说这 2 个属性是必需值,保存失败。

看起来也与此相同 post: mage2gen.com/snippets/customerattribute

我最近遇到了类似的问题,请尝试将其添加到 'used_in_forms'。

您可能必须删除该属性并重新安装它:

'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']

编辑

哦,我认为这应该可以解决问题,刚刚检查了我的 installData 和 upgradeData 脚本,它们都有 system => 0。加进去就行了。

    $customerSetup->addAttribute('customer', 'customershipping_enabled', [
        'label'=>'Customer Shipping Enabled',
        'type' => 'int',
        'input' => 'select',
        'source' => Boolean::class,
        'required'=>true,
        'visible'=>true,
        'default' => 0,
        'position' => 198,
        'system' => 0
    ]);

将与此问题相关:

https://apiworks.net/magento2/magento-2-is-not-saving-the-customer-attribute/

The function getCustomAttributesMetadata is looping through all EAV attributes and checking if the attribute is marked as “is_system” inside the “customer_eav_attribute” table, which was the case with my custom attribute.

Solution:

By default, Magento flagged my custom attribute as is_system = 1, so I just needed to add ‘system’ => false in my upgrade script and execute it again (after I removed the original attribute directly from the database. ).

此问题的根本原因是 magento 2 的设计行为。

如果将自定义属性设置为必需属性,则必须将其配置为显示在店面和所有表单中。

如果您希望仅在某些特定表单上需要自定义属性,则应使用扩展属性而不是 'required'=>false。

扩展属性用于扩展自定义属性的功能。

你只需要更换

'required'=>真,

'required'=>假,

详情请参考link: Click here