Get admin URL in Magento 2

We can use \Magento\Backend\Helper\Data class to get Admin Url. /** * @var Magento\Backend\Helper\Data */ private $backendHelper; public function __construct( ... \Magento\Backend\Helper\Data $backendHelper ... ) { ... $this->backendHelper = $backendHelper; ... } public function getAdminUrl() { retrun $this->backendHelper->getHomePageUrl(); } You can get admin URL by getHomePageUrl() function of backend helper class Here you will get admin…

Continue Reading Get admin URL in Magento 2

Add a module in the composer with GitHub repository Magento 2

In this article, I will show you how we can add -magento 2 module in the composer. Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Read more 1. Add module repository into Github First of…

Continue Reading Add a module in the composer with GitHub repository Magento 2

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

Notice: unserialize(); Error at offset in Magento 2.2

In this article, I will show how we can solve unserialize error in Magento 2. As per Magento 2.2 release note, Magento removes serialize/unserialize from most the code to improve protection against remote code execution attacks. Magento 2.2 Release Note: In general, we’ve removed serialize/unserialize from most the code to improve protection against remote code…

Continue Reading Notice: unserialize(); Error at offset in Magento 2.2

Get order id by increment id Magento 2

In this article, I will show you how you can load order by increment id in Magento 2 We can use the order interface Magento\Sales\Api\Data\OrderInterface to load order by increment id. By Object Manager: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $incrementId = "100009633"; $orderInterface = $objectManager->create('Magento\Sales\Api\Data\OrderInterface'); $order = $orderInterface->loadByIncrementId($incrementId); // Print Order Object print_r($order->debug()); // Get Order Id…

Continue Reading Get order id by increment id 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
Check Customer is logged in using knockout js 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?
How to add a new page layout in 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?
How to convert date according to specific locale in Magento 2?
How to convert date according to specific locale in Magento 2?

How to convert date according to specific locale in Magento 2?

Sometimes we need to convert date formate to locale. We can use \Magento\Framework\Stdlib\DateTime\TimezoneInterface class to convert dare according to locale. Using Object Manager: $date = new \DateTime('2019-11-21'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $formatDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\TimezoneInterface') ->formatDate( $date, \IntlDateFormatter::FULL, false ); Using Dependency Injection: namespace MagePrince\Testing\Model; class FormatDatLocale { private $timezoneInterface; public function __construct( \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface ) {…

Continue Reading How to convert date according to specific locale in Magento 2?
How to set custom date format in Magento 2?
How to set custom date format in Magento 2?

How to set custom date format in Magento 2?

We can use Magento\Framework\Stdlib\DateTime\DateTimeFactory class to set custom date format. During development, we need to set a custom date format to display in the frontend or need to pass the custom data to APIs. So here is the code to set custom date format. namespace MagePrince\Testing\Model; class FormatDate { private $dateTimeFactory; public function __construct( \Magento\Framework\Stdlib\DateTime\DateTimeFactory…

Continue Reading How to set custom date format in Magento 2?