【发布时间】:2020-03-05 15:51:40
【问题描述】:
我正在创建一个网络应用程序来使用他们的 API 更新 woocommerce 产品。我能够更新除图像之外的所有产品属性。任何建议将不胜感激!
这是我的编辑产品页面代码:
$product_id = (int) filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
$product = blurred_api_get_collection_item( 'products', $product_id );
?>
<?php if ( $product ) { ?>
<?php $fields = blurred_get_edit_product_fields( $product->type ); ?>
<form action="<?php echo blurred_get_current_url( array( 'action' => 'update_product' ) ); ?>" method="post">
<?php foreach( $fields as $field_id => $field ) { ?>
<div class="form-group">
<label for="<?php echo $field_id; ?>"><?php echo $field['label']; ?></label>
<?php printf(
'<input type="%1$s" class="form-control" id="%2$s" placeholder="%3$s" name="%2$s" value="%4$s" >',
$field['type'],
$field_id,
$field['placeholder'],
$product->{$field['parameter']}
); ?>
</div>
<?php } ?>
<button type="submit" class="btn btn-primary">Update</button>
</form>
<?php } else { ?>
<div class="alert alert-warning" role="alert">
<p>Sorry, no product was found for that ID.</p>
</div>
<?php } ?
我的功能页面有以下代码:
function blurred_get_edit_product_fields( $product_type = 'simple' ) {
$fields = array(
'name' => array(
'label' => 'Name',
'parameter' => 'name',
'placeholder' => '',
'type' => 'text',
),
'description' => array(
'label' => 'Description',
'parameter' => 'description',
'placeholder' => '',
'type' => 'text',
),
'short-description' => array(
'label' => 'Short Description',
'parameter' => 'short_description',
'placeholder' => '',
'type' => 'text',
),
'stock-quantity' => array(
'label' => 'Stock Quantity',
'parameter' => 'stock_quantity',
'placeholder' => '',
'type' => 'number',
),
'images' => array(
'label' => 'Images',
'parameter' => 'images',
'placeholder' => '',
'type' => 'text',
),
);
执行上述代码时,我收到图像错误:
Notice: Array to string conversion in /Users/username/Sites/api/public_html/edit-product.php on line 26
这是第 26 行:
$product->{$field['parameter']}
图像字段显示数组。
【问题讨论】:
标签: wordpress api woocommerce product