I am trying to add an attribute programmatically in Magento 2 but getting error. Please I need urgent help.
Thanks in advance. Details are as below:
I followed the instructions on
https://www.atwix.com/magento/adding-attribute-programatically-magento2/ and tried to add attribute.
I have windows 7,xampp with php 7.0, magento 2.1 and mysql.
in path variable I set following for php commands:
C:\xampp\php;C:\xampp\php\
php.exe
When I run
C:\xampp\htdocs\maglocal1>
php bin/magento setup:upgrade
I get following error at the end
Parse error: syntax error, unexpected 'use' (T_USE) in C:\xampp\htdocs\maglocal1
\app\code\Atwix\TestAttrib
ute\Setup\
InstallDat
a.php on line 5
My data in \app\code\Atwix\TestAttrib
ute\Setup\
InstallDat
a.php is
<?php
namespace Atwix\TestAttribute\Setup;
use Magento\Eav\Setup\EavSetup
;
use Magento\Eav\Setup\EavSetup
Factory;
use Magento\Framework\Setup\In
stallDataI
nterface;
use Magento\Framework\Setup\Mo
duleContex
tInterface
;
use Magento\Framework\Setup\Mo
duleDataSe
tupInterfa
ce;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactor
y $eavSetupFactory)
{
$this->eavSetupFac
tory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.Ex
cessiveMet
hodLength)
*/
public function install(ModuleDataSetupInt
erface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->cr
eate(['set
up' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttr
ibute(
\Magento\Catal
og\Model\P
roduct::EN
TITY,
'test_attribut
e',
[
'type' => 'int',
'backend' => '',
'frontend'
=> '',
'label' => 'Test Attribute',
'input' => '',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\Res
ource\Eav\
Attribute:
:SCOPE_GLO
BAL,
'visible' => true,
'required'
=> false,
'user_defi
ned' => false,
'default' => 0,
'searchabl
e' => false,
'filterabl
e' => false,
'comparabl
e' => false,
'visible_o
n_front' => false,
'used_in_p
roduct_lis
ting' => true,
'unique' => false,
'apply_to'
=> ''
]
);
}
}