Last active
August 29, 2015 14:15
-
-
Save molotovbliss/605a0de924197895bc74 to your computer and use it in GitHub Desktop.
addMediaGalleryAttributeToCollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Source: http://www.magentocommerce.com/boards/viewthread/17414/#t141830 | |
| function addMediaGalleryAttributeToCollection(Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $_productCollection) { | |
| $_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId(); | |
| $_read = Mage::getSingleton('core/resource')->getConnection('catalog_read'); | |
| $_mediaGalleryData = $_read->fetchAll(' | |
| SELECT | |
| main.entity_id, `main`.`value_id`, `main`.`value` AS `file`, | |
| `value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`, | |
| `default_value`.`position` AS `position_default`, | |
| `default_value`.`disabled` AS `disabled_default` | |
| FROM `catalog_product_entity_media_gallery` AS `main` | |
| LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value` | |
| ON main.value_id=value.value_id AND value.store_id=' . Mage::app()->getStore()->getId() . ' | |
| LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value` | |
| ON main.value_id=default_value.value_id AND default_value.store_id=0 | |
| WHERE ( | |
| main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ') | |
| AND (main.entity_id IN (' . $_read->quote($_productCollection->getAllIds()) . ')) | |
| ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC | |
| '); | |
| $_mediaGalleryByProductId = array(); | |
| foreach ($_mediaGalleryData as $_galleryImage) { | |
| $k = $_galleryImage['entity_id']; | |
| unset($_galleryImage['entity_id']); | |
| if (!isset($_mediaGalleryByProductId[$k])) { | |
| $_mediaGalleryByProductId[$k] = array(); | |
| } | |
| $_mediaGalleryByProductId[$k][] = $_galleryImage; | |
| } | |
| unset($_mediaGalleryData); | |
| foreach ($_productCollection as &$_product) { | |
| $_productId = $_product->getData('entity_id'); | |
| if (isset($_mediaGalleryByProductId[$_productId])) { | |
| $_product->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId])); | |
| } | |
| } | |
| unset($_mediaGalleryByProductId); | |
| return $_productCollection; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment