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:

require_once 'app/Mage.php';

$category = Mage::getModel('catalog/category')->load(846);


And to add the products in the category:

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'];


Unfortunately, it isn't working. What should I do to make this work?


The second script needed

require_once 'app/Mage.php';
  • Do you have a flat category enabled? Commented Feb 20, 2018 at 6:43
  • No I don't, so the code should be working?
    – Dahaan
    Commented Feb 20, 2018 at 6:51
  • Clear category product code is working? Commented Feb 20, 2018 at 6:59
  • Yes, as founded here: magento.stackexchange.com/a/149165/63888
    – Dahaan
    Commented Feb 20, 2018 at 7:04
  • please check by echo $product if it is returning product id or not, I think $productId will return you product id instead of $product, please check by echo Commented Feb 20, 2018 at 7:12

1 Answer 1


If you have a flat category enabled, then please disable it first and run that script again, the code which you have used will not work with flat category enabled. So after running the script enable flat catalog again and then do indexing.

Also You can try below code to assign product to any category by modifying your code:

foreach ($productIdList as $productId) {
$product = Mage::getModel("catalog/product")->load($productId);

$categories = $product->getCategoryIds();
$categories[] = $category_id;
  • Wouldn't this remove the categories where the product is in as well? I want to keep the 'normal' categories too.
    – Dahaan
    Commented Feb 20, 2018 at 7:27
  • $categories = $product->getCategoryIds(); $categories[] = $category_id; This line of code will keep the existing product categories also. Commented Feb 20, 2018 at 7:38
  • I still get the same error as in the other comment section
    – Dahaan
    Commented Feb 20, 2018 at 7:55
  • Is it working now? Commented Feb 20, 2018 at 9:08

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.