更改商店的属性数据 - Magento 2

时间:2017-07-16 17:35:25

标签: magento magento2

我在magento 2中使用InstallData为客户创建了一个自定义属性。

但是我想更改属性存储的is_required选项。

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();
}
}

1 个答案:

答案 0 :(得分: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();
        }