Search code examples
magentomagento-1.8cron-task

How to create new category that lists new products added in the last 7 days


I need to display products which added in last 7 days in a new category called New In. When I import products I will add all the products to category New In and it should automatically delete products that old more than 7 days automatically. I have searched for this but did not find a solution, can someone please help me on this?


Solution

  • create a cron job with following code replace your "YOUR NEW IN CATEGORY ID"

    set_time_limit(0);
    define('MAGENTO', realpath(dirname(__FILE__)));
    
    require_once MAGENTO . '/app/Mage.php';
    
    Mage::app();
    
    $to = Mage::getModel('core/date')->date('Y-m-d H:i:s');
    $from = date('Y-m-d H:i:s',strtotime("-7 days", strtotime($to)));
    
    
    //Load product model collecttion filtered by sale attribute
    $proCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addfieldtofilter('created_at', array('lt' => $from));
    
    
    foreach ($proCollection as $product) {
        $ids = $product->getCategoryIds();
        if (($key = array_search(YOUR NEW IN CATEGORY ID, $ids)) !== false) {
            unset($ids[$key]);
            $product->setCategoryIds($ids);
            $product->save();
        }
    }