了解客户是所有电子商务业务成功的关键。在 Magento 2 中,商家可以通过注册表单、帐户仪表板等中的各个字段收集客户属性。为了更好地了解您的客户,需要在客户网格中添加自定义属性。在此博客中,我们将指导您如何通过 2 个简单的步骤在 Magento 2 中创建自定义客户属性。

为您提供智能解决方案:使用Magento 2 的客户属性有效地收集客户信息。

步骤 1:创建数据补丁

Magento 的最新版本引入了一种在 Magento 2 数据库中修改数据的新方法:数据/模式补丁(更多信息在这里:Magento 文档)。

我们将实施一个数据补丁来将客户的属性添加到数据库中。

CustomerAttributePatcher首先,按照这个目录约定在你的模块目录中创建一个名为:的类:<your-module>/Setup/Patch/Data/CustomerAttributePatcher.php

注意所有数据补丁都必须在此目录中。

必须实施Magento\Framework\Setup\Patch\DataPatchInterface

<?php
namespace Magenest\Customer\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Config;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AccountPurposeCustomerAttribute implements DataPatchInterface
{
    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var ModuleDataSetupInterface
     */
    private $setup;

    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * AccountPurposeCustomerAttribute constructor.
     * @param ModuleDataSetupInterface $setup
     * @param Config $eavConfig
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $setup,
        Config $eavConfig,
        CustomerSetupFactory $customerSetupFactory
    )
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->setup = $setup;
        $this->eavConfig = $eavConfig;
    }

    /** We'll add our customer attribute here */
    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
        $attributeSetId = $customerSetup->getDefaultAttributeSetId($customerEntity->getEntityTypeId());
        $attributeGroup = $customerSetup->getDefaultAttributeGroupId($customerEntity->getEntityTypeId(), $attributeSetId);
        $customerSetup->addAttribute(Customer::ENTITY, 'new_attribute', [
            'type' => 'int',
            'input' => 'select',
            'label' => 'New Attribute',
            'required' => true,
            'default' => 0,
            'visible' => true,
            'user_defined' => true,
            'system' => false,
            'is_visible_in_grid' => true,
            'is_used_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
            'position' => 300
        ]);
        $newAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'new_attribute');
        $newAttribute->addData([
            'used_in_forms' => ['adminhtml_checkout','adminhtml_customer','customer_account_edit','customer_account_create'],
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroup
        ]);
        $newAttribute->save();
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

第二步:安装数据补丁

运行升级命令为了数据补丁采取行动:php bin/magento setup:upgrade

现在我们的客户属性已安装。

我们希望这些简单的步骤可以帮助您在 Magento 2 中添加您需要的自定义客户属性。如果您有任何问题,请在评论中提出。