Skip to main content
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
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
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.9k
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
  • 36.1k
23 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
  • 36.1k
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,470
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
  • 853
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
  • 419
16 votes

Magento 2 Store switcher error in "section-config.js" Uncaught TypeError: Cannot read property '*' of undefined(…)

I had the same problem and found out that these two blocks where not called any more due to customizations in the theme. The two blocks are per default added in the <referenceContainer name="...
Anna Völkl's user avatar
  • 17.4k
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
  • 18.1k
15 votes
Accepted

Remove apply discount code from Cart

app/design/frontend/{Vendor}/{theme}/Magento_Checkout/layout/checkout_cart_index.xml <?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...
PЯINCƎ's user avatar
  • 11.7k
13 votes
Accepted

Magento 2 how to get sum of a column?

$collection = $this->customFactory->create()->addFieldToFilter('status',2) ->addFieldToFilter('customer_id',2); $collection->getSelect()->columns(['total' => new \Zend_Db_Expr('...
Rakesh Jesadiya's user avatar
12 votes

Magento 2 How to Remove product information tabs

When looking in /slippers.nl/vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml you can see how it is added to the page. <block class="Magento\Catalog\Block\...
Rann's user avatar
  • 331
12 votes

How to use escapeHtml in Magento 2?

With the latest version of Magento 2.4 now you can directly use $escaper variable to use class \Magento\Framework\Escaper. The $escaper local variable is available inside the any .phtml templates. <...
Prince Patel's user avatar
  • 22.9k
11 votes
Accepted

Magento2: How to remove the newsletter block?

You can remove this block completely via layout with remove="true": app/design/frontend/TemplateMonster/Spiceli/Magento_Newsletter/layout/default.xml <?xml version="1.0"?> <page xmlns:xsi="...
Khoa Truong's user avatar
  • 32.3k
11 votes

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

Declare \Magento\Sales\Api\OrderRepositoryInterface in construct of your class. Try following snippet: protected $orderRepository; public function __construct( \Magento\Sales\Api\...
Ashish Viradiya's user avatar
11 votes
Accepted

Magento2 : Call getViewFileUrl() function in block file

Use \Magento\Framework\View\Asset\Repository class to get view file url in block file protected $_assetRepo; public function __construct( ... \Magento\Framework\View\Asset\Repository $...
Prince Patel's user avatar
  • 22.9k
11 votes

Magento 2 How to get only one field data in collection

Magento has an inbuilt function on Magento\Framework\Data\Collection which provide a specific field value from the collection. You have to use getColumnValues($colName) for getting the specific field ...
Amit Bera's user avatar
  • 77.7k
10 votes
Accepted

magento 2 - How to create a custom "admin" block and display it on an existing Admin page?

Traditional location for admin blocks is (Example): app/code/YourVendor/YourModule/Block/Adminhtml/... If you'd like to use block with phtml template you'd like to extend \Magento\Framework\View\...
Sergii Ivashchenko's user avatar
10 votes

addAttributeToSelect Call to undefined method M2

You have to replace addAttributeToSelect('*') with addFieldToSelect('*') addAttributeToFilter() is used to filter EAV collections. addFieldToFilter() is used to filter Non-EAV collections.
PЯINCƎ's user avatar
  • 11.7k
10 votes
Accepted

How to call phtml file from custom theme to cms block

File location will be app/design/frontend/CustomTheme/CustomChild/Magento_Theme/templates/html/test.phtml calling a block {{block class="Magento\Framework\View\Element\Template" name="testblock" ...
PROGOSTECH's user avatar
10 votes
Accepted

Magento 2 - Call list phtml by passing collection variable

First you need to create block Vendor\Module\Block\ProductList.php by overriding this function _getProductCollection() by extending your class from this Magento\Catalog\Block\Product\ListProduct. ...
Muhammad Hasham's user avatar
10 votes
Accepted

Magento 2 - Call different module block function in my custom phtml

You Can Call Another Module Block Like This :- $block = $this->getLayout()->createBlock('\Vendor\Module\Block\FileName')->yourFunction();
Ronak Rathod's user avatar
  • 6,480
9 votes
Accepted

Difference Between $block and $this magento 2

Regardless of what object in template you use ($block or $this) always will be called method from $block. Magic happens in \Magento\Framework\View\TemplateEngine\Php::__call(). public function ...
yaronish's user avatar
  • 808
9 votes
Accepted

Magento 2: Add block above page-header

Change your default.xml to <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/...
Prince Patel's user avatar
  • 22.9k
9 votes

Invalid template file override Magento 2 block

In the file customer_address_index.xml you can see this: <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/...
raumatbel's user avatar
  • 1,275

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