优化你的Magento:怎样随机显示magento里面相关产品(Related Products)

2,192 人次阅读
没有评论

共计 2120 个字符,预计需要花费 6 分钟才能阅读完成。

在magento里面相关产品(Related Products),推荐销售产品(Up-sells Products),交叉销售产品(Cross-sells Products)都是在后台手动设置的,这篇文章教大家如何随即显示magento Up-sells 产品,修改后的结果是magento的推荐产品里面随即显示当前目录下产品,这样节省了一大笔时间。当然,修改相关产品和推荐产品的方法也是一样。
1.首先找到对应模板下的upsell.phtml文件,app/design/frontend/default/yourthemes/template/catalog/product/list/upsell.phtml
2.打开编辑,用下面代码替换掉原来的内容:

< ?php
$_related = $this->getProduct();
// get the parent id to skip
$_parentid = $_related->getId();

if ($_related) {
// get collection of categories this product is associated with
$categories =$_related->getCategoryCollection()
->setPage(1, 1)
->load();

// if the product is associated with any category
if ($categories->count())
foreach ($categories as $_category)
{
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());

?>

<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">

<tr>

< ?php

$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$products = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category)
->addAttributeToFilter('visibility', $visibility)
->addAttributeToSelect('small_image');

$products->getSelect()->limit(5);

// count the number of displayed products
$_i=0;

foreach ( $products as $productModel )       {
$_related = Mage::getModel('catalog/product')->load($productModel->getId());
$_realtedid = $_related->getId();

// prevents displaying the same product your are already on
if ($_realtedid != $_parentid && $_i&lt;4):

?>

<td>
<p><a href="<?php echo $_related->getProductUrl() ?>"><img src="<?php echo $this-/>helper('catalog/image')->init($_related, 'small_image')->resize(125) ?>" width="125" height="125" alt="< ?php echo $this->htmlEscape($_related->getName()) ?>" /></a></p>
<h5><a href="<?php echo $_related->getProductUrl() ?>">< ?php echo $this->htmlEscape($_related->getName()) ?></a></h5>

< ?php echo $this->getPriceHtml($_related, true) ?>
< ?php echo $this->getReviewsSummaryHtml($_related) ?>
</td>

< ?php
// increment displayed products
$_i++;
endif;
}
?>

</tr>

< ?php }
}
?>
</table>
<script type="text/javascript">decorateTable('upsell-product-table')</script>
</div>

3.保存后上传,清除缓存刷新前台即可看到效果。

正文完
 0
评论(没有评论)