Fatal error for WooCommerce Rest API "PUT" request (update product)

Viewed 179

I am trying to update the product via the WooCommerce REST API. But I am getting an error.

This is the HTML form:

<form action="update_product_connect.php" name="update" method="post">
<tbody>
<?php foreach ( $data as $row ) : ?>
<tr>
<td><?= $row['id']; ?></td>
<td><input type="text" name="namee" value="<?= $row['name']; ?>"/></td>
<td><input type="text" name="descriptions" value="<?= $row['description']; ?>"/></td>
<td><input type="text" name="short_descriptions" value="<?= $row['short_description']; ?>"/></td>
<td><input type="number" name="regular_pricee" value="<?= $row['regular_price']; ?>"/></td>
<td><input type="submit" name="sil" value="kaydet" /></form>
</td>
</tr>
<?php endforeach; ?>

And this is the code inside the update_product_connect.php file that sends the HTTP call to update the product.

<?php
$dataname = $_POST['namee'];
$dataprice = $_POST['regular_pricee'];
$datadescription = $_POST['descriptions'];
$datashort_description = $_POST['short_descriptions'];
$data = 
[
'name'  => $_POST['namee'],
'regular_price'  => $_POST['regular_pricee'],
'description' => $_POST['descriptions'],
'short_description' => $_POST['short_descriptions'],

];
?>
<?php echo json_encode($woocommerce->PUT('products', $data)); ?>

Below is a screenshot of the error I'm getting:

enter image description here

1 Answers

Most likely it is because you are not entering the product ID to be updated in the $woocommerce->PUT() request endpoint.

I see that in the form you have neither the product SKU nor the product ID. You will need to add them to let WooCommerce know which product needs to be updated.

It should be something like this:

<?php
$product_id = 123;
echo json_encode($woocommerce->PUT("products/{$product_id}", $data));
?>

or:

<?php
$sku = 'B-678';
$product_id = wc_get_product_id_by_sku( $sku );
echo json_encode($woocommerce->PUT("products/{$product_id}", $data));
?>

You can find more information here:

USEFUL ANSWERS

Related