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.
Hello @preetkamigaeac
Update your code to ensure category_links is in the proper format before making the API request.
$categories = $item['extension_attributes']['category_links'];
$catArray = [];
foreach ($categories as $category) {
$category_id = $category['category_id'];
foreach ($localCategories as $lcat) {
if ($category_id == $lcat['baseId']) {
$catArray[] = ['category_id' => (string) $lcat['id']];
}
}
}
$product['extension_attributes']['category_links'] = $catArray;
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9
Great post! Assigning categories to a product in a REST API helps organize items and makes searching easier. This is useful for online stores and apps to sort products better!