Magento 2: Command Line Tool / Interface (CLI)

Magento 2 uses Symfony’s Console Component for its command-line interface or tool (CLI) where we can execute different commands for different tasks related to installation and configuration. Through the Command Line Interface (CLI) tool in Magento 2, you can perform different tasks like: Installing Magento Clearing the cache Managing indexes, including reindexing Creating translation dictionaries … Read more

Magento 2: Simple Hello World Module – Step-by-Step Beginner Tutorial

This beginner step-by-step tutorial shows how you can create a simple and basic HelloWorld module / extension in Magento 2. The necessary files are listed with the code. This module just calls a block function in template file to print Hello World text. GitHub Repository of the Module: https://github.com/chapagain/simple-helloworld-magento2 Things to note: In Magento 2, … Read more

Magento 2: Check if a module is installed, enabled or active

This article shows how we can check if a module is installed / enabled or active in Magento 2. Both Dependency Injection and Object Manager ways are shown below. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\Module\Manager class in the constructor of my … Read more

Magento 2: How to call any template block from phtml file?

In Magento 1.x, you could call/print any module’s template block in any other template (phtml) file with the following code: echo $this->getLayout() ->createBlock('newmodule/newblock') ->setTemplate('newmodule/newblock.phtml') ->toHtml(); In Magento 2.x, it’s slightly different. Below is the code to call/print a custom template block in another template file in Magento 2. Suppose, you want to call a template … Read more

Magento 2: How to call CMS Static Block from template (phtml) file?

In Magento 1.x, you could call/print the CMS Static Block in template file with the following code: echo $this->getLayout() ->createBlock('cms/block') ->setBlockId('your_block_identifier') ->toHTML(); In Magento 2.x, it’s quite similar. Below are the sample codes to show/call template (.phtml) files in Magento 2. Call CMS Static Block in any template (.phtml) file echo $this->getLayout() ->createBlock('Magento\Cms\Block\Block') ->setBlockId('your_block_identifier') ->toHtml(); … Read more

Magento 2: Get parent category, children categories & product count

This article shows how we can get parent category, children categories and total number of products in a category in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\CategoryFactory class in the constructor of my module’s block class. Objects of class \Magento\Catalog\Helper\Category and \Magento\Catalog\Model\CategoryRepository as also … Read more

Magento 2: Get all Products of a Category

This article shows how we can get all products of a particular category in Magento 2. Both Dependency Injection and Object Manager way of coding is shown below. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\CategoryFactory class in the constructor of my module’s … Read more

Magento 2: Get Product Collection

This article shows how we can get product collection in Magento 2. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( … Read more

Magento 2: Get Related, UpSell & CrossSell Products

This article shows how we can get related products, upsell products, and cross-sell products in Magento 2. Check the product relation tables in MySQL database mysql> SELECT * FROM `catalog_product_link_type`; +————–+————+ | link_type_id | code | +————–+————+ | 1 | relation | | 3 | super | | 4 | up_sell | | 5 | … Read more