共计 2727 个字符,预计需要花费 7 分钟才能阅读完成。
magento系统中可以设定相关产品,但是产品多的话一个一个设置是非常浪费时间的,如果对相关产品的要求不高的话,下面的代码可以实现这个要求。
只要把一下代码放到upsell.html中即可实现,显示的数量也可以自行调整
[php]
<?php if(count($this->getItemCollection()->getItems())): ?>
<div>
<div><h4><?php echo $this->__(‘You may also be interested in the following product(s)’) ?></h4></div>
<table cellspacing="0" id="upsell-product-table">
<?php // $this->setColumnCount(4); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>
<?php $this->resetItemsIterator() ?>
<?php for($_i=0;$_i<$this->getRowCount();$_i++): ?>
<tr>
<?php for($_j=0;$_j<$this->getColumnCount();$_j++): ?>
<?php if($_link=$this->getIterableItem()): ?>
<td>
<p><a href="<?php echo $_link->getProductUrl() ?>"><img src="<?php echo $this->helper(‘catalog/image’)->init($_link, ‘small_image’)->resize(125) ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($_link->getName()) ?>" /></a></p>
<h5><a href="<?php echo $_link->getProductUrl() ?>"><?php echo $this->htmlEscape($_link->getName()) ?></a></h5>
<?php echo $this->getPriceHtml($_link, true) ?>
<?php echo $this->getReviewsSummaryHtml($_link) ?>
</td>
<?php else: ?>
<td> </td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
<mce:script type="text/javascript"><!–
decorateTable(‘upsell-product-table’)
// –></mce:script>
</div>
<?php else: ?>
<?php
$_helper = $this->helper(‘catalog/output’);
$_product = $this->getProduct();
$categories =$_product->getCategoryCollection()->load();
$mainProd= $_product->getId();
?>
<div>
<div>
<h3><?php echo $this->__(‘You may also be interested in the following product(s)’) ?></h3>
</div>
<table cellspacing="0" id="upsell-product-table">
<tr>
<?php
$jk=0;
if ($categories->count())
foreach ($categories as $_category)
{
$Cnt=0;
$products = Mage::getResourceModel(‘catalog/product_collection’)->addCategoryFilter($_category)->addAttributeToSelect(‘small_image’);
foreach ($products as $productModel )
{
$_product = Mage::getModel(‘catalog/product’)->load($productModel->getId());
$width=125; $height=125;
$_imageUrl = $this->helper(‘catalog/image’)->init($productModel, ‘small_image’)->resize($width, $height);
// echo $mainProd,'<=>’,$productModel->getId();
if($mainProd!=$productModel->getId() && $Cnt++ < 2 && $jk < 3)
{
if($jk == 0)
{?>
<td class=’no-devider’>
<?php
}
else
{?>
<td>
<?php
}
?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src="<?php echo $_imageUrl ?>" width="<?php echo $width ?>" height="<?php echo $height ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
<h4><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h4>
<!–<div><span><span><?php echo "$".$_product->getPrice() ?></span></span></div>–>
</td>
<?php
$jk++;
}
}
}
?>
</tr>
</table>
<mce:script type="text/javascript"><!–
decorateTable(‘upsell-product-table’)
// –></mce:script>
</div>
<?php endif ?>
[/php]