Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top