I am using the following code in order to add images to an existing product in Magento.
Mage::app()->setCurrentStore($target_store);
$product = Mage::getModel("catalog/product")->loadByAttribute($sku);
$product->addImageToMediaGallery(
$local_img_path,
array('image', 'thumbnail', 'small_image'),
false,
false
);
$product->save();
Once I execute this for all the products I need to update/add, I reindex all and clear cache. The images do not show up in the admin area for the product. However the image does show on the frontend product detail page, but not on the small images on the category list page.
Checking the database, there are the following 3 relevant entries for the product in catalog_product_entity_varchar:
value_id entity_type_id attribute_id store_id entity_id value 888509 4 79 4 35743 /t/e/tempimg_791.jpg 888332 4 80 4 35743 /t/e/tempimg_791.jpg 888333 4 81 4 35743 /t/e/tempimg_791.jpg
There are the base, small, and thumb images for the frontend store. Now if I run the following SQL query to find all rows in catalog_product_entity_varchar having store_id 4 and attribute_id 79 that do not have a corresponding entry with store_id 0, and creates a corresponding row:
INSERT INTO catalog_product_entity_varchar
(entity_type_id, attribute_id, store_id, entity_id, value)
SELECT DISTINCT 4, 79, 0, entity_id, value
FROM catalog_product_entity_varchar AS cpev
WHERE attribute_id = 79
AND store_id != 0
AND entity_id
IN (
SELECT entity_id
FROM catalog_product_entity_varchar
WHERE store_id = 4
AND attribute_id = 79
AND entity_id NOT
IN (
SELECT entity_id
FROM catalog_product_entity_varchar
WHERE store_id = 0
AND attribute_id = 79
)
) ON DUPLICATE KEY UPDATE value = cpev.value;
And then do the same again twice, replacing the 79’s with 80 and then again with 81, I get the following relevant rows in catalog_product_entity_varchar:
value_id entity_type_id attribute_id store_id entity_id value 888509 4 79 4 35743 /t/e/tempimg_791.jpg 888332 4 80 4 35743 /t/e/tempimg_791.jpg 888333 4 81 4 35743 /t/e/tempimg_791.jpg 888605 4 79 0 35743 /t/e/tempimg_791.jpg 888606 4 80 0 35743 /t/e/tempimg_791.jpg 888607 4 81 0 35743 /t/e/tempimg_791.jpg
the images now show up in the front end on the category listings, but STILL not in the admin area for the product.
Now, I execute the following query, to add an entry into catalog_product_entity_media_gallery representing the image already set in the previous queries:
INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, value)
SELECT 82, entity_id, value FROM catalog_product_entity_varchar AS cpev
WHERE attribute_id = 79 AND store_id = 0 AND
entity_id IN (
SELECT entity_id
FROM catalog_product_entity_varchar
WHERE store_id = 4
AND attribute_id = 79
AND entity_id NOT IN (
SELECT entity_id
FROM catalog_product_entity_media_gallery
WHERE attribute_id = 82
)
) ON DUPLICATE KEY UPDATE value = cpev.value;
Once this is done, and a reindex/recache is completed. the Images show up in admin.
There must be a proper way that does not result in me needing to execute 4 SQL queries manually to get the images working properly after an import! What am I doing wrong?
Thanks people.
Embarrassing and infuriating – I fixed it by deleting app/code/core/Mage and replacing it with a freshly downloaded copy of that folder. Looks like some of the core files had been edited, and broken in the process.
The moral of the story – Don’t Edit the Core.