回答:
プログラムによる製品属性の追加の概要
InstallData.phpinstall()    メソッドを定義するステップ1:ファイルを作成するInstallData.php
次の場所にあるInstallDataクラスから始めます。
app/code/Mageplaza/HelloWorld/Setup/InstallData.php. このファイルの内容:
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
}ステップ2:install()メソッドを定義する
<?php
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
}ステップ3:カスタム属性InstallData.phpを作成するこれは、製品属性をプログラムで作成するための
すべての行コードです。
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sample_attribute',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sample Atrribute',
                'input' => '',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}ご覧のとおり、addAttributeメソッドに必要なのは次のとおりです。属性を追加するエンティティのタイプID属性の名前グループ、入力タイプ、ソース、ラベルなどの属性を定義するキーと値のペアの配列…
すべて完了したら、アップグレードスクリプトphp bin / magento setup:upgradeを実行してモジュールをインストールすると、製品属性sample_attributeが作成されます。
製品属性を削除する場合は、addAttributeの代わりにremoveAttributeメソッドを使用できます。次のようになります。
編集:
アンインストールするには、app / code / Mageplaza / HelloWorld / Setup / Uninstall.phpを作成します。
<?php
namespace Mageplaza\HelloWorld\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
class Uninstall implements UninstallInterface
{
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->removeAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sample_attribute');
    }
}また、以下のカスタム製品属性を作成するためのURLに従うことができます。
URL:https : //www.mageplaza.com/magento-2-module-development/magento-2-add-product-attribute-programmatically.html