I have done success fully data migration M1.9.6 to M2.3.3 after in admin side product inside category not assign default cateogry so any idea how to assign it automatically because our product data large and not manually selected.

  • let me know how many categories in your site ? Jan 11, 2020 at 14:02
  • Have you tried to set programmatically using custom script magento.stackexchange.com/questions/140875/… I recommend you to try with one single product & test the scenarios at backend and frontend if it works as expected, then try with all product collection & assign the product to a respected category programmatically. Jan 11, 2020 at 14:05
  • @NagarajuK Thnaks for reply but there 50k plus product in our website and how to assign only default cateogry in admin side product edit after manually not possible so is it any other way
    – Rv Singh
    Jan 13, 2020 at 4:02
  • Programatically we have an option @rv singh do you have local instance? Jan 13, 2020 at 5:37
  • Yes we have local instance
    – Rv Singh
    Jan 13, 2020 at 5:40

1 Answer 1


Below is the code to assign the products to root Categorie programmatically

ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
ini_set('max_execution_time', 800000);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');

$rootCategoryId = array('2');//Root categorie Id here
//For single product
$sku = '24-MB01';
$categoryLinkRepository = $objectManager->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
$categoryLinkRepository->assignProductToCategories($sku, $rootCategoryId);
echo "successfully assigned to root categorie=".$sku;

//For all the products
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()->addAttributeToSelect('*')->load();
foreach ($collection as $product){
    $categoryLinkRepository->assignProductToCategories($product->getSku(), $rootCategoryId);
    echo "successfully assigned to root categorie".$product->getSku();



Note: 1. Please make sure the test with a single product first then if it works as expected then you can try with the product collection on local instance.

  1. I strongly recommend you do an experiment on your local instance first

  2. Verify the frontend whether the product has been displaying or not.

  3. Copy-paste & change the root Categorie Id as per your website.

  4. If you want you can add try/catch in the above script

  5. Execute this script from the command line from the root directory of magento2

  6. Once the script execution has done, do reindex & flush cache

  • Thanks for reply but if migrated data already in m2 inside so i have e to used for all data product inside cateogry by default cateogry selected
    – Rv Singh
    Jan 13, 2020 at 5:56
  • Did you know how to custom api module "magento.stackexchange.com/questions/300780/…"
    – Rv Singh
    Jan 13, 2020 at 5:57
  • @RvSingh one small query is the above script is if works for you? Jan 13, 2020 at 5:59
  • but actually after migration in most of the product is not assign to root category so for that what i have to do?I know this is work ony one product but for multiple what can i do?
    – Rv Singh
    Jan 13, 2020 at 7:12

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.