Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.
Solved

Is it possible to somehow rename product option via REST API?

SergiiGolddev
Shopify Partner
4 1 0

I'm trying to rename a product option using the REST API:

 
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, testurl.'products/4383793250431.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

$product = array(
    "product"=>
        array(
            //"id"=>4383793250431,
            "options"=>
            array(
                "id"=>5688792809599,
                "name"=> "Material1",
            )
        ));

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($product));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Shopify-Access-Token: '.TEST_ADMIN_API_TOKEN;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch)


But I'm getting this error:

"{"errors":{"base":["could not delete option because it has more than 1 value"]}}

Despite the fact that the interface does not have any problems with this:

SergiiGolddev_0-1657783586128.png

I loosely renamed the "Material" option to "Materials 1".

 

Accepted Solution (1)

SergiiGolddev
Shopify Partner
4 1 0

This is an accepted solution.

It turns out that the error was referring to the 2nd product option, which is not mentioned in the request. As you can see in the screenshot, the product has the "Size" option, it must also be passed in the request, otherwise the API tries to remove this option. The correct request looks like this:

$product = array(
    "product"=>
        array(
            "options"=>
            array(
                array(
                    "id"=>5688792809599,
                    "name"=> "Material3",
                    ),
                    array(
                        "id"=>5688792842367,
                        "name"=> "Size",
                )
            )      
        ));

View solution in original post

Reply 1 (1)

SergiiGolddev
Shopify Partner
4 1 0

This is an accepted solution.

It turns out that the error was referring to the 2nd product option, which is not mentioned in the request. As you can see in the screenshot, the product has the "Size" option, it must also be passed in the request, otherwise the API tries to remove this option. The correct request looks like this:

$product = array(
    "product"=>
        array(
            "options"=>
            array(
                array(
                    "id"=>5688792809599,
                    "name"=> "Material3",
                    ),
                    array(
                        "id"=>5688792842367,
                        "name"=> "Size",
                )
            )      
        ));