Hot answers tagged

82 votes
Accepted

magento 2 how to call any block function in phtml

Try like this. For ex your block class is <?php namespace Company\Helloworld\Block; use Magento\Framework\View\Element\Template; class Main extends Template { public function ...
Ashish Madankar M2 Professiona's user avatar
59 votes
Accepted

Fatal Error Call to a member function dispatch() while call my Block in magento 2

The problem is that your constructor does not match the parent class constructor. To fix that you need to update your constructor: public function __construct( \Magento\Framework\View\Element\...
Raphael at Digital Pianism's user avatar
57 votes

Magento 2: template security: which method to use?

The escaping methods in AbstractBlock all delegate calls to Magento\Framework\Escaper, so you'll find an overview there. Let's look at the public methods and their documentation: escapeHtml() /** *...
Fabian Schmengler's user avatar
45 votes
Accepted

Move blocks to other container how to

There is a new move node in the layout XML that we have access to in M2. This node sets the declared block or container element as a child of another element in the specified order. Example: <move ...
Tyler Craft'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

Magento 2: get the current currency code

In a block In Magento 2, you can use the \Magento\Store\Model\StoreManagerInterface which is stored in an accessible variable $_storeManager for every class extending \Magento\Framework\View\Element\...
Raphael at Digital Pianism's user avatar
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
32 votes
Accepted

DEBUG (7): Security problem: block_name has not been whitelisted. (Magento system.log)

This message means that one of the blocks that is used in your Magento store is not on the whitelist. With Security Patch SUPEE-6788 and Magento CE 1.9.2.2 a new whitelist for blocks was introduced. ...
MatthijsIJ's user avatar
  • 3,622
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
31 votes
Accepted

When it is good to use viewModel ? What is difference between viewModel and Block?

Magento veteran Jessie has a well famous blog on viewModel which you can find it here. It answers your question at the end. It says: When to use ViewModels? When would you want to use ...
Rajeev K Tomy's user avatar
28 votes

Magento 2: How to add additional block on checkout page?

Here I give example to show custom block above shipping method of checkout Create di.xml at app/code/Vendor/Module/etc/frontend/di.xml <?xml version="1.0"?> <config xmlns:xsi=&...
Prince Patel's user avatar
  • 22.7k
26 votes
Accepted

How to remove block from left or right panel in magento 2?

Its almost the very same way <referenceBlock name="block.name.wantoberemoved" remove="true"/> Remove compare products from sidebar <referenceBlock name="catalog.compare.sidebar" remove="...
Haijerome's user avatar
  • 2,181
26 votes
Accepted

Magento 2: How to add text within container via XML

You need to use the argument directly without the array. Instead of <arguments> <argument name="data" xsi:type="array"> <item name="text" xsi:type="string">About ...
Raphael at Digital Pianism's user avatar
25 votes
Accepted

How do I override/rewrite a block class in Magento 1?

Every block, or group of blocks is declared in the config.xml file of a module like this (inside the <global> tag). Here is an example from the catalog module <blocks><!-- marks ...
Marius's user avatar
  • 198k
25 votes
Accepted

Customise a custom product attribute input renderer in Magento 2 the right way

TL;DR: If you need to customise your custom attribute in product edit form Magento 2, use PHP modifier in UI components instead of input renderer. I found the solution myself. As we all knew that ...
Toan Nguyen's user avatar
  • 3,049
25 votes
Accepted

Change page title in controler or block

You can do it in different ways - it's enough to use only one: 1. From controller /** @var \Magento\Framework\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $...
Sohel Rana's user avatar
  • 35.7k
23 votes
Accepted

Set and get block arguments programatically

If you look further in the code, in the \Magento\Framework\View\Layout\Generator\Block class, the createBlock function only adds data from the $arguements['data'] element. So, I think you should ...
Jackie Lowery's user avatar
22 votes

Magento 2 How to Remove product information tabs

You can use the following in your layout file to remove the "More Information" tab: <referenceBlock name="product.attributes" remove="true" /> If you need to remove the "Details" tab: <...
Raphael at Digital Pianism's user avatar
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
20 votes
Accepted

How to override blocks in v2.1

Override block: Create your own module under app/code folder. We can use preference to override the class in Magento 2. app/code/Vendor/Module/etc/di.xml <?xml version="1.0"?> <config ...
Khoa TruongDinh'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

Magento 2: Remove block depending on a config setting

I couldn't find a way to do this with layout either but here is an example of a way you can do it with observers (providing they extend the Template block)... Create your events.xml in etc/events.xml ...
Smartie's user avatar
  • 3,240
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

How to find name of block

On top of what everyone else said, you can use the following code in the template file to find out the block name: $this->getNameInLayout();
Raphael at Digital Pianism's user avatar
18 votes
Accepted

Magento 2: How to call custom phtml file in another phtml file?

Replace below code with your block code namespace Lapisbard\General\Block; use Magento\Customer\Model\Session; class CustomerAccount extends \Magento\Framework\View\Element\Template { public ...
John's user avatar
  • 863
18 votes

How to override Virtual Type class in Magento 2

You have to use: <preference for="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Vendor\Module\Block\Product\ListProductSearch" /> as Raphael mentioned. To make this singular ...
Bartłomiej Szubert's user avatar
18 votes
Accepted

Magento 2: template security: which method to use?

This is for Magento 2.0. For 2.1, refer to Fabian's answer escapeHtml Use this function in the case of a string output that should not contain HTML. Example: <span class='label'><?php ...
Raphael at Digital Pianism's user avatar
17 votes
Accepted

Add WYSIWYG Editor to Block Form in Magento 2

Discovered I was missing the Config setting: /** * @var \Magento\Cms\Model\Wysiwyg\Config */ protected $_wysiwygConfig; /** * @param \Magento\Backend\Block\Template\Context $context * @param \...
tim.baker's user avatar
  • 843
17 votes

Magento 2.2.0 - Display Recently Viewed Products block on Product Detail page

Unfortunately the accepted answer didn't work for me and I wasn't able to use widget instance layout updates in my use case If for whatever reason you need to add a 'Recently Viewed' products block ...
Chedaroo's user avatar
  • 409

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