简体   繁体   English

WooCommerce Rest API“PUT”请求的致命错误(更新产品)

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

I am trying to update the product via the WooCommerce REST API.我正在尝试通过 WooCommerce REST API 更新产品。 But I am getting an error.但我收到一个错误。

This is the HTML form:这是 HTML 形式:

<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.这是update_product_connect.php文件中的代码,该文件发送 HTTP 调用以更新产品。

<?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:以下是我收到的错误的屏幕截图:

在此处输入图像描述

Most likely it is because you are not entering the product ID to be updated in the $woocommerce->PUT() request endpoint.很可能是因为您没有$woocommerce->PUT()请求端点中输入要更新的产品 ID。

I see that in the form you have neither the product SKU nor the product ID.我在表格中看到您既没有产品 SKU 也没有产品 ID。 You will need to add them to let WooCommerce know which product needs to be updated.您需要添加它们以让 WooCommerce 知道需要更新哪个产品。

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有用的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM