Magento 2: Set Unset Get Session

This article shows how to create and destroy different types (Checkout, Customer, Catalog) of sessions in Magento 2. As you can see below, there are different types of session classes present in Magento 2. vendor/magento/module-catalog/Model/Session.php vendor/magento/module-newsletter/Model/Session.php vendor/magento/module-persistent/Model/Session.php vendor/magento/framework/Message/Session.php vendor/magento/module-customer/Model/Session.php vendor/magento/module-backend/Model/Session.php vendor/magento/module-checkout/Model/Session.php In below example code, I will be dealing with Catalog, Customer, and Checkout sessions. … Read more

Magento 2: Get Current URL & Base URL

This article shows how we can get current and base URL in Magento 2. Both Dependency Injection and Object Manager ways of coding are given below. Using Dependency Injection (DI) Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface & UrlInterface in the constructor of my module’s block … Read more

Magento 2: Get Current Category & Current Product

This article shows how we can get current category and current product data in Magento 2. Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\Registry 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 $_registry; public function __construct( … Read more

Magento 2: Writing Custom Log

In Magento 1, you could simply write your own custom log with the following code: Mage::log("Your Log Message", null, "your_log_file.log"); and it would be saved at MAGENTO_ROOT/var/log/your_log_file.log However, it’s not that simple and quite different in Magento 2. As Magento 2 uses dependency injection design pattern, you need to pass the logger instance through the … Read more