Page 53 of 69

Magento 2 – How to Call Template Block in phtml File

In this post I am going to explain how to to call template block in phtml file or how to use phtml file in another phtml file. Sometimes you want to get content from another phtml file without defining in layout. In this case, we can use a method called toHtml of a block. Suppose…

Magento 2 – How to get Current Store ID, Code, Name, URL and Website ID

In this post I am going to explain how to get current store information like store id, code, name, url and website id in Magento 2. 1. Using Dependency Injection protected $_storeManager; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, array $data = [] ) { $this->_storeManager = $storeManager; parent::__construct($context, $data); } public function getStore() {…

Magento 2 – How to Check Current URL is Homepage

In this post I am going to explain How to check IsHomePage in Magento 2. Add below code snippet in Block class. protected $_logo; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Theme\Block\Html\Header\Logo $logo, array $data = [] ) { $this->_logo = $logo; parent::__construct($context, $data); } public function isHomePage() { return $this->_logo->isHomePage(); }   Add below code snippet…

Magento 2 – How to Get Module, Controller, Action and Route Name

In this post I am going to explain how to get module, controller, action and route name in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_request; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Request\Http $request, array $data = [] ) { $this->_request = $request; parent::__construct($context, $data); } public function getModuleName()…

Magento 2 – How to get Logo Height, Width, Image URL and Alt Text

In this post I am going to explain how to get logo height, width, image url and alt text in Magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_logo; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Theme\Block\Html\Header\Logo $logo, array $data = [] ) { $this->_logo = $logo; parent::__construct($context, $data); } //…

Magento 2 – How to Get List of Current Store Categories

In this post I am going to explain how to get list of store categories in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_storeManager; protected $_categoryCollection; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection, array $data = [] ) { $this->_storeManager = $storeManager; $this->_categoryCollection = $categoryCollection; parent::__construct($context,…

Magento 2 – How to Get All Categories of Specific Product

In this post I am going to explain how to get all categories of specific product in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_categoryCollectionFactory; protected $_productRepository; public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, \Magento\Catalog\Model\ProductRepository $productRepository, array $data = [] ) { $this->_categoryCollectionFactory = $categoryCollectionFactory; $this->_productRepository = $productRepository;…

Magento 2 – How to Get All Active Payment Methods

In this post I am going to explain how to get all active payment methods in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_paymentConfig; protected $_scopeConfigInterface; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Payment\Model\Config $paymentConfig, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface, array $data = [] ) { $this->_paymentConfig = $paymentConfig; $this->_scopeConfigInterface = $scopeConfigInterface; parent::__construct($context,…

Magento 2 – How to Get All Payment Methods

In this post I am going to explain how to get all payment methods in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_paymentHelper; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Payment\Helper\Data $paymentHelper, array $data = [] ) { $this->_paymentHelper = $paymentHelper; parent::__construct($context, $data); } public function getAllPaymentMethods() { $paymentMethods =…

Magento 2 – How to Get Customer Group Collection

In this post I am going to explain how to get customer group collection. 1. Using Dependency Injection Add below code snippet in Block class. protected $_customerGroupCollection; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroupCollection, array $data = [] ) { $this->_customerGroupCollection = $customerGroupCollection; parent::__construct($context, $data); } public function getCustomerGroupCollection() { $customerGroupsCollection = $this->_customerGroupCollection->toOptionArray(); return $customerGroupsCollection;…

« Older posts Newer posts »