2016-09-28 116 views
7

我進口的一些客戶:設置自定義客戶屬性值編程的Magento 2

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

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


     $customer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail('[email protected]'); 


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

但是這並沒有起作用。

自定義Attribute在Magentos 2後臺中顯示爲correclty,如果手動創建客戶,也會正確保存值。

+1

分享您用於創建客戶屬性的代碼。 –

回答

1

你有沒有嘗試過:

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

,不要忘記保存並記錄信息:

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