Add Product images through Admin API

I’m adding products through the Admin API but I can’t find anything on how to add product images. Can anybody explain to me how I can add product images through the api?

Folgende Reihenfolge:

  1. Post um eine MediaID zu erhalten

  2. Post um Bild hochzuladen

  3. Bei Product Post MediaID und ggf. CoverID in den payload aufnehmen

 public function uploadProductImage($imageUrl, $imageName)
    {
        try {
            $this->log->info(„Connector/Shopware->uploadProductImage: " . $imageName);
            $mediaId = null;
            if ($this->cacheMediaFolder[$this->productMediaFolder][„id“]) {
                $data = [„mediaFolderId“ => $this->cacheMediaFolder[$this->productMediaFolder][„id“]];
                $url = $this->baseUrl . $this->apiEndpoints[‚media‘];
                $response = $this->postData($url, $data);
                $mediaId = $response->id;
                if ($mediaId) {
                    $data = [
                        „url“ => $imageUrl,
                    ];
                    $image = explode(“.", $imageName);
                    $url = $this->baseUrl . ‚/api/v1/_action/media/‘ . $mediaId . ‚/upload?extension=‘ . $image[1] . ‚&fileName=‘ . $image[0];
                    $response = $this->postData($url, $data);
                    if (!$response) {
                        $this->log->error(
                            new \Exception(„Upload Error try to delete MediaID: " . $mediaId . " for: " . $imageUrl)
                        );
                        $url = $this->baseUrl . $this->apiEndpoints[‚media‘];
                        $this->deleteData($url, $mediaId);
                    }
                }
            }
        } catch (\Exception $exception) {
            $this->log->error($exception);
        }
    }
    public function postData($url, $data, $returnResponse = true)
    {
        try {
            if ($returnResponse) {
                if (strpos($url, „?“) === false) {
                    $url = $url . „?_response=true“;
                } else {
                    $url = $url . „&_response=true“;
                }
            }
            $token = $this->getToken();
            $client = $this->getClient();
            $response = $client->request(
                ‚POST‘,
                $url,
                [
                    ‚headers‘ => [
                        ‚Content-Type‘ => ‚application/json‘,
                        ‚charset‘ => ‚utf-8‘,
                        ‚Accept‘ => ‚application/json‘,
                        ‚Authorization‘ => 'Bearer ’ . $token,
                    ],
                    ‚body‘ => json_encode($data),
                ]
            );
            if ($returnResponse) {
                $data = json_decode($response->getBody()->getContents());
                if (!$data) {
                    $location = $response->getHeader(„Location“)[0];
                    $id = explode(“/", $location);
                    $id = $id[sizeof($id) - 1];

                    return (object)[‚id‘ => $id];
                }

                return $data->data;
            } else {
                $location = $response->getHeader(„Location“)[0];
                $id = explode("/", $location);
                $id = $id[sizeof($id) - 1];

                return $id;
            }
        } catch (\Exception $exception) {
            $this->log->error($exception);

            return null;
        }
    }
 

Product Payload:

$data = [
                „active“ => true,
                „isCloseout“ => true,
                „taxId“ => $tax[„id“],
                „productNumber“ => $product[‚identifier‘],
                „name“ => $values[‚name‘][0][‚data‘],
                „description“ => $values[‚beschreibung‘][0][‚data‘],
            ];

$data[‚media‘] = ;
                foreach ($productImages as $index => $productImage) {
                    $dataImage = [
                        „mediaId“ => $productImage[‚id‘],
                        „position“ => $index,
                    ];
                    if ($index == 0) {
                        $data[‚coverId‘] = md5($product[„identifier“] . $productImage[‚id‘], false);
                        $dataImage[„id“] = $data[‚coverId‘];
                    }
                    $data[‚media‘] = $dataImage;
                }
 

2 „Gefällt mir“

Where is this from, a package? I’m not working from Shopware or any other existing software but I’m building my own middleware.

My code 

php with guzzle http 

rest is on you

Thanks for the code but I’m trying to understand how the api works. Can I upload an image or do I have to give it a url? Is there any documentation on this?

Apart from the uploading, if I understand it correctly:

  1. I make a post to /media (createMedia) to create a new Media Resource
  2. Somehow get the image to Shopware
  3. Update the Product and give it the media ID

Look at the code everything is there

Step 2:

$url = $this->baseUrl . ‘/api/v1/_action/media/’ . $mediaId . ‘/upload?extension=’ . $image[1] . ‘&fileName=’ . $image[0];
                    $response = $this->postData($url, $data);

 

@oliverriske‍ Could you write me your variables, which you are using in your code?

 $this-\>cacheMediaFolder $this-\>productMediaFolder $this-\>apiEndpoints $this-\>baseUrl $this-\>log

sry for the resurrection, but i’m stuck on a problem related to this thread…

i’m currently creating a json for an http request towards the admin api to create a product, i already created a media file and uploaded data to it, i can see the image when i open the product, but i really have no clue how to set one of the added media ids as cover… this is my json

UPDATE: i got it to work, i had to define an id for the media product relationship and use this id to set one of the associations as cover

{
    "productNumber" : "SCG1",
    "stock" : 0, 
    "price" : [
        {
            "currencyId" : "b7d2554b0ce847cd82f3ac9bd1c0dfca", 
            "gross": 15, 
            "net": 10, 
            "linked" : false,
            "listPrice": {
                "currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
                "net": 37.81512605042,
                "gross": 45.0,
                "linked": true,
                "listPrice": null,
                "extensions": []
            }
        }
    ],
    "media": [{
        "id": "b6d2554b0ce847cd82f3bd8bd1c0dfca", // random UUID selfgenerated will be used as "coverId"
        "media": { 
            "id": "7cdd383f3fcb4418b5be2a5d11ba9afc",
            "position": 0
        }
    },  {
        "media": { 
            "id": "4aaf1eff97e848d08aae9590cd8b7af1",
            "position": 1
        }
    }],
    "coverId": "b6d2554b0ce847cd82f3bd8bd1c0dfca",
    "manufacturerId": "fc9b37e6bdfe4efb8cedf62725dfddec",
    "deliveryTimeId": "910be68cd9f44e378c1ba8965a4a8896",
    "taxId": "05ee7e68ead64bec8157c24014cfbbfc",
    "restockTime": 14,
    "isCloseout": true,
    "purchaseSteps": 16,
    "maxPurchase": 160,
    "minPurchase": 16,
    "weight": 0.26,
    "width": 78.0,
    "height": 178.0,
    "length": 78.0,
    "unitId": "e2845da8c4534b8fadc28267c0b7fb10",
    "referenceUnit": 1,
    "purchaseUnit": 1,
    "properties": [
        {"id": "3b3401cc9c6a4362baa92a373617e712"},
        {"id": "a2b0cd0eff3748d890453e493e743755"}
    ]
}