Magento – Magento2: Calling upsell block from phtml is not working.

blockscustom blockmagento-2.1

I want get up-sell product block. when I am working with XML script block is working. but when I am trying to this in .phtml file it's showing nothing .I do not what is wrong with .phtml and XML data is working good .

step1: For xml this code working good

<block class="Magento\Catalog\Block\Product\ProductList\Upsell" name="product.info.upsell" template="Magento_Catalog::product/list/items.phtml" after="product.info.media.video">
    <arguments>
       <argument name="type" xsi:type="string">upsell</argument>
    </arguments>
</block>

Step:2 for .phtml file this code is showing blank

 <?php echo $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ProductList\Upsell')->setTemplate('Magento_Catalog::product/list/items.phtml')->toHtml();?>

Best Answer

use above code in phtml file:-

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\ProductList\Upsell")->setTemplate("Magento_Catalog::product/list/items.phtml")->toHtml();?>
Related Topic