畅销产品是一类人逛Magento商店时会询问和浏览的。默认安装就已经有畅销产品这个功能……但是需要在CMS页面定义一个静态块。我们打算把它提升一下。

Magento已经使用了畅销产品的聚合,你可以在Admin->Reports->Products->Bestsellers下确认。得益于这个被聚合的数据,我们不再需要确认所有订单来知道哪件产品卖得最多。通过各种聚合数据,我们能够获取从开始或者特定时间最受欢迎的产品。

有很多实现的方法。我找到对我最有用的就是每月畅销产品的集合。这个方法快速而且即使聚合表为空时也可以访问产品。

class Alwayly_Luckcy_Block_Bestsellers extends Mage_Core_Block_Template
{
    public function getBestsellerProducts()
    {
        $storeId = (int) Mage::app()->getStore()->getId();
 
        // Date
        $date = new Zend_Date();
        $toDate = $date->setDay(1)->getDate()->get('Y-MM-dd');
        $fromDate = $date->subMonth(1)->getDate()->get('Y-MM-dd');
 
        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addStoreFilter()
            ->addPriceData()
            ->addTaxPercents()
            ->addUrlRewrite()
            ->setPageSize(6);
 
        $collection->getSelect()
            ->joinLeft(
                array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
                "e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId} AND aggregation.period BETWEEN '{$fromDate}' AND '{$toDate}'",
                array('SUM(aggregation.qty_ordered) AS sold_quantity')
            )
            ->group('e.entity_id')
            ->order(array('sold_quantity DESC', 'e.created_at'));
 
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
 
        return $collection;
    }
}

这段代码的作用是返回上月卖得最多的产品的集合。如果这个逻辑将是或者被使用超过一次,推荐将逻辑加入到分类产品集合文件(扩展这个文件或者使用事件)并在构建集合时作为方法来调用。现在,当产品集合准备好了,这里有更多的方法来将它们展示到前台。我们就以修改首页为例。后台进入CMS->Page,选择Identifier为'home'的那项。用下面的代码替代content。

<div class="col-left side-col">
    <p class="home-callout"> 
    <p class="home-callout"><img src="{{skin url='images/ph_callout_left_rebel.jpg'}}" alt="" border="0" /></p>
    {{block type="tag/popular" template=“tag/popular.phtml"}}
</div>
<div class="home-spot">
    <p class="home-callout"><img src="{{skin url='images/home_main_callout.jpg'}}" alt="" width="470" border="0" /></p>
    <p class="home-callout"><img src="{{skin url='images/free_shipping_callout.jpg'}}" alt="" width="470" border="0" /></p>
    {{block type="damir/bestsellers" template=“luckcy/bestsellers.phtml"}}

上面的代码包含了畅销产品块。最后一件事就是创建模版文件。模版文件使用了CMS页面同样的html结构:


<div class="box best-selling">
<h3>Best Selling Products</h3>
<table border="0" cellspacing="0">
    <tbody>
    <?php $counter=0; foreach ($this->getBestsellerProducts() as $product): ?>
        <?php if ($counter%2 == 0): ?><tr class="<?php echo $counter%4 ? 'even' : 'odd'; ?>"><?php endif ?>
        <td>
            <a href="<?php echo $product->getProductUrl() ?>"><img class="product-img" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(99); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" width="95" border="0" /></a>
            <div class="product-description">
                <p><a href="<?php echo $product--&gtgetProductUrl() ?>"><?php echo $this->stripTags($product->getName(), null, true); ?></a></p>
            </div>
        </td>
        <?php if ($counter++%2): ?></tr><?php endif ?>
    <?php endforeach; ?>
    </tbody>
</table>
</div>

这个例子使用的是每月聚合,我觉得是最实用的。这里也有每日聚合和每年聚合,但是使用得并不多。许多插件都有同样的功能,只是进行了些包装,最底层的逻辑都是一样的。

由于强烈不推荐修改核心文件,所以每个项目必须至少有一个模型来放置自己的块。如果你对如何创建模块不熟悉,那么可以看看360magento.net中插件开发文章。

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系