2

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

0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you very much for your priority answer and support. I can update as you specify without any problems.but,I do not know how to create a form and update a product I selected. I can do all operations such as adding a product, deleting a product, on the form. but i can't update the data on the form i created
Maybe you know. I am using the automattic rest api files and using these files I can create a form, add a product, add a product, add an order, delete an order. Finally, the product update remained
Did you create the HTML code of the form? The update of the product data can be done in several ways. Where will the form be?
$row['id']; is the product ID? If yes, you will need to convert <td><?= $row['id']; ?></td> with <td><input type="text" name="id" value="' . $row["id"] . '" readonly/></td>. This way you can get the product ID from the update_product_connect.php file like so: $_POST['id'];. Make sure that the name attribute values of each form field are unique on the page.
If it's an HTML page (and not PHP) try using this: <td><input type="text" name="id" value="<?php echo $row["id"]; ?>" readonly/></td>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.