Magento使用产品集合在模板中调用.phtml文件

Mar*_*tin 7 php object magento

我可以将.phtml文件调用到我的.phtml模板,就像list.phtml一样.

<?php 
  echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();
?>
Run Code Online (Sandbox Code Playgroud)

但在test.phtml中,我无法调用$ _product值.

例如:

<?php 
$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_product): 
?>
Run Code Online (Sandbox Code Playgroud)

作品

<?php echo $_product->getName() ?>
Run Code Online (Sandbox Code Playgroud)

不起作用:

<?php 
      echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();
    ?>
Run Code Online (Sandbox Code Playgroud)

在文件中:test.html : <?php echo $_product->getName() ?>.

我是否必须在每个包含的文件中再次加载产品中的完整集合,如何才能在test.phtml中获得$ _product值最有效的方法?

Moh*_*ora 7

有两种选择:

  1. 您可以Mage::getModel('catalog/product')->load(<product_id>)在foreach循环中每次加载带有id的产品.

  2. 使用下面

echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('goodtest/test.phtml')->toHtml();

代替

echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml();