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

Product image roles randomly disappear #10687

Closed
boxyman opened this issue Aug 28, 2017 · 35 comments · Fixed by #17554
Closed

Product image roles randomly disappear #10687

boxyman opened this issue Aug 28, 2017 · 35 comments · Fixed by #17554
Assignees
Labels
Event: dmcdindia Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line good first issue Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@boxyman
Copy link

boxyman commented Aug 28, 2017

After upgrading from Magento 2.1.7 to 2.1.8 we had problems with product image roles. We have experienced that some products that previously had product image role set to base, small, thumbnail didn't have it anymore. It seems that the image roles get's removed from random products over time. This weekend at we have at least identified 10 products that got images roles removed during the weekend.

Preconditions

  1. Magento 2.1.8
  2. PHP 7
  3. MySQL 5.7
  4. Ubuntu 16.04
  5. Nginx
  6. Redis
  7. Varnish
  8. SSL (HTTPS)

Steps to reproduce

  1. Go to "Products" > "Catalog"
  2. Choose a product
  3. Go to "Images And Videos"
  4. Click an image
  5. Set "Role" to base, small, thumbnail

Expected result

  1. Product image roles should be set to base, small, thumbnail unless the user changes it.

Actual result

  1. Product image roles disappear randomly from some products from time to time.

Short way to reproduce:

Preconditions

Create product with image and set role image: base, small, thumbnail

Steps to reproduce

Save Product in backend controller like this:

$store = $this->storeManager->getStore(0);
$this->storeManager->setCurrentStore($store);
$product = $this->productRepository->getById($data['product_id'], true, $store->getId());
$this->productRepository->save($product);

Expected result

Product image roles should be set to base, small, thumbnail unless the user changes it.

Actual result

Product image does not have any roles

@kervin
Copy link

kervin commented Sep 11, 2017

This is a big issue for us. Any news when this will be fixed?

@magento-engcom-team magento-engcom-team added bug report Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed 2.1.x Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed labels Sep 11, 2017
@RomaKis
Copy link
Contributor

RomaKis commented Sep 12, 2017

@boxyman, thank you for your report.
We were not able to reproduce this issue by following the steps you provided.
Please provide more details regarding your environment, or try to reproduce this
issue on a clean installation.

@kervin
Copy link

kervin commented Sep 12, 2017

I think it's only when you save a product programmatically.

The problem that I found it when you set the current store to admin..

    // Set to Admin Store
    // When this is set, product images disappear
    // $store = $this->storeManager->getStore(0);
    // $this->storeManager->setCurrentStore($store->getCode());

@magento-engcom-team
Copy link
Contributor

magento-engcom-team commented Sep 12, 2017

@kervin Unfortunately, we could not reproduce the issue as you described it. Please add more details to your description of the steps you followed when identifying this issue. Screenshots or logs would be helpful, too.

@kervin
Copy link

kervin commented Sep 13, 2017

Just do the following on 2.1.8. If I do not set current store, it saves products to the default store view.. when I set the store, it breaks the images.


// This line is breaking images

$this->storeManager->setCurrentStore($store);

$product = $this->productRepository->getById($data['product_id'], true, $store->getId());

$this->productRepository->save($product);

@kervin
Copy link

kervin commented Sep 13, 2017

I found a workaround, unset media_gallery, if you are not changing images.

$product->unsetData('media_gallery');
$this->productRepository->save($product);

@magento-engcom-team magento-engcom-team added Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch and removed Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed bug report labels Sep 14, 2017
@magento-engcom-team magento-engcom-team self-assigned this Sep 18, 2017
@magento-engcom-team magento-engcom-team removed their assignment Oct 5, 2017
@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed and removed Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Progress: needs update labels Oct 5, 2017
@tanghia
Copy link

tanghia commented Oct 16, 2017

I got the same issue with Magento 2.2.0. Some my customers have updated their websites from 2.1.8 to 2.2.0 and all custom extensions used $this->productRepository->save($product); will unset value of image properties "base", "small" and "thumbnails" . We see placeholder images only!

$product->unsetData('media_gallery');
$this->productRepository->save($product);

Two lines of code are great to fix this issue, however, it can make a critical error on the catalog_product_view page.

1 exception(s):
Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in /var/www/html/m220sampledata/vendor/magento/module-catalog/Block/Product/View/Gallery.php on line 114

This issue is the same with #10461 and it must be fixed quickly!

@jokeputs
Copy link
Contributor

I have the same issue with Magento 2.1.9. In my installation it only happens when the product is saved in a cron task. If I run the same code using the cli tool the images keep their roles.

@magento-engcom-team
Copy link
Contributor

@boxyman, thank you for your report.
We've created internal ticket(s) MAGETWO-83133 to track progress on the issue.

@magento-engcom-team magento-engcom-team added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Nov 1, 2017
Scarraban added a commit to Scarraban/magento2 that referenced this issue May 31, 2018
To fix magento#10687 - adds the 'types' key to the $images array to provide existing image roles (if not set), which are re-attributed after clearMediaAttribute() is called
@kanasagraj
Copy link

kanasagraj commented Jun 25, 2018

I think its issue on Magento 2.2.3 or below version

Open root/app/code/Magento/Catalog/Model/ProductRepository.php

protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
{
find : if (isset($entry['value_id']) // near line no : 499
TO
if (isset($entry['value_id']) && $entry['value_id'] != '') {
}

I have make this patched and its working for me.( See : #10461 (comment) )

@astragt
Copy link

astragt commented Jul 4, 2018

Quick heads up for anyone who finds this issue because their image roles randomly disappears but you are not doing anything with the productRepository.

Its enough that a Class that you are using i saving though the product repository to lose your image roles.

I saved tier prices through Magento\Catalog\Model\Product\ScopedTierPriceManagement and this is using the product repository so i lost my image roles.

Tested in 2.2.3

@josefbehr
Copy link
Contributor

We have a customer's shop, where image roles disappeared. Now we're trying to get them back, but none of the possible solutions here work, apparently. Magento version is 2.1.12. Anyone have any idea how to get the roles back?

@magento-engcom-team
Copy link
Contributor

Hi @boxyman. Thank you for your report.
The issue has been fixed in #15606 by @Scarraban in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.7 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.2.x The issue has been fixed in 2.2 release line label Aug 8, 2018
magento-engcom-team added a commit that referenced this issue Aug 8, 2018
 - Merge Pull Request #15606 from Scarraban/magento2:10687
 - Merged commits:
   1. a99e97e
@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Aug 16, 2018
@magento-engcom-team
Copy link
Contributor

Hi @boxyman. Thank you for your report.
The issue has been fixed in #17554 by @eduard13 in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

magento-engcom-team added a commit that referenced this issue Aug 16, 2018
…ing #17554

 - Merge Pull Request #17554 from eduard13/magento2:2.3-develop-fp-15606
 - Merged commits:
   1. 1c74f55
@ishakhsuvarov
Copy link
Contributor

Hi @boxyman. Thank you for your report.
The issue has been fixed in #17553 by @eduard13 in 2.1-develop branch
Related commit(s):

The fix will be available with the upcoming 2.1.16 release.

@ishakhsuvarov ishakhsuvarov added the Fixed in 2.1.x The issue has been fixed in 2.1 release line label Sep 12, 2018
magento-engcom-team added a commit that referenced this issue Sep 12, 2018
…17553

 - Merge Pull Request #17553 from eduard13/magento2:2.1-develop-bp-15606
 - Merged commits:
   1. abb3c4a
   2. 2be962d
   3. 6a8fadf
@stijnkuppens
Copy link

The code fix is not present in 2.2.6, this looks still to be an issue. Any updates ?

@eduard13
Copy link
Contributor

@skdude because the fix was provided in 2.2.7 (Release: 2.2.7).
Upgrade to2.2.7 or use a patch to apply the needed fix.

@merance2000
Copy link

I'm having this issue in 2.3.3 - how do we fix?

@markalston
Copy link

Also having this issue in 2.3.3.
I think the import function is wiping all the roles. Images are there but all roles are cleared on import Add/Update

@merance2000
Copy link

Yes exactly what is happening for us too.

@SamJUK
Copy link

SamJUK commented Feb 28, 2020

Had the same issue today with a client, easiest fix we found was to just reset the missing roles to the first image in the gallery.

<?php

ini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);

use Magento\Framework\App\State;
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(State::class);
$state->setAreaCode('frontend');


$pri = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$smi = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$smdl = $smi->getStores();
$stores = [0 => 'global'];
foreach ($smdl as $k => $v) {
	$stores[$k] = $v['code'];
}


foreach ($stores as $k => $v) {
	$smi->setCurrentStore($k);
	echo str_repeat('-', 20), PHP_EOL;
	echo "Processing {$k} ({$v})", PHP_EOL;
	echo str_repeat('-', 20), PHP_EOL;

	$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)->create()->addAttributeToSelect('*');
	foreach ($collection as $product) {
		$objectManager->get("Magento\Catalog\Model\Product\Gallery\ReadHandler")->execute($product);
		$existingMediaGalleryEntries = $product->getMediaGalleryEntries();

		if (!is_iterable($existingMediaGalleryEntries) || count($existingMediaGalleryEntries) === 0) {
			continue;
		}

		$required_fields = ['image', 'small_image', 'thumbnail', 'swatch_image'];
		foreach ($existingMediaGalleryEntries as $key => $entry) {
			$required_fields = array_diff($required_fields, $entry['types']);

			if (count($required_fields) === 0) {
				continue 2;
			}
		}

		$slime = implode(' | ', $required_fields);
		echo "{$product->getSku()} :: $slime", PHP_EOL;

		$types = array_merge($required_fields, $existingMediaGalleryEntries[0]['types']);
		$existingMediaGalleryEntries[0]->setTypes($types);

		$product->setMediaGalleryEntries($existingMediaGalleryEntries);
		$pri->save($product);
	}
}

@tthierryEra
Copy link

Seems like it is still happening in 2.4.6 for me when I use

  • $this->productRepository->getById
  • $product->setStoreId(0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Event: dmcdindia Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line good first issue Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.