Magento 2:以编程方式添加产品属性


Answers:


34

以编程方式添加产品属性概述

  • 步骤1:建立档案 InstallData.php
  • 步骤2:定义install() 方法
  • 步骤3:建立自订属性

步骤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


我想创建一个文件上传属性。我必须做些什么改变?友善的向导
临时的,

@ephemeral,您可以更改'input'=>''的值,您可以在此处阅读:magento.stackexchange.com/a/116829/2694
Andhi Irawan,

我必须将'int'替换为吗?在这个链接上,我没有找到要上传的文件:(
短暂

作为特殊提示,请不要将字段'input'=>''留空。这将导致错误。magento.stackexchange.com/questions/204420/…–
ZFNerd

嗨@Prakash Patel,如果没有安装程序,我们可以创建产品属性吗?
贾法尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.