Magento 1.9 – Add New Category for Products with Special Price

categorymagento-1.9special-price

I'm trying to create a cron job that checks if a product has a special price and if it does it adds it to the sale category as well as the one it is currently in.

$category_id = 9;
$_product = Mage::getModel('catalog/product')->load($id);
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents();
$productposted = array();
foreach ($collection as $product){
    if(($product->getFinalPrice() < $product->getPrice()) && ($_product->getData('sale_expempt')==1)) {
        $productposted[$product->getId()]  = 9;
    }
}
$category = Mage::getModel('catalog/category')->load($category_id);
$category->setPostedProducts($productposted);
$category->save();

Best Answer

Try below solution

$collection = Mage::getModel('catalog/product')->getCollection()   
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents() ;
 $productposted = array();
 foreach ($collection as $product){
   if($product->getFinalPrice() < $product->getPrice()){
      $productposted[$product->getId()]  = 1; //or your sort order
   }
}
$category = Mage::getModel('catalog/category')->load($category_id);//your category id
$category->setPostedProducts($productposted);
$category->save();
Related Topic