Tag: Magento 2 (page 17 of 26)

Magento 2 – How to Get List of All Countries

In this post I am going to explain how to get country collection in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_countryCollectionFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory, array $data = [] ) { $this->_countryCollectionFactory = $countryCollectionFactory; parent::__construct($context, $data); } public function getCountryCollection() { $collection = $this->_countryCollectionFactory->create()->loadByStore();…

Magento 2 – How to Get Formatted Price With Currency

In this post I am going to explain how to get formatted price with currency in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_priceHelper; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Pricing\Helper\Data $priceHelper, array $data = [] ) { $this->_priceHelper = $priceHelper; parent::__construct($context, $data); } public function getFormattedPrice($price) { $formattedPrice…

Magento 2 – How to Get All Images of Product by Product ID

In this post I am going to explain how to get all images of product by product id in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_productFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ProductFactory $productFactory, array $data = [] ) { $this->_productFactory = $productFactory; $this->_storeManager = $storeManager; parent::__construct($context, $data);…

Magento 2 – How to Get Subtotal and Grand total in Checkout Page using Knockout JS

In this post I am going to explain how to get subtotal and grand total in checkout page using knockout js in Magento 2. Here, I am using my custom module – Ht_Mymodule In order to display content on checkout page, first we need to add block in checkout_index_index.xml file. So create layout file checkout_index_index.xml…

Magento 2 – How to Get Country Name from Country Code

In this post I am going to explain how to get country name from country code in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_countryFactory; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Directory\Model\CountryFactory $countryFactory, array $data = [] ) { $this->_countryFactory = $countryFactory; parent::__construct($context, $data); } public function getCountryName($countryCode) {…

Magento 2 – How To Override Registration Page Template

In this post I am going to explain how to override registration page template (register.phtml) in magento 2. Here, I am using my custom module – Ht_Mymodule First create xml file customer_account_create.xml and add below code snippet. File Path: app/code/Ht/Mymodule/view/frontend/layout/customer_account_create.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_form_register”> <action method=”setTemplate”> <argument name=”template” xsi:type=”string”>Ht_Mymodule::form/register.phtml</argument> </action> </referenceBlock>…

Magento 2 – How to Check file Exists in Directory

In this post I am going to explain how to check file exists in directory using isExists method in magento 2. Use below code snippet to check file is exists or not. private $_fileDriver; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Filesystem\Driver\File $fileDriver, array $data = [] ) { $this->_fileDriver = $fileDriver; parent::__construct($context, $data); } public function…

Magento 2 – How to Get URL in JS File

In this post I am going to explain how to get URL in js file in magento 2. Use below code snippet to get URL in the js file. define([ ‘jquery’, ‘mage/url’ ], function($, url) { // Login Page URL var loginUrl = url.build(‘customer/account/login’); console.log(loginUrl); // Customer Account URL var accountUrl = url.build(‘customer/account’); console.log(accountUrl); });…

Magento 2 – How to Write log

Logging is an important part of any development process. Logs assist you to spot an error and the reason for it. Magento 2 contains built-in logging solution based on Monolog Library. To start working with a logger, you must create an instance of \Psr\Log\LoggerInterface. With this interface, you can call the following functions to write…

Older posts Newer posts