【问题标题】:Updating Product Image WooCommerce API更新产品图片 WooCommerce API
【发布时间】: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


    【解决方案1】:

    通过使用 if/else 语句解决了这个问题:

    $product_id = (int) filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
    $product = blurred_api_get_collection_item( 'products', $product_id );
    $image = $product->images[0]->src;
    ?>
    
    <?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 
                    if ($field['label'] == 'Image Link') {
                        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->images[0]->{$field['parameter']}
                        ); 
                    }else {
                        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 } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2016-09-10
      • 2016-03-14
      相关资源
      最近更新 更多