如何在magento 2中保存客户自定义属性

时间:2015-08-22 06:29:38

标签: magento2

我已在现有客户实体的扩展期间通过安装架构成功安装了新的自定义属性。

我的问题是每当我试图保存客户时,它都会给我以下错误。

  

致命错误:在第236行的/var/www/html/magento2/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php中的非对象上调用成员函数get()

我试图通过以下代码保存客户属性:

$customerData[\Magento\Framework\Api\CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] = [\Magento\Framework\Api\AttributeInterface::ATTRIBUTE_CODE=>'md_customer_profile_id',\Magento\Framework\Api\AttributeInterface::VALUE=>$customerProfileId];
$this->dataObjectHelper->populateWithArray($customerObject, $customerData, '\Magento\Customer\Api\Data\CustomerInterface');
$this->customerRepository->save($customerObject);

1 个答案:

答案 0 :(得分:-1)

这是我为客户撰写自定义属性的方式。

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;

$this->objectManager = Bootstrap::create(BP, $_SERVER)->getObjectManager();
$this->customerRepo = $this->objectManager->create(CustomerRepositoryInterface::class);

/** @var CustomerInterface $customer */
$customer = $factory->create();

// Create new Customer
$factory = $this->objectManager->create(CustomerInterfaceFactory::class);

// Set custom attribute
$customer->setCustomAttribute('my_custom_attribute_customer_profile_id', '123abc');


$this->customerRepo->save($customer);