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 2: Get list of all Categories & Store Categories

This article shows how we can get list of all categories and all category of current store in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory and \Magento\Catalog\Helper\Category classes 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 … Read more

Magento 2: Get Controller, Module, Action & Route Name

This article shows how we can get name of the current module, controller name, action name and route name in Magento 2. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\App\Request\Http class in the constructor of my module’s block class. app/code/Chapagain/HelloWorld/Block/HelloWorld.php <?php namespace Chapagain\HelloWorld\Block; … Read more

Magento 2: Check if Current URL is Homepage

This article shows how we can check if the current page URL is homepage URL in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of Logo block 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 $_logo; … Read more

Magento 2: Check if Current URL & Frontend URL are Secure (https)

This article shows how we can check if current URL and frontend URl are secure (https) or not in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface 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 … Read more

Magento 2: Get Store Information (Store ID, Code, Name, URL, Website ID)

This article shows how we can get store information in Magento 2. We will be fetching store id, store code, store name, store url, and store’s website id. We also write a function which checks if the store is active or not. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected … Read more