Work with HTML and JS in Magento 2

During development in Magento 2, we face many issues related to HTML or JS. Sometimes js and HTML changes are not reflected. I often see that many developer run all the commands after doing changes in HTML or JS files. To reflect Html or js changes you don't need to run any command. You even…

Continue Reading Work with HTML and JS in Magento 2

How to add form key in Magento 2?

In this article, I will show you how you can use form key in your form in phtml file. In Magento, form key is used for preventing Cross-Site Request Forgery. Learn more about cross-site forgery You can use directly getBlockHtml('formkey') to get form key with a hidden field in phtml files like echo $block->getBlockHtml('formkey'); OutPut:…

Continue Reading How to add form key in Magento 2?

How to get a formatted price with currency in phtml file in Magento 2

In this article, I will show you how you can get the formatted price with currency. You can get the formatted price with class Magento\Directory\Model or Magento\Framework\Pricing\PriceCurrencyInterface or Magento\Framework\Pricing\Helper\Data 1. By using class Magento\Framework\Pricing\Helper\Data The best way of doing this by using class Magento\Framework\Pricing\Helper\Data as a dependency. This class has two methods currency and currencyByStore.…

Continue Reading How to get a formatted price with currency in phtml file in Magento 2

When you should run which commands in Magento 2?

I often see that many new Magento developer run setup upgrade command every time when they changed something from js or HTML. In this article, I will discuss about which changes need which commands to reflect changes on the frontend or backend of Magento 2. 1) php bin/magento setup:upgrade Short Form: php bin/magento set:up You…

Continue Reading When you should run which commands in Magento 2?

How to validate forms in Magento 2?

In this article, I will show you how you can validate the custom form with the default Magento 2 validation library. Step 1: First of all you need to add "data-mage-init" attribute with "validation" alias in your custom form. <form class="form my-form" action="" method="post" data-mage-init='{"validation":{}}'> //Form code </form> The validation alias invokes js lib/web/mage/validation/validation.js which…

Continue Reading How to validate forms in Magento 2?

How to print logs in Magento 2?

Logs in Magento 2 consist of system information records for the analysis in the future. One of the most common examples of such events is the error log. Developers know the pain of errors and the process to deliver a working solution. Debugging can be made easier for them by custom logs. It helps to…

Continue Reading How to print logs in Magento 2?

How to get the root directory path in Magento 2?

You can use \Magento\Framework\Filesystem\DirectoryList class to get a directory path. You can get directory path of root, media, pub, var etc... Here is an example of how you can get root directory path. By ObjectManager: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList'); echo $rootPath = $directory->getRoot(); By dependency Injection: protected $_dir; public function __construct( ... \Magento\Framework\Filesystem\DirectoryList…

Continue Reading How to get the root directory path in Magento 2?

How to get the current language of the store?

You can use both \Magento\Store\Api\Data\StoreInterface or Magento\Framework\Locale\Resolver class to get the current language of the store. 1) By using \Magento\Store\Api\Data\StoreInterface Class With objectManager: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $store = $objectManager->get('Magento\Store\Api\Data\StoreInterface'); echo $store->getLocaleCode(); With Dependency Injection: protected $_store; public function __construct( ... \Magento\Store\Api\Data\StoreInterface $store, ... ) { ... $this->_store = $store; ... } Now you can use getLocaleCode() function to get…

Continue Reading How to get the current language of the store?

How to check if the current area is frontend or backend?

Use Magento\Framework\App\State class to get the area code of the current page. I will show you how you can get the current area by object manager and with dependency injection. With ObjectManager: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $state = $objectManager->get('Magento\Framework\App\State'); echo $state->getAreaCode(); With Dependency Injection: protected $_state; public function __construct ( \Magento\Framework\App\State $state ) { $this->_state =…

Continue Reading How to check if the current area is frontend or backend?