1

I've recently created some code that grabs a remote image, pulls it to the server, and sets a product to have this image with a number of roles. See below:

file_put_contents($import_filepath, file_get_contents($bgg_image));
$attribute_codes = ['image', 'small_image', 'thumbnail', 'bgg_image'];
$product->addImageToMediaGallery($import_filepath, $attribute_codes, false, false);
unlink($import_filepath);
$this->productRepository->save($product);

$import_filepath is the path to place on the server. $bgg_image is a remote image.

This is working fine, the upload is successful. The roles are actually being set too, i.e.

$attribute_codes = ['image', 'small_image', 'thumbnail', 'bgg_image'];

I can tell because if I use the below code, it will print out the correct path for my image:

 $product = $this->productRepository->getById($product_id);
 $logger->info('data is now bgg -------- ' . $product->getData('bgg_image'));
2020-01-02T04:48:43+00:00 INFO (6): data is now bgg -------- //b/g/bgg_img_24291_pic2428687_1_2.jpg

The issue is that in the below screenshot, these roles don't display on the frontend at all unless I manually select them and save the product. All other behaviour on the frontend website is as expected.

enter image description here enter image description here I've tried reloading the cache, completely clearing the cache, redploying all static files, and a whole bunch of stuff that's brought up nada.

Pls help.

3
  • do you have multiple stores on your magento website ? Commented Jan 2, 2020 at 6:29
  • I have one store under on site, and another store under another site. Commented Jan 2, 2020 at 22:16
  • okay so setting store code worked ? Commented Jan 3, 2020 at 5:03

1 Answer 1

1

Turns out they were being set for the scope of my store view, not the scope of all stores. If one wants to set scope for all stores, they should set store view to 0, set the image, and then set the store scope back again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.