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

Magento: Delete Orders Programmatically

Here is a quick code to delete orders programmatically in Magento. In Magento, there isn’t the facility to delete orders from admin grid. You need to write custom code to delete orders in Magento. Here is the full code to do so. This code can be saved in a new file in your Magento root … Read more

Magento: Delete Customers Programmatically

Here is a quick code to delete customers programmatically in Magento. You can manually delete customers from customer admin grid. But, sometime you might need to do it automatically through code. Here is the full code to do so. This code can be saved in a new file in your Magento root folder and then … Read more

Magento: Redirecting to Cart page from Checkout page after Place Order

Scenario: I added products to cart. Then did proceed to checkout. I have tried Cash on Delivery and Check / Money Order as Payment method. Everything is fine until the order review section. And then when I click the Place Order button then it gets redirected to Cart page instead of going to Order Success … Read more