Search code examples
shopwareshopware6

Import and sort media for an article in shopware 6


at some products the images are not in the right order. instead of going through all of the media from the admin, the adminstrator want to sort them via import.

i created a profile like mentioned here:

https://docs.shopware.com/en/shopware-en/settings/importexport#import-of-media

but as a result it gives me an error when importing the previously exported file (reduced to only one testarticle and without any further changes):

A file with the name "xxx-yyy-zzz.jpg" already exists.

and then i have the media double on the product, but they are non-existing (redcrossed in admin and storefront).

is there a proper way to export the existing media, resort them and import the file again?

if that does not work, is there another way to complete that task properly?


Solution

  • Fastest way would be via the admin API.

    First fetch the media mappings for a given product, e.g. via a product number filter.

    // POST /api/search/product-media
    // Accept: application/json
    {
        "includes": {
            "product_media": ["id", "media", "position", "productId"],
            "media": ["id", "fileName", "fileExtension"]
        },
        "associations": {
            "media": []
        },
        "filter": [
            {
                "type": "equals",
                "field": "product.productNumber",
                "value": "EXAMPLE123"
            }
        ]
    }
    

    Example response:

    {
        "total": 2,
        "data": [
            {
                "productId": "01dbf9f74b964383b1a5ef931ea39b1e",
                "position": 1,
                "media": {
                    "fileExtension": "jpg",
                    "fileName": "foo",
                    "id": "5164cb0e69ad487a92c1fc1f1d4a7505",
                    "apiAlias": "media"
                },
                "id": "3fd590d1e27d425aba6ce4cabd96c4d5",
                "apiAlias": "product_media"
            },
            {
                "productId": "01dbf9f74b964383b1a5ef931ea39b1e",
                "position": 2,
                "media": {
                    "fileExtension": "jpg",
                    "fileName": "bar",
                    "id": "eec8195fb1cf4ae69f1d7ded85c39111",
                    "apiAlias": "media"
                },
                "id": "4cb8c195612b40d68da0130d48037b99",
                "apiAlias": "product_media"
            }
        ],
        "aggregations": []
    }
    

    You can then take that entire array from the data property, change the position properties according to your needs, and use it to update the product with the productId. Just remove the nested media properties in the payload.

    // PATCH /api/product/01dbf9f74b964383b1a5ef931ea39b1e
    {
      "media": [
            {
                "productId": "01dbf9f74b964383b1a5ef931ea39b1e",
                "position": 2,
                "id": "3fd590d1e27d425aba6ce4cabd96c4d5"
            },
            {
                "productId": "01dbf9f74b964383b1a5ef931ea39b1e",
                "position": 1,
                "id": "4cb8c195612b40d68da0130d48037b99"
            }
        ]
    }