Magento 2 API Manage Cost Values

Magento 2 API (Updated 2024): Manage Cost Values

The cost value is the actual cost of each product. For companies, the cost value will help us a lot to compare prices, set discounts, and vouchers, … rely on this cost by by Magento 2 API. 

However, not all companies can know how to manage cost values. This is the reason today I will show you the way to set cost values, get cost values and delete cost values more easily than ever.

Set Cost Value

You can use REST endpoints to set tier prices for the following product types:

  • Simple
  • Virtual
  • Downloadable

Step 1: Generate Admin Access Token

Endpoint: POST <host>/rest/V1/integration/admin/token

Enter your admin username and password in Body sections, then click Send.

{
  "username": "string",
  "password": "string"
}Code language: JSON / JSON with Comments (json)

Response: access token

Step 2: Set Cost Value

Create a new request and enter the admin access token

Authorization: Bearer token

Endpoint: POST <host>/rest/<store_code>/V1/products/cost

Payload:

{
  "prices": [
    {
      "cost": 10,
      "store_id": 0,
      "sku": "24-MB02"
    }
  ]
}Code language: JSON / JSON with Comments (json)

Response:

An empty array.

[]

Magento 2 API Manage Cost Values
Response

Step 3: Verify The Cost Value

In the Admin dashboard → Choose Catalog → Click Products

Search 24 – MB02 (24 – MB02 is SKU of the product I set cost value) 

And look at the cost column.

Magento 2 API Manage Cost Values
Result

Get Cost Value

Magento will return the store id and cost value for products we created requests.

Step 1: Generate Admin Access Token

Endpoint: POST <host>/rest/V1/integration/admin/token

Enter your admin username and password in Body sections, then click Send.

{
  "username": "string",
  "password": "string"
}Code language: JSON / JSON with Comments (json)

Response: access token

Step 2: Get Cost Value

Create a new request and enter the admin access token

Authorization: Bearer token

Endpoint: POST <host>/rest/<store_code>/V1/products/cost-information

Payload:

{
  "skus": [
    "24-MB02"
  ]
}Code language: JSON / JSON with Comments (json)

Response:

[
  {
    "cost": 10,
    "store_id": 0,
    "sku": "24-MB02"
  }
]Code language: JSON / JSON with Comments (json)
Magento 2 API Manage Cost Values
Response

Delete Cost Value

Step 1: Generate Admin Access Token

Endpoint: POST <host>/rest/V1/integration/admin/token

Enter your admin username and password in Body sections, then click Send.

{
  "username": "string",
  "password": "string"
}Code language: JSON / JSON with Comments (json)

Response: access token

Step 2: Delete Cost Value

Create a new request and enter the admin access token

Authorization: Bearer token

Endpoint: POST <host>/rest/<store_code>/V1/products/cost-delete

Payload

{
  "skus": [
    "24-MB02"
  ]
}Code language: JSON / JSON with Comments (json)

Response:

True

Magento 2 API Manage Cost Values
Response

Step 3: Verify The Result

In the Admin dashboard → Choose Catalog → Click Products

Search 24-MB02

And look at the cost column. 

Magento 2 API Manage Cost Values
Result

This is the way to manage cost values, hopely you will do it successfully. During the working process if you have any questions, feel free to leave a comment below. And For more information, you can refer to Magento DevDocs or Developer docs.