2

I want to get & set special price for a product in Magento 2.0 using APi. Following is the http url for getting the price of an item.

http://192.168.222.45:8081/MagentoDev/index.php/rest/V1/products/special-price-information

Body:

{ "skus": [ "SKU505050" ] }

The result of the request is :

{ "message": "Request does not match any route." }

Similarly, I can not set the special price of the product. For Setting the price, I use the following link:

http://192.168.222.45:8081/MagentoDev/index.php/rest/V1/products/special-price

Body:

[ { "price": 160, "store_id": 0, "sku": "SKU505050", "price_from": "10/01/2017", "price_to": "10/01/2017", "extension_attributes": {} } ]

result:

{ "message": "Request does not match any route." }

Has anyone faced this issue? A quick help will be highly appreciated !

MGento
  • 1,519
  • 9
  • 22

1 Answers1

3

you can achieve everything via rest/V1/products endpoint

// get token
$userData = array("username" => "MyUsername", "password" => "MyPassword");
$ch = curl_init("https://example.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);
$token = str_replace('"', '', $token);

// get product special price
$ch = curl_init("https://example.com/index.php/rest/V1/products/24-MB04");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));

$result = curl_exec($ch);
$data = json_decode($result,true);
$specialPrice = $data['price'];

foreach ($data['custom_attributes'] as $attr) {
    if ($attr['attribute_code'] == 'special_price') {
        $specialPrice = $attr['value'];
    }
}
echo $specialPrice;


// update special price
$productUpdate = [
    'product' => [
        'sku' => '24-MB04',
        'custom_attributes' => [
            'special_price' => 20.99,
        ]
    ]
];
$productsJson = json_encode($productUpdate);

$ch = curl_init('https://example.com/index.php/rest/V1/products');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $productsJson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
Pavel Novitsky
  • 533
  • 2
  • 9