A-A+

Magento获取分类的销售排行

2012年04月07日 灵犀一指 Magento获取分类的销售排行已关闭评论 阅读 570 人 次

Magento有个热门商品的插件,但是那个插件不显示产品的销售数量,现在分享下能够指定分类,显示销售数量的方法。
新建一个block,内容:

totalPerPage = ($this->show_total) ? $this->show_total : 8;
$counter = 1;
$catalogid = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();
$catalogid=($catalogid) ? $catalogid : 2;

$_featcategory = Mage::getModel('catalog/category')->load($catalogid); 
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );

$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->addCategoryFilter($_featcategory)   //全部产品的排行
                              ->setOrder('ordered_qty', 'desc');
?>

<ul>

<?php foreach($_productCollection as $product): ?>

<?php if($counter <= $totalPerPage): ?>

<?php $productUrl =  $product->getProductUrl() ?>

<li class="l">
<a onclick="this.target='_blank'" class="hotimg" href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
<img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(77); ?>" alt="<?php echo $product->name ?>"  />
</a>
  <h2><a onclick="this.target='_blank'" href="<?php echo $productUrl ?>" title="View <?php echo $product->name  ?>">
<?php echo Mage::helper("core/string")->truncate($product->name,6,'') ;?>
</a> </h2>
 <span class="price-label"><?php echo $this->__('Special Price:') ?></span>
<span class="bestprice"> <?php echo $this->helper('core')->currency($product->getFinalPrice(),true,false) ?></span>
<span class="selled">已售出: <?php echo (int)$product->ordered_qty ?></span>
</li>
<?php endif; $counter++; ?>
<?php endforeach; ?>
</ul>

然后在magento中需要的地方插入即可,这个我在一个商城中已经用上,没有问题。

评论已关闭!