magento 如何加载产品媒体集沿着收藏?

yi0zb3m4  于 2022-11-12  发布在  其他
关注(0)|答案(7)|浏览(189)

有谁能给予我一个提示,关于如何加载产品的媒体集沿着收集?
我得到的集合是这样的:

$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())单独加载每个产品,但我想避免这样做,因为这会导致不必要的工作量。

4dbbbstv

4dbbbstv1#

如果有人在寻找另一种方法,我发现这是工作(在只有一种情况下,所以没有保证!):
确保先执行$collection->addAttributeToSelect(’image’);,然后在遍历集合产品时执行:

$attributes = $product->getTypeInstance(true)->getSetAttributes($product);
$media_gallery = $attributes[’media_gallery’];
$backend = $media_gallery->getBackend();
$backend->afterLoad($product); //this loads the media gallery to the product object

我不确定是否有必要这么做,但是我很着急。在我的特殊情况下,我试图使用$product->getImageUrl();来获取图像URL,这种方法对我很有效。
希望对其他人有帮助。

tzcvj98z

tzcvj98z2#

最近我也曾这样做过,方法最快:

class My_Module_Block_Name extends Mage_Catalog_Block_Product_View_Abstract
{

/**@var null|Mage_Catalog_Model_Resource_Eav_Attribute */
protected static $_mediaGalleryBackend = null;

public function getGalleryImages()
{
    $product = $this->getProduct();
    $this->_getBackend()->afterLoad($product);
    $collection = $product->getMediaGalleryImages();

    return $collection;
}

/**
 * @return Mage_Catalog_Model_Resource_Eav_Attribute
 */
protected function _getBackend() {
    if (self::$_mediaGalleryBackend === null) {

        $mediaGallery = Mage::getSingleton('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');

        self::$_mediaGalleryBackend = $mediaGallery->getBackend();
    }

    return self::$_mediaGalleryBackend;
}

}
txu3uszq

txu3uszq3#

试试这个

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

llew8vvj4#

它可以直接在循环中使用:

foreach ($collection as $product) {
    $product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product);
    foreach ($product->getMediaGalleryImages() as $image) {
        var_dump($image->debug());
    }
}
kmbjn2e3

kmbjn2e35#

您必须用途:

// Returns the Media Gallery Images
Mage::getModel(’catalog/product’)->load(productid)->getMediaGalleryImages();

参考http://www.magentocommerce.com/boards/viewthread/29639/

ewm0tg9j

ewm0tg9j6#

处理涉及产品的自定义集合的秘诀是init方法的第三个参数...至少对我来说是这样。这样我就不需要加载整个产品并运行代价高昂的查询
因此,有了我的自定义$product(它表示Mage_Catalog_Model_Product的一个示例,但来自我的自定义集合),我可以执行以下操作:

(string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())

我还需要将image属性添加到我的自定义集合中,我通过将->addAttributeToSelect(['image'])添加到该集合中来实现这一点。
您也可以相应地调整图像大小:

(string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())->resize($width, $height=null)
pzfprimi

pzfprimi7#

以下是一个将媒体集添加到收藏集的函数:

// 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);

相关问题