I was wondering if there is any way to add a date filter to a product collection. What i am trying to do: I have a collection for new products, i need to add a filter to it so it filter the products by date. i dont need all days just three past days. The idea is just like layered navigation just the option must be three past days and they mus be generated dynamically. Note: The date mus be selected in frontend by the user. THanks in advance

I'm not sure about your actual use case, but to filter a product collection so that it only returns products created within the last 3 days do as follows

$collection = Mage::getResourceModel('catalog/product_collection')
    ->addFieldToFilter('created_at', array('gt' => date("Y-m-d H:i:s", strtotime('-3 day'))));
  • No i want to select the date in frontend. – Ylli Dec 12 '14 at 15:09
  • Well that's entirely dependent on your frontend implementation. Perhaps an ajax request to a controller, gets given the date, converts it using strtotime (as above) to show past X days, then loads and returns the product data you need. – Luke Rodgers Dec 12 '14 at 16:22
  • Well thank you for the answer but i need a little bit help implementing it. Its beyond my magento knowledge. I can make the controller but i dont know how to create the frontend part and send data with ajax request. – Ylli Dec 13 '14 at 8:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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