Magento-将随机产品添加到.../template/catalog/product/view.phtml [英] Magento - Adding a random product to .../template/catalog/product/view.phtml
问题描述
我想在.../template/catalog/product/view.phtml模板上为随机产品添加3个链接.
I would like to add 3 links to random products on the .../template/catalog/product/view.phtml template.
基于list.phtml,我尝试添加:
Based on list.phtml, I tried adding:
<?php
$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_randomProduct) {
echo $_randomProduct->getProductUrl();
}
?>
但是我没有得到URL的列表.如何获得其他产品的链接和图像的数组?
But I'm not getting a list back of URL's. How can I get an array of other product's links and images?
推荐答案
该代码使您失望,因为view.phtml中的$this
与list.phtml中的$this
不同.
如果您想获得一些与当前产品类别相关的产品,请尝试以下操作:
That code lets you down because $this
in view.phtml is not the same $this
as in list.phtml.
If you wanted to get some products relevant to the current product's category try this:
$_productCollection = $_product->getCategory()->getProductCollection();
或者,如果您不在乎它们的来源:
Alternatively if you didn't care where they come from:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->setStore(Mage::app()->getStore());
要获得随机性,此小技巧将有所帮助:
To get the randomness this little trick will help:
$_productCollection->setPageSize(3)
->getSelect()->order('RAND()');
现在您可以在foreach循环中使用该集合.
Now you can use the collection in a foreach loop.
这篇关于Magento-将随机产品添加到.../template/catalog/product/view.phtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!