0
votes

I am creating a web app to update woocommerce products using their API. I am able to update all product properties except images. Any suggestions would be greatly appreciated!

here is my edit product page code:

$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 } ?

My functions page has the following code:

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',
    ),
);

When executing the above code, I get this error for the images:

Notice: Array to string conversion in /Users/username/Sites/api/public_html/edit-product.php on line 26

This is line 26:

$product->{$field['parameter']}

and the image field says array.

1

1 Answers

1
votes

Figured it out by using an if/else statement:

$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 } ?>