How to add custom block for shipping methods in onepage checkout in Magento 2
To add custom block for shipping methods in onepage checkout in Magento 2, you should do the following actions: Continue Reading
To add custom block for shipping methods in onepage checkout in Magento 2, you should do the following actions: Continue Reading
Unfortunately, the process is not as easy as in Magento 1 ($crumbs = Mage::app()->getLayout->getBlock(‘breadcrumbs’);). In Magento 2, it depends on where you are going to instantiate it from. To create an instance from another block, use the following code:
1 |
$this->getLayout()->createBlock('Full\Block\Class\Name\Here'); |
from a controller:
1 |
$this->_view->getLayout()->createBlock('Full\Block\Class\Name\Here'); |
from a model and a helper:
1 |
$this->_blockFactory->createBlock('Full\Block\Class\Name\Here'); |
Please note that in a case of the model you have to create _blockFactory (a protected member), and inject a \Magento\Framework\View\Element\BlockFactory instance in the constructor, assigning it to the member var. For instance:
1 |
protected $_blockFactory; public function __construct( ..., \Magento\Framework\View\Element\BlockFactory $blockFactory, .... ){ .... $this->_blockFactory = $blockFactory; .... } |
To add a custom block to specific product type view pages, use product type dependent layout updates catalog_product_view_type_{prodyct_type}.xml (catalog_product_view_type_ grouped.xml). For instance:
1 |
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd"> <referenceContainer name="content"> <block class="..." /> </referenceContainer> </layout> |