Programmatically Assign/Remove Products from Category in Magento 2

Magento 2 has the inbuilt feature on the admin side to add or remove the products from the category in the following admin path, Magento 2 Admin Panel > CATALOG > Categories. Here, you can add/remove products to the category under the Products in Category section. But sometimes like importing the bulk of products/category in Magento 2, we need to follow either Magento’s default import feature on the admin side or can create a custom script. In this blog, we will see how to assign/remove the products programmatically from the category by using the custom script.

Assign Products to the Category:

Here is the example code to add the products to category, to assign the products to the category, we have to inject the \Magento\Catalog\Api\CategoryLinkManagementInterface class in our code,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$categoryLink = $objectManager->get('\Magento\Catalog\Api\CategoryLinkManagementInterface');

$productSKU = '240-LV09';
$categoryIds = array('11','37');

$categoryLink->assignProductToCategories($productSKU, $categoryIds);

In the above example, I have used the function assignProductToCategories() to assigned the product which has the SKU “240-LV09” to the categories 11 & 37. But the above code will be removed the products from previously assigned categories and assign the products to newly passed categories.

To keep the product on the previously assigned categories, we can use the following method,

 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$productSKU = '240-LV09';
// get product information by sku 
$product = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface')->get($productSKU);

$previousCategoryIds = $product->getCategoryIds(); 

$previousCategoryIds[] = 12; 
$previousCategoryIds[] = 37; 
$categoryLink = $objectManager->get('\Magento\Catalog\Api\CategoryLinkManagementInterface');
$categoryLink->assignProductToCategories($productSKU, $previousCategoryIds);

In the above example, I have used the \Magento\Catalog\Api\ProductRepositoryInterface class to get the product’s information (i.e, previously assigned categories id), and merged the new categories id with previously assigned categories id, then finally, passed the merged categories id with product SKU by using the function assignProductToCategories() in the class \Magento\Catalog\Api\CategoryLinkManagementInterface.

Remove Products from the Category:

Here is the example code to remove the product from the category, in the following example I have inject the \Magento\Catalog\Api\CategoryLinkRepositoryInterface class to remove the product from the category,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$categoryLink = $objectManager->get('\Magento\Catalog\Api\CategoryLinkRepositoryInterface');

$categoryIds = 37;
$productSKU = '240-LV09';

$categoryLink->deleteByIds($categoryIds, $productSKU);

In the above example, I have passed the category id and product SKU in the function deleteByIds() to remove the products from the category.

Note: In this blog, I have used object manager method for the demonstrated purpose. But Magento recommends using the dependency injection method and avoid using the object manager.

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *