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

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

最佳答案

我找到了一个解决方案,在下面分享相同的内容。

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

关于magento - 更改商店的属性数据 - Magento 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45131554/

相关文章:

magento2 - 在 magento2 运行时设置包和主题

php - 在 ubuntu 上安装语言包 magento2 时出错

php - Magento Cron Tab 作业时区

php - Magento更新每商店价格查看SQL

Magento:将其用作通用 FrameWork/CMS(BareBone Magento)

magento2 - 如何在 magento2 的标题包装器中移动顶部菜单?

php - 多个 Magento 环境

php - Magento 1.9 - 产品未添加到已注销用户的购物篮中

requirejs - 如何将 Magento2 与 RequireJS 一起使用?

hook - magento 2 供应商 git diff 并应用补丁