How to Add Products To Category Programmatically in Magento 2

When there are too many products, it might be difficult for online shoppers to browse the website and find what they are looking for. That’s why it’s important to divide and arrange products in different relevant categories.

To avoid uncategorized products bothering your customer experience, you can create multiple categories and add classified products into suitable ones to make optimized navigation for your website.

Magento 2 allows store admins to add and remove products from the category under Magento 2 Admin panel > CATALOG > Categories > Products in Categories section. However, in some cases, you need to add and remove products from categories programmatically.

In this article, I will show you how to assign and remove products from category programmatically through two methods:

Method 1: Using Object Manager

Add Products To Category Programmatically Using Object Manager

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryLinkRepository = $objectManager->get('\Magento\Catalog\Api\CategoryLinkManagementInterface');
$categoryIds = array('101','102');
$sku = '24-MB01';
$categoryLinkRepository->assignProductToCategories($sku, $categoryIds);

In this code snippet, there are two parameters passed including SKU of product and the array of categories to assignProductToCategories function. Please remember that this code snippet will remove the products from previously assigned categories and assign the products to newly passed categories.

For instance, SKU ‘24-MB01’ is assigned to category ids 99 and 100. After running above code SKU ‘24-MB01’ will be removed from category ids 99, 100 and assigned to 101, 102.

Remove Products From Category Programmatically Using Object Manager

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryLinkRepository = $objectManager->get('\Magento\Catalog\Model\CategoryLinkRepository');
$categoryId = 101;
$sku = '24-MB01';
$categoryLinkRepository->deleteByIds($categoryId,$sku);

In this code snippet, there are two parameters passed including category id and SKU to deleteBylds function. Please remember that after running this code snippet, the products will be removed from only that particular category and not from all previously assigned categories.

For instance, SKU ‘24-MB01’ is assigned to categories 99, 100, and 101. The above code snippet will remove SKU ‘24-MB01’ from category 101 only.

Method 2: Using The Dependency Injection

Using the below code snippet to add products to category programmatically:

<?php

public function assignProductsToCategory(){
	$this->getCategoryLinkManagement()
	->assignProductToCategories(
		$product->getSku(),
		$product->getCategoryIds() 
	);
}
private function getCategoryLinkManagement() { 

	if (null === $this->categoryLinkManagement) { 
		$this->categoryLinkManagement = \Magento\Framework\App\ObjectManager::getInstance()
		->get('Magento\Catalog\Api\CategoryLinkManagementInterface'); 
		} 

	return $this->categoryLinkManagement; 
}

Related Post:

Conclusion

Above is the snippet codes which can help you to add or remove products from category programmatically. Hopefully, this tutorial will help you to manage your store in a more convenient way. If you have any questions or new ideas, feel free to leave a comment below.

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x