Skip to content

Instantly share code, notes, and snippets.

@jameshiggins
Created October 26, 2016 18:38
Show Gist options
  • Save jameshiggins/85f2773c93a2c85691d70c85eab36ea3 to your computer and use it in GitHub Desktop.
Save jameshiggins/85f2773c93a2c85691d70c85eab36ea3 to your computer and use it in GitHub Desktop.
I’ve seen recently this issue - editing and saving products in admin deleted existing images from frontend.
On evaluation it appeared that the images, while showing on the frontend, did not appear in the admin product edit Images tab at all.
Inspection of the database tables revealed that while EAV records contained image names (used on frontend), the image gallery (used in admin) was missing the relevant entries, so it appears that CSV import does not populate gallery table.
This problem will happen with both new and updated products, when new image names are added with CSV.
I’ve made an SQL statement that will fill the missing records, and which you will need to run each time after importing products with images using CSV:
INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, `value`)
SELECT ga.attribute_id, v.entity_id, v.value
FROM catalog_product_entity_varchar v
INNER JOIN eav_entity_type et ON et.entity_type_code='catalog_product'
INNER JOIN eav_attribute va ON va.entity_type_id=et.entity_type_id AND va.frontend_input='media_image' AND va.attribute_id=v.attribute_id
INNER JOIN eav_attribute ga ON va.entity_type_id=et.entity_type_id AND ga.attribute_code='media_gallery'
LEFT JOIN catalog_product_entity_media_gallery g ON g.entity_id=v.entity_id AND g.value=v.value
WHERE v.value<>'no_selection' AND v.value<>'' AND g.value IS NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment