0

I'm trying to find a way how to display the bestsellers items in Magento 1.9.

The widget is working correctly, but the issue is that it display's all time products.

What I'm looking for is to get the data from the last 7 day's and display the best sellers from that period of time.

Can someone help me with it? Or point me in the correct direction?

Thank you!

Code update:

    <?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;
    }
}
3
  • I'm voting to close this question as off-topic because Stack Overflow is a programming-related Q&A site. Your question is not about programming. Perhaps you should post it on magento.stackexchange.com instead? Jan 17, 2018 at 12:33
  • maybe you can post the code of the widget you're working on so we can better point out what is not working.
    – sissy
    Jan 17, 2018 at 14:07
  • Sorry for the late reply, I've added the code.
    – Artur Rain
    Jan 26, 2018 at 10:15

1 Answer 1

0

If this items have created_at, or bought_at date field in collection you can filter it

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

Update

You can replace

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

with

$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();

3
  • Hi, thanks for your reply, could you please take a look at the code that I'm working with and tell me how to implement your suggestion correctly? Thank you
    – Artur Rain
    Jan 26, 2018 at 10:16
  • I've tryed the code, created_at and bought_at, with the bought_at the page brakes, with created_at the result's are the same as before? Any ideas? Thanks for your help!!!
    – Artur Rain
    Jan 26, 2018 at 16:13
  • You need to debug SQL query and see what it response to you in any MYSQL client. Code die((string)$collection->getSelect()); return sql query on your page, copy and paste in any MYSQL client and see result Jan 27, 2018 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.