Sandbox script for quick testing in Magento2

September 14, 2016  |  No Comments  |  by Raj (MagePsycho)  |  Linux, Mac OS-X, Magento 2

What’s your approach for quick & dirty testing in Magento2?

  • Creating a test module with a controller and executing it from browser to see the output?
  • Creating a Console module and executing via CLI?

Obviously, above two approaches takes time.

Rather I would create a simple script (a single file) and put it somewhere in /[path-to-magento2]/pub/sandbox.php

<?php
/**
 * @author Raj KB<magepsycho@gmail.com>
 * @website http://www.magepsycho.com
 */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

// Your quick and dirty code goes here...
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote()->load(1);
Zend_Debug::dump($quote->getOrigData());

Now you can easily test by pointing to URL
http://[magento2-url]/sandbox.php

But wait, this won’t work in the case when you are using Nginx + PHP-FPM server.
The reason you can explore from the Nginx conf file: [path/to/magento2]/nginx.conf.sample

...
# PHP entry point for main application
location ~ (index|get|static|report|404|503)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 1024 4k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=768M \n max_execution_time=600";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
...

As you can see only PHP files: index.php, get.php, static.php, report.php, 404.php & 503.php files are parsed by the PHP-FPM.

In order to include your sandbox.php in the whitelist, just edit the line of Nginx conf file:

location ~ (index|get|static|report|404|503)\.php$ {

to

location ~ (index|get|static|report|404|503|sandbox)\.php$ {

And don’t forget to reload or restart your Nginx server depending upon your Operating System.
Ubuntu

sudo service nginx reload
sudo service nginx restart

MacOSx

sudo nginx -s reload
sudo nginx -s stop && sudo nginx

What’s your approach for quick & dirty testing in Magento2? Please do comment below.

MySQL Issue: Table storage engine for ‘catalog_product_relation’ doesn’t have this option

July 30, 2016  |  1 Comments  |  by Raj (MagePsycho)  |  Linux, Mac OS-X, Magento, Mysql

When you export the Magento database dump from MySQL 5.5.x and try to import in MySQL 5.7.x, You are likely to face an error:

ERROR 1031 (HY000) at line 3002: Table storage engine for ‘catalog_product_relation’ doesn’t have this option

PROBLEM

This is probably due to the table option that you have in your CREATE TABLE DDL: ROW_FORMAT=FIXED

Let’s check if there is any such string in the SQL dump (Ex: magento-db-dump.sql).

cat magento-db-dump.sql | grep '=FIXED'

Which resulted as:

) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED COMMENT='Catalog Product Relation Table';
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED COMMENT='Catalog Product To Website Linkage Table';

Refer – MySQL Row Format Option

SOLUTION

Removing ROW_FORMAT=FIXED option from CREATE TABLE DDL will fix the issue.
So let’s try possible solutions.

#1

sed -i 's/ROW_FORMAT=FIXED//g' magento-db-dump.sql

This didn’t work for me in MacOSx which resulted in following error:

sed: 1: “magento-db-dump.sql”: invalid command code m

#2

sed -i '' 's/ROW_FORMAT=FIXED//g' magento-db-dump.sql

And even this resulted as:

sed: RE error: illegal byte sequence

#3
But this one worked for me in MacOSx (Refer Mac OS X, Sed, and strange document encoding to know more about the issue)

LC_ALL=C sed -i '' 's/ROW_FORMAT=FIXED//g' magento-db-dump.sql

Re-check if the string has been removed or not:

cat magento-db-dump.sql | grep '=FIXED'

If the string is removed, now try to import as:

mysql -u <user> -p <db-name> < magento-db-dump.sql

Yay! Now it imported successfully.
Please do share and care if you liked this article.

Cheers!

Configure your Bash Shell for Magento 2 CLI Commands

June 29, 2016  |  No Comments  |  by Raj (MagePsycho)  |  Mac OS-X, Magento 2

One of the major improvement in Magento 2 is the introduction of CLI commands based on Symfony Console Component.

CLI commands can perform multiple tasks. Some of them are:

  • Installing Magento (and related tasks such as creating or updating the database schema, creating the deployment configuration, and so on)
  • Clearing the cache
  • Managing indexes, including reindexing
  • Creating translation dictionaries and translation packages
  • Generating non-existent classes such as factories and interceptors for plug-ins, generating the dependency injection configuration for the object manager.
  • Deploying static view files
  • Creating CSS from LESS
  • Providing “plug in” capability for third party developers

To invoke the CLI command:

php /path/to/your/magento2/bin/magento group:[subject:]action

To list all the available console commands:

php /path/to/your/magento2/bin/magento --list

which will output similar to:
Magento2 CLI Commands

In this article, we will discuss on how to configure bash shell for Magento 2 CLI to increase your productivity.

Bash Configuration Tips for Magento 2 Console

1. Install Bash Completion

Refer to our previous article – Install Bash Completion for Mac OS-X, which shows how to enable the completion feature for shell commands just by pressing [TAB]

2. Create useful bash aliases

Alias for bin/magento: mage2
Edit your ~/.bash_profile and add the following code:

alias mage2 = "./bin/magento"

Now instead of using:

php bin/magento [options]

You can simply use:

mage2 [options]

(Must be run from root of Magento2 path)

Alias for enabling module: mage2-module-enable
In order to enable module in Magento2, you have to run following series of command:

php bin/magento module:enable Vendor1_Module1 VendorN_ModuleN --clear-static-content
php bin/magento setup:upgrade

To simplify this process, you can simply add the alias as:

mage2ModuleEnable() {
	mage2 module:enable "$@" --clear-static-content && mage2 setup:upgrade
}
alias mage2-module-enable="mage2ModuleEnable"

Now you can simply enable the module(s) by running:

mage2-module-enable Vendor1_Module1 Vendor2_Module2

These are just a few examples. You can add similarly if needed.

Isn’t that a smarter way of executing CLI command?
Note: Don’t forget to reload the bash after adding aliases:

source ~/.bash_profile

3. Install Magento2 Bash Completion

Some of the cool dudes in Magento community are really doing amazing work. One of such work is magento2-bash-completion plugin.

In order to install this magento2-bash-completion, proceed as follow.
Download plugin to bash-completion hook folder

curl -o /usr/local/etc/bash_completion.d/magento2-bash-completion https://raw.githubusercontent.com/yvoronoy/magento2-bash-completion/master/magento2-bash-completion

Reload the shell

source /usr/local/etc/bash_completion.d/magento2-bash-completion

Time for Magic
Just write few letters of command and press [TAB], you will see as per the following gif:
Magento2 Bash Completion
(Image Source: https://github.com/yvoronoy/magento2-bash-completion)

Do you have any useful tips on executing Magento2 CLI commands? Please do share in the comment.
(We will keep on updating this post with more cool bash tips for Magento 2 CLI, so keep your eye on it.)

Install Bash Completion for Mac OS-X

May 22, 2016  |  No Comments  |  by Raj (MagePsycho)  |  Linux, Mac OS-X

One of the nicest features of the modern shell is the built in “completion” support. This allows you to complete commands and their arguments easily without memorizing.

Unfortunately, Mac OS-X bash shell doesn’t have completion feature by default unlike Ubuntu(Debian based Linux OS).
But the good thing is you can easily install the auto completion by using Homebrew or Macports.

Homebrew being my favourite packaging tool for OS-X, I will be explaining the installation using this tool.

Bash Completion Installation

1. Install bash-completion package using brew

brew install bash-completion

Brew Bash Completion

2. Edit the ~/.bash_profile

vi ~/.bash_profile

And add the following code:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

3. Reload the bash shell

source ~/.bash_profile

Now try to type few letters of command and press [TAB], you will see the auto-completed command or auto-suggested commands(for more than one matches). For example:
nets[TAB] ->

netstat

net[TAB] ->

net-server               net-server5.18           net-snmp-config
net-server5.16           net-snmp-cert            net-snmp-create-v3-user

Bonus

You can also install additional completions from Homebrew-Completions
All you have to do is:

brew tap homebrew/completions
brew install <formula>

If you know how to create bash completions, you can create your own file and drop in the folder:

/usr/local/etc/bash_completion.d

After sourcing(loading) it, your custom completion is ready to take into account.

Please do share/comment on your favourite bash-completion.

Magento 2 installation on MEMP (Mac OS X + Nginx + MySQL + PHP-FPM) stack

September 7, 2015  |  6 Comments  |  by Raj (MagePsycho)  |  Mac OS-X, Magento, Magento 2

Magento is a highly customizable e-commerce platform and content management system that you can use to build online storefronts or web sites for selling merchandise. Magento provides common e-commerce features, such as shopping carts and inventory management, and encourages extensive customization to meet your organization’s specific goals.

Magento 2 Merchant Beta codebase was released on July 15, 2015. This is a significant milestone before the general availability of Magento 2 in Q4 2015.

Before you begin installation

1. MEMP(Mac OS X + Nginx + MySQL + PHP-FPM) stack should already be installed & configured in your system. And system should meet the requirements discussed in Magento system requirements.

If MEMP stack is not installed, I would recommend homebrew package manager for installation.
2. Some Assumptions
Magento Dir: ~/Sites/opensource/magento/magento2beta
Virtual Host: magento2beta.dev

Once your MEMP system is ready meeting all the requirements, you are ready to move ahead for the installation.

Installation Steps

1. Create the project folder & clone the magento2 codebase

mkdir ~/Sites/opensource/magento/magento2beta && cd ~/Sites/opensource/magento/magento2beta
git clone git@github.com:magento/magento2.git .
Git Clone Magento2

Git Clone Magento2

2. Checkout to master branch

git checkout master

OR you can directly use the following command while cloning:

git clone -b master https://github.com/magento/magento2.git
Git Checkout Master Branch

Git Checkout Master Branch

Note:
You can clone either the master or develop branch:
– master is more stable
– develop is more recent

Initially we had an issue installating the sample data with develop branch, which resulted in the following error:

Fatal error: Call to undefined method Magento\Catalog\Model\Resource\Product\Interceptor::getWriteConnection() in app/code/Magento/SampleData/Module/Catalog/Setup/Product/Gallery.php on line 144

Sample data installation error in Magento 2

Sample data installation error

Due to recent changes, you can now use sample data with either the develop branch (more current) or the master branch.

3. File permission

chmod -R 755 ./

4. Update Magento dependencies using Composer
Install Composer if it’s not already installed:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Now run Composer to update dependencies:

composer install

This command updates package dependencies and can take a few minutes to complete.

Update Magento dependencies using Composer

Update Magento dependencies using Composer

5. Install sample data before Magento installation

– Edit composer.son:
In the first section, add “minimum-stability”: “beta”, before license.

then run the following commands:

composer config repositories.magento composer http://packages.magento.com
composer require magento/sample-data:1.0.0-beta

Sample Data Installation

Sample Data Installation Result


Note: You can also install sample data after Magento installaiton

[EDIT]
Magento 2 dev team has an updated version on how to install sample data:
http://devdocs.magento.com/guides/v2.0/install-gde/install/web/install-web-sample-data.html

6: Finally install Magento System
Run following command in order to install the Magento system:

php bin/magento setup:install --base-url=http://magento2beta.dev/ \
--db-host=localhost \
--db-name=magento2beta \
--db-user=root \
--db-password=root \
--admin-firstname=Raj \
--admin-lastname=KB \
--admin-email=myemail@gmail.com \
--admin-user=admin \
--admin-password=pass123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--sales-order-increment-prefix="ORD$" \
--session-save=db \
--use-rewrites=1 \
--use-sample-data \
--cleanup-database

To find more information on command options, please check the following url:
http://devdocs.magento.com/guides/v2.0/install-gde/install/cli/install-cli-install.html#instgde-install-cli-magento

Magento 2 - CLI Installation

Magento 2 – CLI Installation

Magento 2 - Installation Complete!

Magento 2 – Installation Complete!

7: Installation Complete!
Now you can explore the frontend and backend.
Some Screenshots:

Magento 2 - Homepage

Magento 2 – Homepage

Magento 2 - Category Page

Magento 2 – Category Page

Magento 2 - Product Page

Magento 2 – Product Page

Magento 2 - Cart Page

Magento 2 – Cart Page

Magento 2 - Checkout Page

Magento 2 – Checkout Page

Magento 2 - Admin Dashboard

Magento 2 – Admin Dashboard

Have fun with Magento 2!