Last edited 7 days ago

Associate a product image to variant when creating a new product? PHP

 I can't seem to figure out how to associate the image to a variant? The example works but adds the images to the product and doesn't associate them to a specific variant. Any help?

$products_array = array(
        "title"=> "Test",
        "body_html"=> "<strong>Good snowboard!</strong>",
        "vendor"=> "Fanbuild",
        "product_type"=> "Clothing",
        "published"=> false ,
						array("name" => "Size"),
						array("name" => "Color"),
						array("name" => "Style"),
		"images" => array(
						array("src" => "http://domain.com/image.jpg"),
						array("src" => "http://domain.comimage2.jpeg"),
						"required_shipping" => true,
						"option1" => "Large",
						"option2" => "Royal",
						"option3" => "Women's Shirt",

						"required_shipping" => true,
						"option1" => "Small",
						"option2" => "Black",
						"option3" => "Shirt",

echo json_encode($products_array);
echo "<br />";
$url = 'https://xxxx@xxxx.myshopify.com/admin/products.json';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";


Alicia D. Shopify Employee
6 days ago

Hey, Jeff! 

Alicia here, from the Shopify Guru team!

Happy to provide any insight that I can for you on this! When creating a product you should be able to simply upload an image for any variant option that you have - you can see our guide on that by clicking here. Once you have your images all added in, you can also set your product up so that the variant choice itself can be made when the customer selects the corresponding image, rather than the drop down for the variants. To set that up you can just follow the steps in our Select Variants by Clicking on Images guide.

I hope this helps, Jeff! Please don't hesitate to reach back out at all if you have any further questions. 

All the best,

Alicia D. | Shopify Guru