以下代码在magento1.4.1.0中运行没有什么问题。将代码放在产品详细页的任何位置,如view.phtml或者media.phtml中。
<!--for show other product-->
<?php $categories = $_product->getCategoryIds(); ?>
<?php
$result = array();
foreach($categories as $cat_id) {
$category = Mage::getModel('catalog/category');
$category->load($cat_id);
$collection = $category->getProductCollection();
foreach ($collection as $product) {
$result[] = $product->getId();
}
}
?>
<div class="box-others-also-like">
<ul>
<?php
if(sizeof($result) >= 5)
{
$ourneed = array_rand($result,5);
foreach($ourneed as $cc)
{
$thisproduct= Mage::getModel('catalog/product')->load($result[$cc]);
?>
<li>
<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a>
</li>
<?php } ?>
<?php
}else
{
foreach($result as $cc)
{
$thisproduct= Mage::getModel('catalog/product')->load($cc);
?>
<li>
<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a>
</li>
<?php
}
}
?>
</ul>
</div>
<!--for show other product-->
作者: Sjolzy
--EOF--