Magento 2 create product attributes using install scripts 0

  1. Under your Magento 2 module, create a directory called “Setup”. (Create Magento 2 Module)
  2. Create a new file: “InstallData.php” inside the Setup directory with the below content.

    namespace Magleaks\Catalog\Setup;
    use Magento\Catalog\Model\Product;
    use Magento\Catalog\Model\Product\Type;
    use Magento\Catalog\Model\Resource\Eav\Attribute;
    use Magento\Eav\Setup\EavSetupFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
     * Class InstallData
     * @package Magleaks\Catalog\Setup
    class InstallData implements InstallDataInterface
         * @var \Magento\Eav\Setup\EavSetupFactory
        private $eavSetupFactory;
         * @param EavSetupFactory $eavSetupFactory
        public function __construct(EavSetupFactory $eavSetupFactory)
            $this->eavSetupFactory = $eavSetupFactory;
         * @param ModuleDataSetupInterface $setup
         * @param ModuleContextInterface   $context
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $productTypes = [
            $productTypes = join(',', $productTypes);
                    'type'                    => 'int',
                    'label'                   => 'Sale',
                    'input'                   => 'boolean',
                    'sort_order'              => 50,
                    'global'                  => Attribute::SCOPE_WEBSITE,
                    'user_defined'            => true,
                    'required'                => false,
                    'used_in_product_listing' => true,
                    'apply_to'                => $productTypes,
                    'group'                   => 'General',
                    'unique'                  => false,
                    'visible_on_front'        => false,
                    'searchable'              => false,
                    'filterable'              => false,
                    'comparable'              => false,
                    'visible'                 => true,
                    'backend'                 => '',
                    'frontend'                => '',
                    'class'                   => '',
                    'source'                  => '',
                    'default'                 => '',
  3. Go to your Magento 2 root folder from your terminal and run

    bin/magento setup:upgrade

Create Magento 2 Module 0

Magento 2 module structure is more organized than Magento 1. It keeps all views, controllers and config files in one directory. This make it easier to maintain and develop modules. Let’s see how to create simple module with controller.

    1. Create name space under app/code. Eg: app/code/Magleaks
    2. Create your module under your name space. Eg: app/code/Magleaks/Tutorial1
    3. Create module.xml file under module etc folder. app/code/Magleaks/Tutorial1/etc/module.xml (this is the minimum requirement for module. No need to create module xmls under etc.)
    4. Copy module deceleration from core module like Magento_Catalog and change module name, and remove other contents.
    5. <?xml version="1.0" encoding="UTF-8"?>
      <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
          <module name="Magleaks_Tutorial1" setup_version="2.0.0">
    6. Run setup upgrade to configure the module. (<Magento 2 root>/bin/magento setup:upgrade)
    7. New module name should be in module list. Also you can verify this in admin. (Stores=>Configuration=>Advanced=>Advanced)
    8. Now lets create controller. Create controller class Magleaks/Tutorial1/Controller/Tutorial1/Index.php as follows.
    9. <?php
      namespace Magleaks\Tutorial1\Controller\Tutorial1;
      class Index extends \Magento\Framework\App\Action\Action {
          public function execute() {
              echo 'We are here !'; 


    10. Create file Magleaks\Turorial1\etc\frontend\routes.xml for route. You can copy content from Magento core and edit.
      <?xml version="1.0" encoding="UTF-8"?>
      <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
          <router id="standard">
              <route id="magleaks" frontName="tutorial1">
                  <module name="Magleaks_Tutorial1" />
    12. Now clear config cache and access controller via front end. /tutorial1/tutorial1/index

Check following video.
How to create M2 module

Magento 2 Theming with Grunt / Less Part 1 0

Magento 2 introduced brand new theme management mechanism. Some of new features are as follows

  • Extending layout files. In Magento 1 we had to copy entire layout xml file to local folder in order to make any changes. But Magento to allow it to extend layout files. Just need copy the part that you want to change. Also it allow you to override entire layout xml as in Magento 1, but it is not recommended.
  • Organized layout structure. Does not allow to add layout files without module folder. Check the folder structure.
  • Less as css compiler.
  • Css / Less inheritance. Less files from parent theme can be changed in custom themes.
Magento 2 theme structure
  • Built in less compiler to make development easier.
  • Css/Less library.

I will be demonstrating how to create and apply new theme in Magento 2 in part 2.

Part 2

Magento 2 Functional Testings (MTAF) 0

Magetno 2 comes with built in functional test framework.(MTAF) This is also available for Magento 1 too. Here is the quick step by step guide to how to run these functional test cases.


  • Working Magento 2 installation
  • Disable admin security key urls from magento admin panel. Store=>Config=>Advanced=>Admin=>Add security key url=NoSelection_003
  • Firefox or Google chrome web browser
  • Java runtime installed in your machine
  • PHP Composer

How to Run

  • Got to <magento root>/dev/tests/functional/.
  • Run composer install to install required dependancies. This will download mtaf, selinium RC etc to vendor folder. [composer install]
  • Run [php utils/generate/factory.php] after composer install to generate required factories.
  • Open phpunit.xml and update urls


<env name="app_frontend_url" value="" />

<env name="app_backend_url" value="" />

<env name="testsuite_rule" value="basic" />

<env name="testsuite_rule_path" value="Magento/Mtf/TestSuite/InjectableTests" />

<env name="log_directory" value="var/log" />

<env name="events_preset" value="base" />

<env name="module_whitelist" value="Magento_Install" />

<env name="basedir" value="var/log" />

<env name="credentials_file_path" value="./credentials.xml.dist" />

  • Open etc/config.xml and update login credentials

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="config.xsd">
















  • Those are the minimum configuration to get test cases runinng. You can find more descriptive configuration documents under vendor/magento/mtf/docs/
  • Start Selenium by [java -jar vendor/netwing/selenium-server-standalone/selenium-server-standalone-2.44.0.jar] your version may variy
  • Now run phpunit from <magento root>/dev/tests/functional/

Following are all the commands that used.

cd <magento root>/dev/tests/functional

composer install

vi phpunit.xml (change urls)

vi etc/config.xml (change urls/credentials)

java -jar vendor/netwing/selenium-server-standalone/selenium-server-standalone-2.44.0.jar


Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory. 0


When you try to run Magento 2 and if you get above error,

copy this code

bin/magento setup:upgrade

Go to your Magento 2 root folder.

Open the Git Bash ( I think this will work on command prompt as well )
Paste the code and Enter.


It will execute and once it completed, Run Magento 2.