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.
Table of Contents
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.
[]
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.
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)
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
Step 3: Verify The Result
In the Admin dashboard → Choose Catalog → Click Products
Search 24-MB02
And look at the cost column.
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.