如何随收藏一起加载产品媒体库? [英] How to load products media gallery along with the collection?

查看:61
本文介绍了如何随收藏一起加载产品媒体库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给我一些有关如何将产品的媒体库与馆藏一起加载的提示. 我正在收集这样的收藏夹:

Can anybody give me a hint about how to load product's media gallery along with the collection. I'm getting the collection like this:

$collection = Mage::getModel('catalog/product')->getCollection()
                        ->addStoreFilter($storeId)
                        ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
    var_dump($product->getMediaGalleryImages());
}

但是getMediaGalleryImages()返回null.我知道我可以分别用$product = Mage::getModel('catalog/product')->load($product->getId())加载每个产品,但是我想避免这种情况,因为它会导致不必要的工作量.

But getMediaGalleryImages() returns null. I know that I can load each product separately with $product = Mage::getModel('catalog/product')->load($product->getId()) but I want to avoid this, because it causing unnecessary workload.

谢谢!

推荐答案

以下是将媒体库添加到集合中的功能:

Here is a function to add the media gallery to a collection:

// Source: http://www.magentocommerce.com/boards/viewthread/17414/#t141830

public 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;
}

其用法示例如下:

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
$this->addMediaGalleryToArray($products);

这篇关于如何随收藏一起加载产品媒体库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆