Hot answers tagged

73 votes
Accepted

Add a static image on PHTML page and CMS Blocks : Magento2

image url Syntax is ok for both static block and phtml, But make sure that when you write this code in static block <img src="{{view url='images/slide-bg.jpg'}}" alt="test" />...
Keyur Shah's user avatar
68 votes
Accepted

How to get root directory path in a phtml magento 2?

Class \Magento\Framework\Filesystem\DirectoryList is used to get path like root, media, var etc This will get root directory of your project like this /var/www/html/myproject By ObjectManager $...
Prince Patel's user avatar
  • 22.7k
65 votes
Accepted

Get Current URL from a PHTML - Magento 2

It's working for me $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]) Also instead $this you can use $block.
Ihor Klymchuk's user avatar
49 votes
Accepted

magento 2: I want display a image in the module template phtml file

Yes you can store images in your module web directories depending on the area of your templates. The path is: Vendor/Module/view/{frontend|adminhtml}/web/images/ Then in your *.phtml files you can ...
Miroslav Petroff's user avatar
47 votes
Accepted

call static block in view.phtml insted of phtml file

You can use the below code: in .phtml file : <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); ?> sample : <?php echo $...
Qaisar Satti's user avatar
  • 32.5k
41 votes
Accepted

Magento 2 How can we get product url with product collection?

$productCollection = $productObj; Gives from product collection url using loop the collection, foreach($productCollection as $_product){ echo $_product->getProductUrl(); }
Rakesh Jesadiya's user avatar
39 votes

How do I get the current product?

Although other answers are correct, they're not the recommended/proper solution either. Using the ObjectManager is absolutely prohibited in Magento 2. So please don't rely on this solution, but use ...
Wesley Vestjens's user avatar
38 votes
Accepted

How to call helper method in .phtml file

You should not use helper calls directly in the template. Have your helper instance provided as a dependency to the block that renders the template and create a method in your block that calls the ...
Marius's user avatar
  • 198k
37 votes

Magento 2: Changing a Block's Template

To understand the difference between <arguments> and <action> you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always ...
Giel Berkers's user avatar
  • 12.2k
34 votes
Accepted

Magento 2 Templates: Use `$block` or `$this`?

In a template file, $block and $this->_currentBlock are the same thing. Take a look at the render method in the Php.php template engine. Before including the template file, this happens: $this-&...
Marius's user avatar
  • 198k
33 votes

How to call helper method in .phtml file

You have to use like this: $helper = $this->helper('{Vendor}\{Module}\Helper\Data'); $values = $helper->YourHelperMethod();
Chirag Prajapati's user avatar
32 votes
Accepted

Magento 2 Get order total on success page?

Magento 2.1 The block mentioned below is now Magento\Checkout\Block\Onepage\Success Magento 2.0 The only thing you can retrieve natively on this page is the order id using the getRealOrderId() ...
Raphael at Digital Pianism's user avatar
30 votes

How to get root directory path in a phtml magento 2?

You can use magento 2 default variable to get absolute path of Magento directory echo BP;
Sunil Patel's user avatar
  • 2,087
25 votes

Add a static image on PHTML page and CMS Blocks : Magento2

For your custom module create a directory structure like Company/Module/view/frontend/web/images/image-icon.png and in phtml use following code <img src='<?php echo $this->getViewFileUrl('...
Manish's user avatar
  • 3,106
25 votes

Magento 2: Call phtml file in static block or cms page

You can use Magento\Framework\View\Element\Template class to call phtml files {{block class="Magento\Framework\View\Element\Template" template="Vendor_Module::myfiles/myfile.phtml"}} To call ...
Prince Patel's user avatar
  • 22.7k
23 votes
Accepted

Magento 2 Override default header template

copy templates folder from vendor/magento/module-theme/view/frontend/templates and paste into app/design/frontend/MySpaceName/MyTheme/Magento_Theme/paste templates folder here
Qaisar Satti's user avatar
  • 32.5k
23 votes
Accepted

How to load a phtml file only for homepage? (Magento 2)

As per as, Magento2, default.xml is call at every page.If you add this code at default.xml then it would be automatically call every pages of your current theme. If you know about Magento 1.x ...
Amit Bera's user avatar
  • 77.4k
22 votes

Magento 2: How to get current date, date time with format in .phtml file?

protected $_date; public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date) { $this->_date = $date; } then use like this $this->_date->date()->format('Y-...
ManiMaran A's user avatar
  • 1,126
21 votes

Magento 2 : load order by id in customer account order view

Try following way: protected $orderRepository; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Api\OrderRepositoryInterface $...
Sohel Rana's user avatar
  • 35.7k
20 votes

How do I get the current product?

If you are using Magento 2.1 or major, you can use this helper because the old method was deprecated. ... use Magento\Catalog\Helper\Data; ... public function __construct( Context $context, ...
Rafael Corrêa Gomes's user avatar
19 votes

Magento 2: Changing a Block's Template

The following worked for me in Magento EE 2.2.3 <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/...
diazwatson's user avatar
  • 2,420
19 votes
Accepted

How do I get the current product?

In order to get the current product, one of the recommended ways is: Extend or use block class: Magento\Catalog\Block\Product\View\AbstractView. Get product using: $block->getProduct() in your ...
Rahil Patel's user avatar
19 votes

Why my overriding of the success.phtml is not working?

In Magento 2.4.1 there are two success.phtml templates: module-checkout/view/frontend/templates/success.phtml module-inventory-in-store-pickup-frontend/view/frontend/templates/success.phtml In my ...
SantiBM's user avatar
  • 1,351
18 votes
Accepted

How to get the Description of a Magento\Catalog\Model\Product?

In Store > Attributes > Product > Description > Storefront Properties set Visible on Catalog Pages on Storefront and Used in Product Listing to TRUE. In your code list.phtml you can use: <?php ...
Rafael Corrêa Gomes's user avatar
18 votes
Accepted

Magento 2 Get Cart Quote Total in minicart.phtml

You have to just keep below line in your minicart.phtml file to get updated subtotal , This Below line is workig for all cases if cache is enable its working fine, <span data-bind="html: ...
Rakesh Jesadiya's user avatar
18 votes
Accepted

How render product wishlist link functionality in custom template (phtml) in magento 2

You can try something like the following code: <a href="#" data-post='<?php echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) ?>' class="action towishlist" ...
Krishna ijjada's user avatar
15 votes
Accepted

CMS Page URL from ID in phtml (M2)

By using object manager you can get CMS page url, if you have already id of cms page. $pageId = 1; $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); or $this->...
Chirag Prajapati's user avatar
15 votes
Accepted

How to use escapeHtml in Magento 2?

/** * Escaper * * @var \Magento\Framework\Escaper */ protected $_escaper; public function __construct( \Magento\Framework\Escaper $_escaper ) { $this->_escaper=$_escaper } Now you can ...
Keyur Shah's user avatar
14 votes

Add a static image on PHTML page and CMS Blocks : Magento2

Easy solution: Put your image on Your Magento 2 Path: /app/design/frontend/< Vendor >/< theme >/web/images Use this code to call the image in phtml file: echo $block->...
Deepak Bhatta's user avatar
14 votes
Accepted

Call Custom phtml file in static block

You don't need to include the theme's directory in the template path, Magento will automatically use the current theme and fallback structure. For example: {{block type="core/template" template="page/...
Ben Crook's user avatar
  • 15.7k

Only top scored, non community-wiki answers of a minimum length are eligible