cancel
Showing results for 
Search instead for 
Did you mean: 

Assigning products to categories using CategoryLinkManagement is not working

Assigning products to categories using CategoryLinkManagement is not working

Hi,

 

I am trying to assign products to categories programmatically using Magento\Catalog\Api\CategoryLinkManagementInterface but its not working. I am calling a custom service code by calling a cron job.

Magento Version is 2.0.15

 

Please help.

Thanks in advance.

 

3 REPLIES 3

Re: Assigning products to categories using CategoryLinkManagement is not working

Hello,

 

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

$this->getCategoryLinkManagement()->assignProductToCategories(                $product->getSku(),                $product->getCategoryIds()
            );

 If above code then please mark as solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Assigning products to categories using CategoryLinkManagement is not working

Thanks Sunil

assignProductsToCategories method is available from version 2.1 and I am
using 2.0.15 version. Is there any other way that can be used instead of
updating module version.

Re: Assigning products to categories using CategoryLinkManagement is not working

@Bhakti1

 

https://github.com/magento/magento2/blob/2.0.15/app/code/Magento/Catalog/Model/CategoryLinkRepositor...

 

check save function

 

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer