Wednesday 3 June 2015

Get system config values in magento 2

How to get data from Magento 2 System Configuration ? Here's how to.

We need to call the default method available.

Just Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
In your constructor argument and create an Object $this->scopeConfig = $scopeConfig;

Now to Get the configuration value just use
$this->_scopeConfig->getValue('dev/debug/template_hints', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

First argument is the value which we need from system configuration and the Second argument is the Store scope.

Demo

public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
) {
$this->_scopeConfig = $scopeConfig;
}


public function helloWorld(){

   $showTemplateHint =  $this->_scopeConfig->getValue('dev/debug/template_hints', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

}

Friday 29 May 2015

Logging in magento 2 including FirePHP

Magento 2 uses Monolog library to log messages. More information about the monolog is here

You can find the Library package in the following location in Magento 2
MAGENTO2_ROOT/vendor/monolog

Log files will be created inside var/log folder.

To add logging to your class we need to add an instance of the monolog class. As magento 2 uses Dependency Injection(DI)  we need to pass the instance in the constructor of your class.

Just for testing, we are going to add this in one of the magento's default class. Go to app/code/Magento/Cms/Block/Page.php

And add the below lines After Protected $pageConfig


/**
* @var \Psr\Log\LoggerInterface
*/
protected $_logger;

And add this line \Psr\Log\LoggerInterface $logger,  as the parameter of the __construct() 

Now we need to create an object. so add these line inside the function

$this->_logger = $logger;
$this->_logger->addDebug('some text or variable');

So Finally our Constructor function will look like this.


public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Cms\Model\Page $page,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\PageFactory $pageFactory,
        \Magento\Framework\View\Page\Config $pageConfig,
        \Psr\Log\LoggerInterface $logger,
        array $data = []
    ) {
        parent::__construct($context, $data);
        // used singleton (instead factory) because there exist dependencies on \Magento\Cms\Helper\Page
        $this->_page = $page;
        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_pageFactory = $pageFactory;
        $this->pageConfig = $pageConfig;
        $this->_logger = $logger;
        $this->_logger->addDebug('some text or variable');
    }

TO USE FIREPHP LOGGING

Detailed Article here

Monolog has inbuilt FirePHP logging library. FirePHP is used to send log messages to the FireBug Console. More information here. Install FirePHP Firefox / Chrome addon to use this.

Now to Use FirePHP in Magento 2 just use this line
$this->_logger->pushHandler(new \Monolog\Handler\FirePHPHandler());
above
$this->_logger->addDebug('some text here');

You can see the message available on the Firebug Console panel, instead of creating a log file.

Friday 22 May 2015

Configurable Product not adding to cart

Community edition 1.9 and Enterprise edition 1.14 both have this problem. There will be a memory exhausted issue or the browser keeps loading forever.

There is a patch for this fix which is available on github.
More discussion can be found on the magento.stackexchange website.

Remove maintenance mode in magento 2

Removing Maintenance mode in Magento 2 is extremely simple as in previous Magento versions.

The .maintenance.flag file is located under var folder in Magento 2. Delete this file to remove the maintenance mode.

Thursday 22 January 2015

Set / Change Meta title and description in Magento 2


SET PAGE META VIA BLOCK


You need to place this code inside your _prepareLayout() function in your Block file. Below is the complete function.

public function _prepareLayout()
{

   //set page Meta's
   $this->pageConfig->setKeywords('Hello Metakeyword');
   $this->pageConfig->setDescription('Hello Metadescription');

   return parent::_prepareLayout();

} 

SET PAGE META VIA LAYOUT XML

Open your xml file inside your layout folder and place this code above the body tag.

<head>
    <meta name="description" content="XML Hello metadesc"/>
    <meta name="keywords" content="XML Hello keywords"/>

</head>

NOTE:  When you try to add title via both the methods first preference will be given to the block method(php file).

Sunday 18 January 2015

Set page title on Magento 2

I was working on creating a simple Hello world module in Magento 2. Tutorials from other websites, there was only instructions to create the module and then I found the page title was empty.

I did a little search in the core files and found the code to set page title.

SET PAGE TITLE VIA BLOCK

$this->pageConfig->getTitle()->set(__('Hello World'));

You need to place this code inside your _prepareLayout() function in your Block file. Below is the complete function.

public function _prepareLayout()
{

   //set page title
   $this->pageConfig->getTitle()->set(__('Hello Index Test'));

   return parent::_prepareLayout();

} 

SET PAGE TITLE VIA LAYOUT XML

Open your xml file inside your layout folder and place this code above the body tag.

<head>
    <title>SAMPLE TITLE</title>

</head>

NOTE:  When you try to add title via both the methods first preference will be given to the block method(php file).

Monday 12 January 2015

Set/Enable developer mode in Magento 2

Magento 2 is already out for developers for testing and contributing improvements. After installing magento 2, you need to enable the Developer mode to show the errors on browser. Else, the errors will be logged into a separate file under var/report.

There are multiple ways to enable a developer mode
Via Console
Run the following command "php bin\magento deploy:mode:developer"

Edit env.php
'MAGE_MODE' => 'developer'  add this line on your env.php file below 'x-frame-options'

OR
Just add this below line on your index.php file
$_SERVER['MAGE_MODE'] = 'developer';

Place the code above on line 1. Above all the codes which was there already.

Wednesday 7 January 2015

Magento 2 Installation Guide.

Magento 2 is under development phase and there are beta release on Github which are free to explore. You can download Magento 2 here.

INSTALLATION

* Once you have downloaded magento2 zip file from Github, you can extract it to your htdocs folder(in xampp).

* Next thing is to Install the Composer. Without installing composer we cannot run Magento 2.

* Steps for Installing Composer on Windows machine can be found here. Follow steps from 1 to 5 if  latest xampp version is installed.

* Step for Installing Composer on Linux machine can be found here.

* Once Composer is installed properly, Now you can install magento 2. Find the composer.JSON file inside your extracted magento 2 folder and Right click on it and select composer Install.

* Now the composer will install the necessary library files and you are good to go.

* Run the file via your Browser like the usual magento Installation.


NOTE: Magento 2 requires PHP version 5.4 or higher.

Complete installation instructions with Screenshots is available on Magestore website.

You can get the Usefull links to study and get to know more about Magento 2 from my next post. Click here.

Magento 2 study guide & useful websites to get to know about it


The new version of Magento has a significant changes in the architecture when comparing to its 1.* versions. I have started learning Magento 2 and found some of the websites that explains the changes and improvements very clearly. These are from the Masters of Magento.

Am not going to explain the changes that are in Magento 2, instead I will give you all the Links that are useful for you to explore and learn it by yourself.

OFFICIAL WIKI
It's good to have the Magento have their own well formatted Wiki for Magento 2. The Wiki explains all the technology changes in Magento 2. You can go to the Official WIKI page here.

THE MASTERS

I have also found other websites that are extremely useful to study magento 2. They are,
* The Alan Kent's Blog. (Magento's chief architect)
* The Inchooers website.
* Magestore's website.
* An Awesome explanation for Dependency Injection(DI) can be found here. The website explains the DI for Symphony, Yet the concept for the DI is the Same for all. More explanation for DI can be found from the Jeff More Presentation.

START EXPLORING WITH THESE INITIATIVES

Magestore have developed a banner module for Magento 2. You can visit their website here to View the Demo and download the banner module.

You can get the THEME for Magento 2 here. Uber theme offers this free Magento 2 theme and the Live Demo of the theme can be found on the link provided.

SAMPLE DATA FOR MAGENTO 2

Uber theme also provides the Sample data for magento 2. You can visit their website here. This website clearly explains the installation steps and configuring magento 2 with Screenshots.

Go crazy on New Magento 2.