从Magento的产品系列中获取产品媒体库图像

Jos*_*ton 7 php magento magento-1.x

我在Magento有一系列产品,我希望能够从中获取媒体库图像.但是我发现我必须迭代我的集合并再次加载产品以使getMediaGalleryImages()函数正常工作.

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('status', 1);

foreach($products as $product) {
    $_product = Mage::getModel('catalog/product')->load($product->getId());

    $product->getMediaGalleryImages();      // This returns nothing
    $_product->getMediaGalleryImages();     // Returns the Collection of Images
}
Run Code Online (Sandbox Code Playgroud)

显然,我每次都可以继续重新加载产品,但这会增加运行此代码所需时间的开销.

有没有办法将媒体库图像添加到集合中?

Mat*_*ine 16

您可以使用

$product->load('media_gallery');
Run Code Online (Sandbox Code Playgroud)

在getMediaGalleryImages之前(关于您在集合中加载的产品).

  • 奇怪的是,这很有效.魔术发生在Mage_Eav_Model_Entity_Abstract-> load($ object,$ entityId,$ attributes)中.如果$ attributes为空,它将调用loadAllAttribute($ object).所以$ product-> load('blah')将加载所有缺少的属性,包括'media_gallery' (6认同)
  • 那么这会加载单个产品吗?如果是这样,那么将它用于大量产品是禁止的,并且无法使用集合的好处. (2认同)

use*_*352 7

一种简单的方法供将来参考:

在foreach之外添加

$mediaBackend = Mage::getModel('catalog/product_attribute_backend_media');
$mediaGalleryAttribute = Mage::getModel('eav/config')->getAttribute(Mage::getModel('catalog/product')->getResource()->getTypeId(), 'media_gallery');
$mediaBackend->setAttribute($mediaGalleryAttribute);
Run Code Online (Sandbox Code Playgroud)

然后做foreach:

foreach ($productCollection as $product) {
    $mediaBackend->afterLoad($product);
}
Run Code Online (Sandbox Code Playgroud)

然后您将在产品上加载图库.


Won*_*and -5

这是您正在寻找的代码,抱歉耽搁了:)

它来自这个讨论: http ://www.magentocommerce.com/boards/viewthread/17414/

我只是对 id a 的数量和分页添加了一些额外的检查

function addMediaGalleryAttributeToCollection(Mage_Catalog_Model_Resource_Product_Collection $_productCollection)
{
    if (Mage::getStoreConfig('color_selector_plus/colorselectorplusgeneral/showonlist', Mage::app()->getStore())) {

        $_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId();
        $_read = Mage::getSingleton('core/resource')->getConnection('catalog_read');

        $pageCur = $_productCollection->getCurPage();
        $pageSize = $_productCollection->getPageSize();
        $offset = $pageSize * ($pageCur - 1);

        $ids = $_productCollection->getAllIds($pageSize, $offset);

        // added check on products number: if 0 ids the following query breaks
        if (count($ids) > 0) {

            $sql = '
    SELECT
        main.entity_id, `main`.`value_id`, `main`.`value` AS `file`, `value`.`disabled`,
        /*`value`.`label`, `value`.`position`, */
       /*`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 */
';
            $_mediaGalleryData = $_read->fetchAll($sql);


            $_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;
}
Run Code Online (Sandbox Code Playgroud)