cancel
Showing results for 
Search instead for 
Did you mean: 

API - multiple products adding

API - multiple products adding

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.

 

 

 

 

2 REPLIES 2

Re: API - multiple products adding

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

Ankit Jasani

Re: API - multiple products adding

Yes, you can add multiple products to a particular category using the Magento API.

Here are the general steps to follow:

  1. Get the category ID of the category where you want to add the products. You can do this by using the catalogCategoryRepositoryV1 API.

  2. Get the product IDs of the products that you want to add to the category. You can do this by using the catalogProductRepositoryV1 API.

  3. 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.