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();?>
2
  • use $this instead of $block, Commented Jun 9, 2017 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
    Commented Jun 9, 2017 at 10:09

3 Answers 3

0

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

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:

<?php 

  $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 />';        
            }
        }

     }
?>
9
  • 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
    Commented Jun 9, 2017 at 10:29
  • this error showing after used this
    – sanjay
    Commented Jun 9, 2017 at 10:29
  • let me check the error Commented Jun 9, 2017 at 10:33
  • @@Prasanta Hatui @you there?
    – sanjay
    Commented Jun 9, 2017 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
    Commented Jun 9, 2017 at 12:40
0

From the thread at Notice: Undefined variable: exist in Magento 2 when using upsell block

echo $this->getLayout()
->createBlock('Magento\Catalog\Block\Product\ProductList\Upsell')
->setData('type', 'upsell')
->setTemplate('Magento_Catalog::product/list/items.phtml')
->toHtml();

So the key is ->setData('type', 'upsell')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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