"/> "/> "/> 这是里面的代码update_produ-6ren">
gpt4 book ai didi

php - WooCommerce Rest API "PUT"请求(更新产品)的 fatal error

转载 作者:行者123 更新时间:2023-12-04 07:38:43 25 4
gpt4 key购买 nike

我正在尝试通过 WooCommerce REST API 更新产品。但我收到一个错误。
这是 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; ?>
这是 里面的代码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)); ?>
以下是我收到的错误的屏幕截图:
enter image description here

最佳答案

很可能是因为 您没有输入产品 ID 将在 $woocommerce->PUT() 中更新请求端点。
我看到在表单中您既没有产品 SKU 也没有产品 ID。您需要添加它们以让 WooCommerce 知道需要更新哪个产品。
它应该是这样的:

<?php
$product_id = 123;
echo json_encode($woocommerce->PUT("products/{$product_id}", $data));
?>
或者:
<?php
$sku = 'B-678';
$product_id = wc_get_product_id_by_sku( $sku );
echo json_encode($woocommerce->PUT("products/{$product_id}", $data));
?>
您可以在这里找到更多信息:
  • WooCommerce Rest API Docs - Update a product

  • 有用的答案
  • REST API: No route was found matching the URL and request method
  • Error "No route was found matching the URL and request method" when call api
  • 关于php - WooCommerce Rest API "PUT"请求(更新产品)的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67608143/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com