0

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';
10
  • Do you have a flat category enabled? Feb 20, 2018 at 6:43
  • No I don't, so the code should be working?
    – Dahaan
    Feb 20, 2018 at 6:51
  • Clear category product code is working? Feb 20, 2018 at 6:59
  • Yes, as founded here: magento.stackexchange.com/a/149165/63888
    – Dahaan
    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 Feb 20, 2018 at 7:12

1 Answer 1

0

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

Your Answer

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

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