1

I am currently trying to programatically add a new attribute from the module I have created, but so far without success. I am using the below:

<?php

namespace COMPANY\MODULENAME\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if ($context->getVersion() && version_compare($context->getVersion(), '1.0.1') < 0) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'chat_lat',
                [
                    'type' => 'text',
                    'label' => 'Chart latitude',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 1,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'wysiwyg_enabled' => false,
                    'group' => 'Product Details',
                ]
            );

            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'chat_lng',
                [
                    'type' => 'text',
                    'label' => 'Chart longitude',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 1,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'wysiwyg_enabled' => false,
                    'is_html_allowed_on_front' => false,
                    'group' => 'Product Details',
                ]
            );

        }

        $setup->endSetup();
    }
}

Obviously I have the correct company and module name specified in the namespace - this is just to protect the client. I have also run magento setup:upgrade, which seems to run fine.

2
  • Please use this tool might be help you thanks mage2gen.com Oct 20, 2017 at 13:52
  • @NagarajuKasa Where do I specify the Magento version with that tool? My understanding is that there are quite big differences between 2.0, 2.1 and 2.2. The code I generated from that tool doesn't seem to work.
    – wickywills
    Oct 20, 2017 at 14:07

2 Answers 2

1

Please try as mentioned below.

 <?php

namespace Stackexchange\Custom\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $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,
            'more_information',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'More Information',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => '',
                'system' => 1,
                'group' => 'General',
                'option' => array('values' => array(""))
            ]
        );


    }
}
1
  • Sorry but this doesn't work for me. Please could you advise the issue with the code in my original question?
    – wickywills
    Oct 20, 2017 at 14:09
1

Turns out that my issue was the version of the upgrade script set to 1.0.1, yet my module (module.xml) was set to 1.0.0. Changed the module to 1.0.1, ran magento setup:upgrade, and all is working fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.