cancel
Showing results for 
Search instead for 
Did you mean: 

Assign categories to product REST API

Assign categories to product REST API

I am trying to import products from 1 store to another Magento 2.4 store. In the below code have issue to assign category to the product.

 

	$categories = $item['extension_attributes']['category_links'];
			$catArray = array();

		 	foreach($categories as $category){
		 		$category_id = $category['category_id'];

		 		foreach($localCategories as $lcat){
		 			if($category_id == $lcat['baseId']){
		 				array_push($catArray, $lcat['id']);
		 			}
		 		}
		 	}
 	$product['categories'] = $catArray;

The above code leaves the category blank. If I remove foreach($categories as $category), product is assigned to all the categories.