Magento: Filter Configurable Product by Child Product’s Attribute

This article shows how you can filter child products associated with a configurable product by the child product’s attribute value. Suppose, we have a configurable product with two attributes (‘size‘ and ‘color‘) and we want to select only those child products with certain size and/or certain color. Let’s say, we want to select only those … 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: Display Product Price Including Tax

To display/show product price including tax amount, you need to do some configuration settings. 1. Create Tax Rule You need to create tax rules (Sales -> Tax -> Manage Tax Rules). For this, you can refer to different tutorials available over the internet on creating Tax rules. 2. Assign Tax Rule to Product Then, you … 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