2017-07-16 70 views
1

我已經在Magento 2.更改屬性數據的商店 - Magento的2

創建使用InstallData客戶自定義屬性,但是我想明智的更改屬性商店的is_required選項。

updateAttribute可以做同樣的事情,但我不知道如何使用它存儲明智。

 $customerSetup->updateAttribute('customer', 'tax_exempted', 'is_required', true); 

Code Snippet to create attribute。

namespace xyz\abc\Setup; 

use Magento\Customer\Model\Customer; 
use Magento\Framework\Setup\ModuleContextInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 

/** 
* Install attributes 
*/ 
class InstallData implements \Magento\Framework\Setup\InstallDataInterface 
{ 

/** 
* @var \Magento\Customer\Setup\CustomerSetupFactory 
*/ 
protected $customerSetupFactory; 

/** 
* @var \Magento\Eav\Api\AttributeRepositoryInterface 
*/ 
protected $attributeRepository; 

/** 
* Init 
* 
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory 
* @param \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository 
*/ 
public function __construct(
    \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, 
    \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository 
) { 
    $this->customerSetupFactory = $customerSetupFactory; 
    $this->attributeRepository = $attributeRepository; 
} 

/** 
* DB setup code 
* 
* @param \Magento\Framework\Setup\SchemaSetupInterface $setup 
* @param \Magento\Framework\Setup\ModuleContextInterface $context 
* @return void 
*/ 
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { 
    /** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */ 
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); 

    $setup->startSetup(); 

    if ($customerSetup->getAttributeId('customer', 'tax_exempted') === false) { 
     $custAttr = $customerSetup->addAttribute(
       Customer::ENTITY, 
       'tax_exempted', 
       [ 
        'label'   => 'Is Tax Exempted', 
        'type'    => 'int', 
        'input'   => 'boolean', 
        'default'   => '0', 
        'position'   => 71, 
        'visible'   => true, 
        'required'   => false, 
        'system'   => false, 
        'user_defined'  => true, 
        'visible_on_front' => false, 
       ] 
      ); 

     $taxExemptedAttr = $customerSetup->getEavConfig()->getAttribute(
      Customer::ENTITY, 
      'tax_exempted' 
     ); 

     $this->attributeRepository->save($taxExemptedAttr); 

    } 

    $setup->endSetup(); 
} 
} 

回答

1

我找到了一個解決方案,在下面分享。

 //Fetch all websites 
     $websites = $this->_storeManager->getWebsites(); 
     foreach ($websites as $website) { 
      //fetch the attribute 
      $customAttribute = $this->_customerSetup->getEavConfig() 
       ->getAttribute(
        \Magento\Customer\Model\Customer::ENTITY, 
        'tax_exempted' 
       ); 

      $customAttribute->setWebsite($website->getId()); 
      $customAttribute->load($customAttribute->getId()); 

      //for options that are website specific, scope_ is prefixed while changing 
      $customAttribute->setData('scope_is_required', 0); 
      $customAttribute->setData('scope_is_visible', 0); 

      /** For xyzwebsite, show the attribute */ 
      if ($website->getCode() == 'xyz') { 
       $customAttribute->setData('scope_is_required', 1); 
       $customAttribute->setData('scope_is_visible', 1); 
      } 
      $customAttribute->save(); 
     }