如何将产品媒体库与产品系列一起加载?

发布于 2024-11-05 10:26:02 字数 584 浏览 1 评论 0原文

有人可以给我一个关于如何加载产品媒体库和收藏的提示吗?

我得到的集合是这样的:

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

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());
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦途 2024-11-12 10:26:02

如果有人正在寻找另一种方法,我发现这是可行的(只有一种情况,所以不能保证!):

请务必先执行 $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,这种方法对我有用。

希望它对其他人有帮助。

In case anyone’s looking for another approach on this, I found this to work (in just one case so no guarantees!):

Be sure to do $collection->addAttributeToSelect(’image’); first, then when looping through the collection products, do:

$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

Not sure if all of this is necessary but I’m in a hurry. In my particular case I was trying to get the image url using $product->getImageUrl(); and this approach worked for me.

Hope it helps someone else.

猫七 2024-11-12 10:26:02

我最近不得不做同样的、最快的方法:

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

}

I had to do the same recently, fastest method:

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

}
初见终念 2024-11-12 10:26:02

试试这个

$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());
}

try this

$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());
}
烙印 2024-11-12 10:26:02

可以直接在循环中使用:

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

It can be used directly in loop:

foreach ($collection as $product) {
    $product->getResource()->getAttribute('media_gallery')->getBackend()->afterLoad($product);
    foreach ($product->getMediaGalleryImages() as $image) {
        var_dump($image->debug());
    }
}
玩世 2024-11-12 10:26:02

您将必须使用:

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

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

You are going to have to use:

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

Reference: http://www.magentocommerce.com/boards/viewthread/29639/

盛夏已如深秋| 2024-11-12 10:26:02

处理涉及产品的自定义集合时的秘密武器是 init 方法的第三个参数......至少对我来说是这样。 这样我就不需要加载整个产品并运行昂贵的查询

因此,拥有代表 Mage_Catalog_Model_Product 实例的自定义 $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)

The secret sauce when working with custom collections involving products is the third parameter of init method... at least it was for me. This way I don't need to load the whole product and run expensive queries.

So, having my custom $product which represents an instance of Mage_Catalog_Model_Product but from my custom collection, I can do:

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

I also needed to add the image attribute to my custom collection, and I did that by adding ->addAttributeToSelect(['image']) to it.

You can also resize your image accordingly:

(string)Mage::helper('catalog/image')->init($product, 'small_image', $product->getImage())->resize($width, $height=null)
下壹個目標 2024-11-12 10:26:02

这是将媒体库添加到集合中的函数:

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

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

An example of it's usage below:

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
$this->addMediaGalleryToArray($products);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文