Magento 2: Get All Product Attributes with Name and Value

This article shows how to load product by it’s ID and then get all attributes associated with that product in Magento 2. We print the attribute code, attribute name, and attribute value of all the attributes related to any particular product. We will be using Magento 2’s Service Layer for this task. Use of Service … Read more

Magento 2: Get Product Stock Quantity and Other Stock Information

This article shows how to get stock quantity (qty) of a product in Magento 2. We can also fetch other stock information like minimum quantity (min_qty), minimum sale quantity (min_sale_qty), maximum sale quantity (max_sale_qty), see if a product is in stock (is_in_stock), etc. We will be using Magento 2’s Service Layer for this task. Use … Read more

Magento 2: Load Product by ID and SKU

This article shows how to load product by id and sku in Magento 2. We will be using Magento 2’s Service Layer for this task. Use of Service Layer is highly encouraged by Magento. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ProductRepository class in the constructor of … Read more

Magento: Get all products by attribute id and attribute option

This article shows how to get / filter products based on it’s attribute id, or the attribute’s options id. Get all products by a single attribute option id Suppose, we have an attribute named ‘manufacturer’. Let’s say, this attribute has options like Samsung, Apple, Nokia, HTC, etc. Each option has certain ID. Suppose, we want … Read more

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: 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