Hello,
I am working on Magento open source (Community Version). API to integrate Magento with third party application.
I would like to know can i add multiple products to a particular category using API.
Hi @tatyanabog2b46 ,
protected $categoryLinkManagement;
private function getCategoryLinkManagement()
{
if (null === $this->categoryLinkManagement) {
$this->categoryLinkManagement = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
}
return $this->categoryLinkManagement;
}
You can use below code
$categoryIds = [1];
$this->getCategoryLinkManagement()->assignProductToCategories(
$product->getSku(),
$categoryIds
);
Problem Solved? Accept as Solution!
Hope it helps!
Thanks
Yes, you can add multiple products to a particular category using the Magento API.
Here are the general steps to follow:
Get the category ID of the category where you want to add the products. You can do this by using the catalogCategoryRepositoryV1 API.
Get the product IDs of the products that you want to add to the category. You can do this by using the catalogProductRepositoryV1 API.
Use the catalogCategoryLinkRepositoryV1 API to add the products to the category. You will need to pass the category ID and the product IDs in the request body.
Here is an example request body for adding multiple products to a category:
{ "categoryLink": { "category_id": 3, "product_ids": [1, 2, 3, 4] } }
This example request would add the products with the IDs 1, 2, 3, and 4 to the category with the ID 3.
Note that the exact API endpoints and request/response formats may differ depending on the version of the Magento API that you are using. You can refer to the Magento API documentation for more information on the specific API calls that you need to make.