美文网首页
magento如何添加一个attribute

magento如何添加一个attribute

作者: lerko_ | 来源:发表于2017-07-14 11:46 被阅读18次

我们需要在module目录下面建一个Setup文件夹
这个文件件存放的是执行setup:upgrade命令的时候会执行的一些代码
主要是数据库以及一些配置

我们要新建一个product的attribute我们只需要新建一个这样的目录结构

image.png

然后我们写入代码

<?php
namespace TmobLabs\Tappz\Setup;

/**
 * Created by PhpStorm.
 * User: bardiaafshin
 * Date: 8/28/16
 * Time: 11:22 AM
 */
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();
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,//对应要添加attribute的entity 类
            'material',//添加的attribute的名称
            [
                'group'=>'Genneral',
                'type' => 'varchat',//存放的类型
                'backend' => '',
                'frontend' => '',
                'label' => 'Material',
                'input' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

相关文章

网友评论

      本文标题:magento如何添加一个attribute

      本文链接:https://www.haomeiwen.com/subject/rzmnhxtx.html