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">
       <argument name="type" xsi:type="string">upsell</argument>

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();?>
  • use $this instead of $block, – Prasanta Hatui Jun 9 '17 at 10:03
  • @ Prasanta Hatui @ No luck it's not working, I do not why it's happen , code is good becouse if i call other template instead of itmes.phtml it's working – sanjay Jun 9 '17 at 10:09

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();?>
| improve this answer | |
  • what is new in this code – sanjay Jun 9 '17 at 9:56
  • Jagriti Joshi@No luck it's not working – sanjay Jun 9 '17 at 10:08

We can not call upsell block this way from phtml file. Because upsell products are associated with a product.

It will working fine when calling from xml because it retuns upsell products for current product.

If you want to get upsell products from current product then you can try below code:


  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $currentProduct = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product

      if ($currentProduct) {

        $upSellProducts = $currentProduct->getUpSellProducts();

        if (!empty($upSellProducts)) {
            echo '<h3>UpSell Products</h3>';
            foreach ($upSellProducts as $upSellProduct) {
                $productId = $upSellProduct->getId();
                $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); 
                echo $product->getName().'<br />';
                echo $product->getSku().'<br />';
                echo $product->getProductUrl().'<br />';
                echo $product->getImage().'<br />';        

| improve this answer | |
  • Exception #0 (Exception): Notice: Undefined property: Magento\ProductVideo\Block\Product\View\Gallery\Interceptor::$frameworkViewLayout in /opt/lampp/htdocs/daydaybag/vendor/magento/framework/View/TemplateEngine/Php.php on line 110 – sanjay Jun 9 '17 at 10:29
  • this error showing after used this – sanjay Jun 9 '17 at 10:29
  • let me check the error – Prasanta Hatui Jun 9 '17 at 10:33
  • @@Prasanta Hatui @you there? – sanjay Jun 9 '17 at 12:07
  • @@Prasanta Hatui @you answer is older . I have used this but this code only return data , does not result default product data – sanjay Jun 9 '17 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.