Tag Archives: magento tutorial for beginners

Magento Create Helper


Magento Create Helper: First Question that comes into our mind is – What is helper in magento and then what are it’s uses? where and how to use them? Here in this tutorial you will get to know everything step by step. Here in this tutorial we are going to explain how you can create helpers and use them.


Magento Create Helper : Custom Helper Example

Helper is basically an object which contains functions. You can call it’s function anywhere such as controller, model or view by just loading helper.

For example –

Magento load and call helper Example :

// load helper
$helper = Mage::helper('testhelper');
$helper->myFunction();

Now let us understand how to create helper in magento step by step –

Magento : Create Helper Steps

Step 1 : Add Configuration in Config.xml

Go to config.xml and the following in global tag as –

Magento Helper Configuration :


//...
//...
//...other config goes here

   
          MyCompany_Test_Helper
    


Step 2 : Create Helper Class

Let us create helper class. Go to your module folder and create folder named as “helper” and create new file as data.php ie. app/code/local/MyCompany/Test/Helper/data.php. Now let us create helper class as below –

Magento Create Helper Class Example :


Make sure class name MyCompany_Test_Helper_Data is correct.

Step 3 : Load Helper & Call Function –

Now first load the helper and then you can call your helper function as below-

Magento load and call helper Example :

// load helper
$helper = Mage::helper('testhelper');
$helper->myFunction();

testhelper is tag name defined in config.xml which is used to load helper in magento.


More About

Let’s have look over more about magento helper here.


Magento helper class not found

Sometimes we get the error helper class not found in magento. This error comes when you have defined the helper in config.xml or the class name defined is not correct. So make sure you have defined both correctly. I am sure it will work if both are correct.

Magento2 Install Sample Data


Magento2 Install Sample Data – You can use the command magento setup:upgrade and magento sampledata:install. It will install the magento 2 sample data./p>


Magento2 Install Sample Data After Setup

Go to the magento root and run the following command to install the sample data in mangeto2. Here are the steps to install sample data on windows (xampp or wamp).

Step 1

Go to magento root folder and open the composer to the php command –

Magento2 Install Sample Data  using composer

Step 2

Now run the below command –

Magento2 Install Sample Data Steps : Magento 2 Sample Data Example

php bin/magento sampledata:deploy

The command will look something like this –

Magento 2 Sample Data Install

Step 3

Now finalize the sample data installing by running the below command –

Magento2 Install Sample Data Steps : Magento 2 Sample Data Example

php bin/magento setup:upgrade

The command will look something like this –

Magento2 Sample Data Installlation

This will finish the sample data installation in magento2. Now you can use sample data in magento2.

Magento 2 Directory Structure


Magento 2 Directory Structure : Magento 2 folder structure is changed significantly. Here in this tutorial we are going to explain the magento 2 folder structure.


Magento 2 Directory Structure

Let us go step by step to understand the new directory struture.

Magento 2 Directory Structure : Root

The root folder contains the following folders and files-

Magento 2 Directory Structure

Magento 2 app folder –

App folder in magento 2 looks like this –

Magento 2 app folder

code folder contains modules and design contains the themes.

In magento 2.0 the app/etc/local.xml is replaced by app/etc/env.php. This contains the database settings & Admin names etc. The folder modules is also removed from magento2.

\app\etc now looks like this –

Magento 2 etc folder

Magento 2 : Database Settings & Admin Name Settings

 
  array (
    'frontName' => 'admin',
  ),
  'install' => 
  array (
    'date' => 'Fri, 11 Mar 2016 05:10:24 +0000',
  ),
  'crypt' => 
  array (
    'key' => '68be9e846774564a7ef08545dab04ceb',
  ),
  'session' => 
  array (
    'save' => 'files',
  ),
  'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magento2',
        'username' => 'root',
        'password' => '',
        'active' => '1',
      ),
    ),
  ),
  'resource' => 
  array (
    'default_setup' => 
    array (
      'connection' => 'default',
    ),
  ),
  'x-frame-options' => 'SAMEORIGIN',
  'MAGE_MODE' => 'default',
  'cache_types' => 
  array (
    'config' => 1,
    'layout' => 1,
    'block_html' => 1,
    'collections' => 1,
    'reflection' => 1,
    'db_ddl' => 1,
    'eav' => 1,
    'config_integration' => 1,
    'config_integration_api' => 1,
    'full_page' => 1,
    'translate' => 1,
    'config_webservice' => 1,
  ),
);
?>

You can add the configurations as per your requirement.

Magento 2 Installation Steps on Windows


Magento 2 Installation Steps on Windows – Magento 2 installation is very easy and simple, you install Magento simply following the steps explained in this tutorial. Magento2 installation requires some server configuration and composer to install on xampp windows localhost. The composer is not installed by default so if it is not installed on your machine please check and install it. Here in this tutorial, we are going to explain how to install magento2 on windows xampp localhost. We will also cover how to install composer & dependencies.


Magento 2 Installation Steps on Windows Xampp & Wamp Localhost

Hrere are Steps to install magento2 on windows xampp localhost-

Step 1 : Download Magento 2

First Download the latest version of Magento2 from – Download Magento 2 and unzip this in htdocs folder. It will look like this –

Magento 2 Installation Steps on Windows

Step 2 : Install Composer

If composer is not installed on your machine install it. Download composer from Download Composer. Now run the Downloaded file – Composer-Setup.exe.

Magento 2 Installation Steps on Windows

After successful installation Now let us go for next step.

Step 3 : Execute Composer Install

Now go to the magento2 folder located at htdocs. Right click on the magento2 folder You will see following options now click on composer install –

Install magento 2

This will install the dependencies and other updates and will generate autoload files. If no update is available it will generate autoload files directly and proceed.

Install magento 2 on xampp localhost windows

Step 4 : Create Database & Complete installation

Create database for the setup.

Now Access in Url You Project something like this – http://localhost/magento2/ magento2 is folder name where you extracted the files. After accessing this in url you will see something like this –

Install magento steps example

Now Click on the terms & condition button you will see next step –

Magento2 Installation steps

If You face issue in php settings you can refer to fix it – http://tutorialsplane.com/magento2-php-settings-check-error-fix/

After This successful step you will be asked to fill the database & other configuration detail as-

Magento 2 install

In the next steps fill the required configuration and last step is intsall-

Magento 2 install steps

After this step you are ready to go with magento 2.

Magento 2 Admin Demo Example page

Magento2 PHP Settings Check Error Fix


Magento2 PHP Settings Check Error Fix : While installing the magento 2 checks the Readiness Check which sometimes gives you the above error if it is disabled. If you are facing this problem on server contact your service provider.


Magento2 PHP Settings Check Error Fix

Go to php.ini file and uncomment the below line and set it to ‘-1’ as below –

Magento2 PHP Settings Check Error Fix:

;always_populate_raw_post_data = On
// change this to 
always_populate_raw_post_data = -1

This will solve the above problem. If this is coming on sever please contact your service provider.

Make Sure to restart the apache after making the above settings.

Magento 2 get base url


Magento 2 get base url – In magento 1.x we use Mage::getBaseUrl(); but in magento 2.0 it is different to get the base url than the magento 1.x. Here in this tutorial we are going to explain how you can get the base url, media url and link url on phtml, blocks, controllers & models in magento 2.0, media url etc.


Magento 2 get base url

You can get base url in magento 2.0 as below –

Magento 2 get base url – Method 1

Magento2 get base url : Example

$baseUrl = $this->_storeManager->getStore()->getBaseUrl();
echo $baseUrl; //will give base url

The above example will give you base url in magento2.

Magento 2 get base url – Method 2

Using this method you can get base url anywhere on – phtml, blocks, controllers or models also get base url using the following method –

Magento2 get base url : Example

$storeId = 1; // add your store id
$baseUrl = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
                     ->getStore($storeId)
                     ->getBaseUrl();
echo $baseUrl;

The above example will give you base url in magento2.

Magento 2 get media url – Method 2

You can get media base url using the following method –

Magento2 get media url : get Media Path Example

$mediaUrl = $this->_storeManager->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
echo $mediaUrl;

The above example will give you media base url in magento2.

Magento 2 get link url – Method 2

You can also get link base url using the following method –

Magento2 get link url : get link Path Example

$linkUrl = $this->_storeManager->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
echo $linkUrl;

The above example will give you link base url in magento2.

Magento 2 not loading css and js


Magento 2 not loading css and js – Sometimes after installing magento 2 we face problem like css and js are not loading. Here in this tutorial we are going to explain how you can fix this problem by following few steps.


Magento 2 not loading css and js

Here are the steps –

  • Step 1 : Open Cmd
  • Step 2 : Go to the magento root directory.
  • Step 3 : Run the command – php bin/magento setup:static-content:deploy
  • Step 4 : Delete Cache
  • Step 5 : Make Sure apache rewrite_module is enabled if it is not enabled go to – Apache->apache modules->rewrite_module and enable this module. If you enable the apache rewrite module restart the server to restart all services.

Hope the above solution will work for you.

Get customer data in magento 2


Get customer data in magento 2 : In magento 2 it is different to access the customer data than magento 1.x. Object Manger ($objectManager = \Magento\Framework\App\ObjectManager::getInstance();) is used to access the customer object. Here in this tutorial we are going to explain how you can access the customer data in magento2.


Get customer data in magento 2

Use $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); and get customer data using the customer id as –

Get customer data in magento 2: customer data from id in magento2

$customerId = 100;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

Thus you can get the customer data from customer in magento 2. The above example will give the data of customer id 100. You can get the customer information such as name, email phone etc from the above example.

Magento 2 get Product Collection


Magento 2 get Product Collection : In magento 1.x we get product collection using the Mage::getModel(‘catalog/product’)->getCollection(); but in magento 2.0 it is different to get the product collection. Here in this tutorial we are going to explain how to get product collection in magento 2.


Magento 2 get Product Collection

You can get product collection in magento 2.0 as below-

Magento 2 get Product Collection:

create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $prodCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('sku',$sku);
             // or ->load($productId);
foreach ($collection as $product){
     echo 'Name =  '.$product->getName().'
'; echo 'Id = '.$product->getId().'
'; echo 'Sku = '.$product->getSku().'
'; } ?>

The above example will give the product collection of by the sku or you can simply load the collection by product id simply using the ->load($productId) method.