3

I need to get all the products created between 2 dates of a specific category. Can you please provide me a coding to get this. Im totally new for magento. I tried this but didnt worked. Please help. Im using magento 1.9.

1
  • 2
    You'll find you get much better responses by posting code you have tried, rather than linking to something you have then likely changed anyway). You need to also say about when you say it didn't work, did it not return what you want or was it an error
    – tim.baker
    Commented Jul 30, 2017 at 12:07

3 Answers 3

3

For category specific products you can use Mage::getModel('catalog/category')->load($categoryId)->getProductCollection() and add a filter condition like:

$now        = Mage::getModel('core/date')->timestamp(time());
$fromDate   = date('Y-m-d' . ' 00:00:00', strtotime('-14 days', $now));
$toData     = date('Y-m-d' . ' 00:00:00', strtotime('-7 days', $now));
$categoryId = 2; # your ID here

$collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
$collection->addAttributeToFilter('created_at', array(
    'from'  => $fromDate,
    'to'    => $toData
));

It will generate a query like this:

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`
FROM `prefix_catalog_product_entity` AS `e`
INNER JOIN `prefix_catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=3 AND cat_index.category_id = '2' AND cat_index.is_parent=1
WHERE (`e`.`created_at` >= '2017-07-17 00:00:00' AND `e`.`created_at` <= '2017-07-24 00:00:00')
2

This should do it:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('created_at', array('gteq' => strtotime('-2 days', now())));
4
  • 1
    I think the last filter is not needed. The updated_at field is always going to be smaller than now().
    – Marius
    Commented Jul 31, 2017 at 9:17
  • Thanks @Marius you're absolutely right, I'll edit my answer
    – Niels
    Commented Jul 31, 2017 at 9:19
  • 1
    Disagree. If the question was "created during last two days" it would be correct, but for two given dates, you need both filters.
    – sv3n
    Commented Jul 31, 2017 at 9:57
  • Yes you're right, your answer is better when using 2 dates, also kudos for adding the resulting query.
    – Niels
    Commented Jul 31, 2017 at 10:00
0

Thank you everyone who took time to help me out. I was able to get the required data by using following. Here for the purpose of testing I have hardcoded the dates.

$collection = mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => $categoryArray))
    ->addAttributeToFilter(
        'status',
        array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    )
    ->addAttributeToFilter('created_at', array('gteq' => '2017-07-20 00:00:00'))
    ->addAttributeToFilter('created_at', array('lteq' => '2017-07-30 23:59:59'))
    ->setOrder('category_id', 'ASC');
$collection->getSelect()->group('e.entity_id');

Your Answer

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

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