浏览文章

文章信息

Magento2 如何添加新属性到产品|how to add attribute for product 14827

示例:

<?php
/**
 * @Author       秋枫雁飞
 * @Email        aiweline@qq.com/1714255949@qq.com
 * @Desc         文件由Aiweline(秋枫雁飞)编写,若有升级需要
 *               建议不要随意修改文件源码。
 **/
namespace Aiweline\Upsell\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    const attr_IS_ON_UPSELL = 'is_on_upsell';
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * @inheritDoc
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
           self::attr_IS_ON_UPSELL,
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Is On Upsell ',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => false,
                'required' => false,
                'user_defined' => true,
                'default' => 0,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}


原创