Get Module’s directory path in Magento 2

Sometimes we need to get module's directory path mainly when we working with libraries. In this article, I will show how we can get the module's directory path. Check this article for Get root directory path in Magento 2 Use Magento\Framework\Module\Dir class to get the module's directory paths. namespace MagePrince\Testing\Model; class Directories { private $directory;…

Continue Reading Get Module’s directory path in Magento 2

Create dynamically link in customer account navigation in Magento 2

We can create custom customer account link with reference block customer_account_navigation with a custom block class 1) Add block class in customer_account.xml File: app/code/Vendor/Module/view/frontend/layout/customer_account.xml <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="customer_account_navigation"> <block class="{vendor}\{Module}\Block\Customer\Link" name="customer-account-navigation-new-product-link" after="-"> <arguments> <argument name="label" xsi:type="string">Some link</argument> <argument name="path" xsi:type="string">customer/somelink/index</argument> </arguments> </block> </referenceBlock> </body> </page> 2) Now create Link.php namespace Vendor\Module\Block\Customer;…

Continue Reading Create dynamically link in customer account navigation in Magento 2

Check Customer is logged in using knockout js Magento 2

We can use Magento_Customer/js/model/customercomponent to check if the customer is logged in or not in knockout js. Check full component here customer.js We can use isLoggedIn() method. It will return true if customer is loggedIn and return false if customer is not loggedIn Check below code snippet of JS: define([ 'uiComponent', 'Magento_Customer/js/model/customer' ], function (Component,…

Continue Reading Check Customer is logged in using knockout js Magento 2

How to add a new page layout in Magento 2?

In this article, I am going to learn that How we can create a new layout in Magento 2. There are 5 default layouts available in Magento Empty1 column2 columns with left bar2 columns with right bar3 columns During frontend development, Sometimes we need to create our own layout page for customization. So follow the…

Continue Reading How to add a new page layout 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 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?