以编程方式设置自定义客户属性值Magento 2

4bbkushb  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(171)

我正在导入一些具有以下条件的客户:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $customerFactory = $objectManager->create('\Magento\Customer\Model\CustomerFactory');

        $customer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail('customrr@custom.com');

        try {
            if(!empty($customer->getData('email')))
            {
                $customer->setAttr1(1); // Attr1 = Name of the custom Attribute 
                $customer->setAttr2(2); // Attr2 = Name of the custom Attribute 
            }
            else
            {
                $customer = $customerFactory->create()->setWebsiteId(1);
            }

            $customer->setLastname("Lastname");

            $customer->setFirstname("Firsty");

            .....

            $customer->save();

客户的所有标准属性都已正确保存,但我的新属性无论如何都不会保存。我还尝试过:

$customer->setCustomAttribute('Attr1','value');

但这个也不管用。
自定义属性在Magentos 2后台办公室中显示正确,如果手动创建客户,值也保存正确。

erhoui1w

erhoui1w1#

您是否尝试过:

$customer-> setData('Attr1','value');

别忘了保存和记录信息:

try {
    $customer->save();
} catch (\Exception $e) {
    // log exception so you can debug the issue if there is one
}
vojdkbi0

vojdkbi02#

<?php

namespace Custom\Module\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface 
{
    private $eavSetupFactory;

    public function __construct(
        EavSetupFactory $eavSetupFactory,
        Config $eavConfig
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

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

        $eavSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_attribute', [
                'label' => 'Label',
                'system' => 0,
                'position' => 720,
                'sort_order' => 720,
                'visible' => true,
                'note' => '',
                'type' => 'int',
                'input' => 'boolean',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'backend' => \Custom\Module\Model\Customer\Attribute\Backend\DoWHatEver::class,
            ]
        );

        $attribute = $this->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_attribute');
        $attribute->addData([
                'is_user_defined' => 1,
                'is_required' => 0,
                'default_value' => 0,
                'used_in_forms', ['adminhtml_customer']
            ])->save();
    }

    public function getEavConfig() 
    {
        return $this->eavConfig;
    }
}

相关问题