{"id":870,"date":"2020-03-09T01:13:59","date_gmt":"2020-03-09T01:13:59","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/09\/programmatically-assign-products-to-multiple-categories-in-magento-2\/"},"modified":"2025-05-22T14:49:06","modified_gmt":"2025-05-22T09:19:06","slug":"programmatically-assign-products-to-multiple-categories-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-assign-products-to-multiple-categories-in-magento-2\/","title":{"rendered":"How To Programmatically Assign Products To Multiple Categories In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 store is designed in such a way that shoppers find their required product easily. If that\u2019s not the case, they may fumble into your store among the thousands of products that you offer and ultimately abandon the store due to the confusion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We definitely do not want that to happen and hence divide the products among various categories. Category help users easily browse among the products and decide the best suited among them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you offer products that belong to multiple categories, maintaining it manually for each product can be tiresome. For example, you offer a t-shirt for men, you assign that product to men\u2019s category as well as to the t-shirts category.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a large number of products, if you have to implement such functionality, I have posted this solution to&nbsp;<em><strong>p<\/strong><\/em><em><strong>rogrammatically assign products to multiple categories in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s dive right in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to programmatically assign products to multiple categories in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nuse Magento\\Framework\\AppInterface;\n\ntry {\n    require_once __DIR__ . '\/app\/bootstrap.php';\n\n} catch (\\Exception $e) {\n    echo 'Autoload error: ' . $e->getMessage();\n    exit(1);\n}\n\ntry {\n    $bootstrap = \\Magento\\Framework\\App\\Bootstrap::create(BP, $_SERVER);\n    $objectManager = $bootstrap->getObjectManager();\n    $objectManager->get('Magento\\Framework\\App\\State')->setAreaCode('adminhtml');\n\n    $newCategoryIds = array('148','152'); \/\/ It should be category Ids\n    $sku = 'tshirt'; \/\/ It should be Product SKU\n\n    $categoryLinkRepository = $objectManager->get('\\Magento\\Catalog\\Api\\CategoryLinkManagementInterface');\n    $categoryLinkRepository->assignProductToCategories($sku, $newCategoryIds);\n\n} catch (\\Exception $e) {\n    echo \"&lt;pre>\";\n    print_r($e->getMessage());\n    echo \"&lt;\/pre>\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is a Magento 2 Root Script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. You can<a href=\"https:\/\/meetanshi.com\/blog\/programmatically-set-product-position-in-a-category-in-magento-2\/\">set product position in a category in Magento 2<\/a>&nbsp;as the product position value determines the order of products on the category page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/programmatically-set-url-key-of-category-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">programmatically set URL key of the category in Magento 2<\/a>, in case you are importing products to your store in bulk and want to create SEO-friendly category pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 store is designed in such a way that shoppers find their required product easily. If that\u2019s not the case, they may fumble into&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"predecessor-version":[{"id":15042,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions\/15042"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}