I got 11 different catalog price rules set up in my Magento 1.9.3 store and I've got a script randomly activating one of the price rules every day with a cronjob.
Now I want to add the products which have a catalog price rule activated to a specific category so I will have 'Day deals' which are randomly selected every day.
I'm no expert at all in coding but by googling I came up with this:
To clear the category of products first:
<?php
require_once 'app/Mage.php';
Mage::app();
$category = Mage::getModel('catalog/category')->load(846);
$category->setPostedProducts(array());
$category->save();
?>
And to add the products in the category:
<?php
require 'app/Mage.php';
$category_id = 846;
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$tableName = $resource->getTableName('catalogrule_product');
$productIdList = $connection->fetchAll('SELECT product_id FROM '.$tableName.'');
foreach ($productIdList as $productId) {
$product = $productId['product_id'];
Mage::getSingleton('catalog/category_api')
->assignProduct($category_id,$product);
}
?>
Unfortunately, it isn't working. What should I do to make this work?
** FIX UPDATE **
The second script needed
require_once 'app/Mage.php';