How To Add Magento 2 Products To Category Programmatically

I have created Magento 2 product programmatically, but how can I add or assign new products to exist Magento 2 categories

You will need to get Magento 2 product SKU and category ID first and call assignProductToCategories method from categoryLinkManagement class to assigning the products to exist Magento 2 categories

Here is the example

$sku = "test";
$cids = array(1,2,3);
$link = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
$link->assignProductToCategories($sku,$cids);
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments