Assign the product to that category

$newCategory = array( $list[0] , $list[$key]); // create an array using root and subcategory id foreach($newproducts as $prod) { $prod->setCategoryIds( array_merge($prod->getCategoryIds(), $newCategory) ); $prod->save(); }

$newCategory = array( $list[0] , $list[$key]); // create an array using root and subcategory id
foreach($newproducts as $prod)
{
    $prod->setCategoryIds(
            array_merge($prod->getCategoryIds(), $newCategory)
         );
    $prod->save();
}

Share this Tutorial