1

I have pulled the product set I need in with my collection, how do I add these products to another category whilst also being in the ones they are?

Here is my code so far:

    //Getting products that are need to go on sale page and have a special price
$collection = Mage::getModel('catalog/product')
            ->getCollection();
$collection ->addFieldToFilter('sale_exempt',1);
$collection ->addAttributeToFilter(
    array(
        array('attribute' => 'special_price', 'is'=>new Zend_Db_Expr('not null'))
    ));

var_dump($collection->getData());

$new_categoryId = 9;

foreach ($collection as $_product) {



}
1

Use this code:

    //Getting products that are need to go on sale page and have a special price
$collection = Mage::getModel('catalog/product')
            ->getCollection();
$collection ->addFieldToFilter('sale_exempt',1);
$collection ->addAttributeToFilter(
    array(
        array('attribute' => 'special_price', 'is'=>new Zend_Db_Expr('not null'))
    ));

$new_categoryId = array(9);

foreach ($collection as $_product) {

    $product = Mage::getModel('catalog/product')->load($_product->getId());
    $product->setCategoryIds($new_categoryId);
    $product->save();
}
|improve this answer|||||
  • 1
    Thanks, it's giving me this: Fatal error: Uncaught TypeError: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be of the type array, null given – Tom Mar 15 '17 at 14:02
  • @Tom: In a similar situation I had to write Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID) to fix this problem. – Zsolti Nov 13 '17 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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