Home > Enterprise >  Add Product Image While Creating A Product And Assign Collection To The Product Through API
Add Product Image While Creating A Product And Assign Collection To The Product Through API

Time:04-12

We are using Shopify Product REST API (PHP programming) to create a new product to our client store. I need help and if possible some references on:

1. How to add product images to newly added product via REST API?

2. How to include the newly added product to a collection in the Shopify store through REST API?

Any help would be appreciated!

Thanks in advance

CodePudding user response:

There is a detailed docs with examples on this matter here: https://shopify.dev/api/admin-rest/2022-04/resources/product#post-products

In order to add images to product usually the best way is to use Base64 image or public URL for the image.

A PHP example of this from the above Docs:

use Shopify\Rest\Admin2022_04\Product;
use Shopify\Utils;
$this->test_session = Utils::loadCurrentSession(
  $requestHeaders,
  $requestCookies,
  $isOnline
);
$product = new Product($this->test_session);
$product->title = "Burton Custom Freestyle 151";
$product->body_html = "<strong>Good snowboard!</strong>";
$product->vendor = "Burton";
$product->product_type = "Snowboard";
$product->images = [
    [
        "attachment" => "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==\n"
    ]
];
$product->save();

As for linking a product to collection you must use the Collect object since that is the link between them: https://shopify.dev/api/admin-rest/2022-04/resources/collect#post-collects You can't link it directly when you are creating the product.

In code from the above Docs:

use Shopify\Rest\Admin2022_04\Collect;
use Shopify\Utils;
$this->test_session = Utils::loadCurrentSession(
    $requestHeaders,
    $requestCookies,
    $isOnline
);
$collect = new Collect($this->test_session);
$collect->product_id = 921728736;
$collect->collection_id = 841564295;
$collect->save();
  • Related