Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert Media with Product #88

Open
mrtnmueller opened this issue Apr 16, 2024 · 1 comment
Open

Upsert Media with Product #88

mrtnmueller opened this issue Apr 16, 2024 · 1 comment

Comments

@mrtnmueller
Copy link

Is it possible to upsert a media file with a product upsert? Either as a separate payload in the same request or directly via the product upsert?

We have about 10k articles with images I want to sync. Optimal strategy would be to only update the image if the article is created (normally they don't change) and of course do everything in one go (like 100 products at a time).

What I have until now:

        $syncService = new SyncService($this->context);
        $payload = new SyncPayload();
        $payload->set(ProductDefinition::ENTITY_NAME . '-upsert', new SyncOperator(ProductDefinition::ENTITY_NAME, SyncOperator::UPSERT_OPERATOR, [
            [
                'id' => '018cf59aa9d870008000fbc39f0fcef2',
                'name' => 'New Product',
                'taxId' => '018de63f1b61715aad18389bfaddf6ba',
                'price' => [
                    [
                        'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
                        'linked' => true,
                        'gross' => 999.99,
                        'net' => (999.99 / 1.19),
                    ],
                ],
                'productNumber' => 'FRE832672',
                'stock' => 2,
                ],
        ]));

        echo("============================================");
        echo("Sync response");
        $response = $syncService->sync($payload);
        var_dump($response);

adding a property like

    'cover' => [
        'url' => 'https://image.shutterstock.com/image-photo/stock-photo-woman-with-static-electric-hair-isolated-on-black-background-250nw-2014152446.jpg',
        ],

doesn't work...

@SpiGAndromeda
Copy link

Please have a look into the media usage in the product entity.

The ProductEntity has a one-to-many relation to the ProductMediaEntity. The ProductMediaEntity has a one-to-one relation to the MediaEntity.

You can create "images" for a product in one upsert:

[
  'id' => '018cf59aa9d870008000fbc39f0fcef2',
  'name' => 'New Product',
  'taxId' => '018de63f1b61715aad18389bfaddf6ba',
  'price' => [...],
  'productNumber' => 'FRE832672',
  'stock' => 2,
  'media' => 
    [
      'id' => '6bcc6ff17506417ebd0d11509ee98471',
      'position' => 1,
      'media' => 
        [
          'id' => '6e72218deae348cdb828b1c4e397efdd'
          'url' => 'https://image.shutterstock.com/image-photo/stock-photo-woman-with-static-electric-hair-isolated-on-black-background-250nw-2014152446.jpg'
        ]
    ],
  'coverId' => '6bcc6ff17506417ebd0d11509ee98471'
]

Something like that should work. The media ID (not product media ID) can be left out and will be generated automatically as far as I know (beware of update operations on collections: there is an explanation in the Admin API documentation). However, you should generate the product media ID by yourself because you want to use it as a reference.

It might be possible that something like this is also working:

[
  'id' => '018cf59aa9d870008000fbc39f0fcef2',
  'name' => 'New Product',
  'taxId' => '018de63f1b61715aad18389bfaddf6ba',
  'price' => [...],
  'productNumber' => 'FRE832672',
  'stock' => 2,
  'cover' =>
    [
      'id' => '6bcc6ff17506417ebd0d11509ee98471',
      'position' => 1,
      'media' => 
        [
          'id' => '6e72218deae348cdb828b1c4e397efdd'
          'url' => 'https://image.shutterstock.com/image-photo/stock-photo-woman-with-static-electric-hair-isolated-on-black-background-250nw-2014152446.jpg'
        ]
    ]
]

Don't forget that you have to do the file upload separately! This is done via Action API. Otherwise only the MediaEntity will exist but it will be a husk without an actual file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants