Assign product to category

function assignProductsToCategory($manufacturers ,$list ) { $product = Mage::getModel(‘catalog/product’); foreach($manufacturers as $key => $manufacturer) { if($key == 0) continue; $attrLabel = ‘manufacturer’; $attr = $product->getResource()->getAttribute($attrLabel); $manufacturer_id = $attr->getSource()->getOptionId($manufacturer); echo ‘Manufatcurer Name – ‘.$manufacturer.’ ‘.$manufacturer_id. PHP_EOL; $newproducts = $product->getCollection()->addAttributeToFilter(array( array(‘attribute’=>’manufacturer’, ‘eq’=> $manufacturer_id))); $newCategory = array( $list[0] , $list[$key]); foreach($newproducts as $prod) { $product->load($prod->getId()); $name = $manufacturer.’ ‘.$product->getName();… Read more »

function assignProductsToCategory($manufacturers ,$list ) {

    $product = Mage::getModel('catalog/product');

    foreach($manufacturers as $key => $manufacturer) {
        if($key == 0) continue;
        $attrLabel = 'manufacturer';
        $attr = $product->getResource()->getAttribute($attrLabel);
        $manufacturer_id = $attr->getSource()->getOptionId($manufacturer);
        echo 'Manufatcurer Name - '.$manufacturer.' '.$manufacturer_id. PHP_EOL;
      
        $newproducts = $product->getCollection()->addAttributeToFilter(array(
            array('attribute'=>'manufacturer', 'eq'=> $manufacturer_id)));
        
        $newCategory = array( $list[0] , $list[$key]);
       
        foreach($newproducts as $prod)
        {
            $product->load($prod->getId());      
            $name = $manufacturer.' '.$product->getName();
            $prod->setName($name);
            echo $name. PHP_EOL;
            $prod->setCategoryIds(
                    array_merge($prod->getCategoryIds(), $newCategory)
                 );
            $prod->save();
        }
     
    }
}

Share this Tutorial