2015-08-22 113 views
3

我在現有客戶實體的擴展期間通過安裝模式成功安裝了新的自定義屬性。如何在magento 2中保存客戶自定義屬性

我的問題是,當我試圖拯救客戶,它給了我像下面的錯誤。

Fatal error: Call to a member function get() on a non-object in /var/www/html/magento2/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php on line 236

我想通過下面的代碼保存客戶屬性:

$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); 

回答

0

這是怎麼我寫的自定義客戶屬性。

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); 
相關問題