1
私は賢明な属性ストアのis_required
オプションを変更したいしかし、私は変更はストアの属性データ - Magentoの2
Magentoの2にInstallDataを使用して、顧客のためのカスタム属性を作成しました。
updateAttribute
でも同じことをすることができますが、私はそれを賢明に使用する方法はわかりません。
$customerSetup->updateAttribute('customer', 'tax_exempted', 'is_required', true);
コードスニペットで属性を作成します。
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();
}
}