获得最近7天最畅销商品? Magento 1.9

时间:2018-01-17 11:21:36

标签: magento widget report magento-1.9

我试图找到一种如何在Magento 1.9中显示畅销书的方法。

小部件工作正常,但问题是它显示的是所有时间产品。

我正在寻找的是获取过去7天的数据并显示该段时间内的畅销书。

有人可以帮助我吗?或者指出我正确的方向?

谢谢!

代码更新:

    <?php class Bigdrop_ProductSlider_Block_Bestsellers extends Bigdrop_ProductSlider_Block_Slider_Abstract {
    protected $_code = 'bestsellers';

    /**
     * Retrieves sort by param
     *
     * @return string
     */
    protected function _getSortByParam()
    {
        return 'ordered_qty';
    }

    /**
     * Preparing product collection of the current Slider
     *
     * @return Mage_Catalog_Model_Resource_Product_Collection|Mage_Reports_Model_Resource_Product_Collection
     */
    protected function _prepareSliderCollection()
    {
        $collection = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty();

        if (Mage::helper('catalog/product_flat')->isEnabled()) {
            $collection->getSelect()
                ->joinInner(array(
                    'e2' => 'catalog_product_flat_' . Mage::app()->getStore()->getId()
                ), 'e2.entity_id = e.entity_id');
        } else {
            $this->assignAttributes($collection);
        }

        $this->_applyStockVisibilityFilter($collection);
        $this->_setOrderToCollection($collection);
        $collection->getSelect()->limit($this->_getLimit());

        return $collection;
    }
}

1 个答案:

答案 0 :(得分:0)

如果此项目在集合中有created_atbought_at日期字段,您可以对其进行过滤

->addFieldToFilter( DATE_FIELD_NAME, array( 'from'=> date('Y-m-d H:i:s', strtotime('-7 days')), 'to' => date('Y-m-d H:i:s') ) )

<强>更新

您可以替换

$collection = Mage::getResourceModel('reports/product_collection') ->addOrderedQty();

$collection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('created_at') ->addFieldToFilter( 'created_at', array( 'from'=> date('Y-m-d H:i:s', strtotime('-7 days')), 'to' => date('Y-m-d H:i:s') ) )->addOrderedQty();