How to use multiple AND and OR conditions in searchCriteria filter in Magento 2?

In this article, I will show you how you can use multiple OR and AND operation in searchCriteria filter. When we are working with API and working with getList collection using searchCriteria, we sometimes need to build a custom query to get the collection. There are two main part of searchCrieria FilterFilter Group 1. Filter…

Continue Reading How to use multiple AND and OR conditions in searchCriteria filter in Magento 2?
How to use Search Criteria for the custom collection in Magento 2?
How to use Search Criteria for the custom collection in Magento 2?

How to use Search Criteria for the custom collection in Magento 2?

In this article, I will show you, how you can use Search Criteria in the custom collection. Search Criteria is an implementation of the SearchCriteriaInterface class that allows you to build custom requests with different conditions. You can read more at devdoc for Search Criteria Searching with Repositories 1. Here we use getList() method to get custom…

Continue Reading How to use Search Criteria for the custom collection in Magento 2?
Work with HTML and JS in Magento 2
Work with HTML and JS in Magento 2

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?
How to add form key 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
How to get a formatted price with currency in phtml file 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?
When you should run which commands 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?
How to validate forms 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?
How to print logs 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?
How to get the root directory path 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?
How to get the current language of the store ?

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?