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

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.

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