3

I need to know for each product sky, price and stock. I am using http://192.168.1.180/magento/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=1&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

In the web documentation I see that I can get it

http://devdocs.magento.com/swagger/index_20.html#!/catalogProductRepositoryV1

"stockItem": {
          "itemId": 0,
          "productId": 0,
          "stockId": 0,
          "qty": 0,
          "isInStock": true,
          "isQtyDecimal": true,
          "showDefaultNotificationMessage": true,
          "useConfigMinQty": true,
          "minQty": 0,
          "useConfigMinSaleQty": 0,
          "minSaleQty": 0,
          "useConfigMaxSaleQty": true,
          "maxSaleQty": 0,
          "useConfigBackorders": true,
          "backorders": 0,
          "useConfigNotifyStockQty": true,
          "notifyStockQty": 0,
          "useConfigQtyIncrements": true,
          "qtyIncrements": 0,
          "useConfigEnableQtyInc": true,
          "enableQtyIncrements": true,
          "useConfigManageStock": true,
          "manageStock": true,
          "lowStockDate": "string",
          "isDecimalDivided": true,
          "stockStatusChangedAuto": 0,
          "extensionAttributes": {}
        },

But when I use it does not appear that information, why?

1

I did it this way, i hope this helps you:

$api = new Api();
$adminUrl='https://example.com/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "{your_username}", "password" => "{your_pw}");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
);
$qty = $_GET["qty"];
$sku = $_GET["sku"];

//you need to replace this with your own code for token
$token = $api->GetToken($ch,$data_string);

$headers = array("Authorization: Bearer $token");
$requestUrl='https://example.com/index.php/rest/V1/stockItems/' . $sku;

//See method "GetProductAsObject" below
$product = $api->GetProductAsObject($sku,$ch,$requestUrl,$headers);

$product->qty=$qty;
$product->is_in_stock=true;

$toencode["stockItem"] = $product;

//This part will print your desired result
echo json_encode($toencode);

//#### Info: this method is in a separate class ###
//This method is in the Class Api.php known as $api above
public function GetProductAsObject($sku,$ch,$requestUrl,$headers)
{
     $ch = curl_init();
     $ch = curl_init($requestUrl);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

     $result = curl_exec($ch);
     return json_decode($result);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.