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
1 Answer
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'))));
-
-
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. Dec 12, 2014 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.– YlliDec 13, 2014 at 8:51