Answers:
以编程方式添加产品属性概述
InstallData.php
install()
方法步骤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。
如果要删除产品属性,可以使用方法removeAttribute代替addAttribute。它将是这样的:
编辑:
要卸载,请创建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创建自定义产品属性。
网址:https : //www.mageplaza.com/magento-2-module-development/magento-2-add-product-attribute-programmatically.html