0

am able to update product title successfully but when i try making a request to shopify api to update my product image alt i keeps getting status code 400 Bad Request ... here is my code below ... I would be thankful for any help

    public function alt(){

    function __construct() {
            parent::__construct();
            $data = array(); //Always define $data in the constructor to make it available throughout your code
        }

    //getting shop info to access the app
    $store_name = $this->session->userdata('store_name');
    $this->load->database();
    $this->load->model('Store');
    $storeinfo_data=$this->Store->load_store_info($store_name);
    $store_name_db = $storeinfo_data->store_name;
    $store_name_tk = $storeinfo_data->access_token;
    $store_name_url = $storeinfo_data->myshopify_domain;
    $this->data['storeinfo']=$storeinfo_data;               




    if($store_name == $store_name_db){

        //authenticating app with shopify api to be alble to access shop info
    $sc = new ShopifyClient($store_name_url, $store_name_tk, SHOPIFY_KEY, SHOPIFY_SECRET);

    //making a GET request to shopify api for shop products 
    $shop_products = $sc->call('GET', '/admin/products.json', null);

    foreach($shop_products as $product) {
        $product_id = $product['id'];
        $product_d = $sc->call('GET', '/admin/products/' . $product_id . '/images.json', null);
        echo "<pre>";
        print_r($product_d);
        $position =  "1";
        $key = "alt";
        $value = "new alt tag content";

        $metafields =  array(
            "key"=>"alt",
            "value"=>"new alt tag content",
            "value_type"=>"string",
            "namespace"=>"tags"
        );

        $image = array(
            "id"=> "16827152131",
            "position" => 1, 
            "metafields"=>$metafields
        );

        //making a request to update image alt of a product in shop 
        try{
            $shop_product = $sc->call('PUT','/admin/products/8445495491/images/16827152131.json', $image);
        }catch( Exception $e ){
            echo '<pre>'; 
                print_r($e);

        }
        exit();





    }



    }else{

    }


}

and here is the output i get

ShopifyApiException Object
(
[method:protected] => PUT
[path:protected] => /admin/products/8445495491/images/16827152131.json
[params:protected] => Array
    (
        [id] => 16827152131
        [position] => 1
        [metafields] => Array
            (
                [key] => alt
                [value] => new alt tag content
                [value_type] => string
                [namespace] => tags
            )

    )

[response_headers:protected] => Array
    (
        [http_status_code] => 400
        [http_status_message] => Bad Request
        [server] => nginx
        [date] => Mon, 26 Sep 2016 09:11:59 GMT
        [content-type] => application/json; charset=utf-8
        [transfer-encoding] => chunked
        [connection] => keep-alive
        [x-frame-options] => DENY
        [x-shopid] => 14690858
        [x-shardid] => 2
        [x-shopify-shop-api-call-limit] => 1/40
        [http_x_shopify_shop_api_call_limit] => 1/40
        [x-stats-userid] => 0
        [x-stats-apiclientid] => 1423954
        [x-stats-apipermissionid] => 31527902
        [x-xss-protection] => 1; mode=block; report=/xss-report/9729121a-c8c6-4ff2-aee8-cb9963cd864f?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_images&source%5Bsection%5D=admin
        [x-request-id] => 9729121a-c8c6-4ff2-aee8-cb9963cd864f
        [x-dc] => ash
        [x-download-options] => noopen
        [x-permitted-cross-domain-policies] => none
        [x-content-type-options] => nosniff
    )

[response:protected] => Array
    (
        [errors] => Array
            (
                [image] => Required parameter missing or invalid
            )

    )

[message:protected] => Bad Request
[string:Exception:private] => 
[code:protected] => 400
[file:protected] => /Applications/MAMP/htdocs/newp/application/libraries/shopify/shopify.class.php
[line:protected] => 70
[trace:Exception:private] => Array
    (
        [0] => Array
            (
                [file] => /Applications/MAMP/htdocs/newp/application/controllers/platforms/Shopify.php
                [line] => 347
                [function] => call
                [class] => ShopifyClient
                [type] => ->
                [args] => Array
                    (
                        [0] => PUT
                        [1] => /admin/products/8445495491/images/16827152131.json
                        [2] => Array
                            (
                                [id] => 16827152131
                                [position] => 1
                                [metafields] => Array
                                    (
                                        [key] => alt
                                        [value] => new alt tag content
                                        [value_type] => string
                                        [namespace] => tags
                                    )

                            )

                    )

            )

        [1] => Array
            (
                [file] => /Applications/MAMP/htdocs/newp/system/core/CodeIgniter.php
                [line] => 514
                [function] => alt
                [class] => Shopify
                [type] => ->
                [args] => Array
                    (
                    )

            )

        [2] => Array
            (
                [file] => /Applications/MAMP/htdocs/newp/index.php
                [line] => 315
                [args] => Array
                    (
                        [0] => /Applications/MAMP/htdocs/newp/system/core/CodeIgniter.php
                    )

                [function] => require_once
            )

    )

[previous:Exception:private] => 

)

1 Answer 1

2

I noticed a few things here. The image data has to be contained in an {"image": ... } object and the metafields should come as an array. Also, if you are modifying an existing metafield then include the metafield ID in the payload.

Try replacing this:

    $image = array(
        "id"=> "16827152131",
        "position" => 1, 
        "metafields"=>$metafields
    );

with something like this:

    $image = array("image" => array(
        "id"=> 16827152131,
        "position" => 1, 
        "metafields"=> $metafields
    ));

And replace this:

    $metafields =  array(
        "key"=>"alt",
        "value"=>"new alt tag content",
        "value_type"=>"string",
        "namespace"=>"tags"
    );

with something like this:

    $metafields =  array(array(
        "id"=> 123,
        "key"=>"alt",
        "value"=>"new alt tag content",
        "value_type"=>"string",
        "namespace"=>"tags"
    ));

123 would be the ID of the metafield that you are looking to modify. You can obtain that ID using an API call like this: GET /admin/metafields.json?metafield[owner_id]=16827152131&metafield[owner_resource]=product_image

You can find examples of the correct format here: https://help.shopify.com/api/reference/product_image#update

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.