Question

Nous avons étendu un curseur page d'accueil pour un client afin qu'ils puissent en vedette des produits dans cet espace.

Dans le cadre de cela, il y a trois fentes d'image où l'on souhaite obtenir l'image principale de produits, puis deux images de la galerie de médias (idéalement au hasard, mais pas la fin du monde si par ID).

Pour une meilleure compréhension, s'il vous plaît voir capture d'écran de ce que nous avons jusqu'à présent: -

featprodslider

Nous construisons la collection pour ce module en utilisant les éléments suivants: -

$featured_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->AddAttributeToFilter('featured', array('eq' => 1));

Obtenir ce fonctionne parfaitement l'image principale est pas un problème du produit, ce qui suit: -

<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />

Et il est assez simple de faire les trois slots d'image utilisent cette image principale comme le montre l'image ci-dessus.

Quand on essaie d'appeler getGalleryImages cependant, cela renvoie toujours NULL (tel que par exemple): -

<?php if (count($this->getGalleryImages()) > 0): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
<?php endforeach; ?>
<?php endif; ?>

S'il vous plaît quelqu'un pourrait conseiller de la meilleure approche pour appeler les images de la galerie sur la page d'accueil. Y at-il quelque chose que nous pouvons inclure la construction de collection ou aurions-nous besoin d'ajouter un observateur.

Merci à l'avance.

Était-ce utile?

La solution 3

We resolved this in the end and as @Marius points out, needed to be looking at getMediaGalleryImages.

The code we went with in the end is:-

<div class="images">
    <?php $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); ?>    
    <?php if($_images){?>            
        <?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
            <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
        <?php } ?>
    <?php } ?>
</div>

And here we load the product's media images and ensure we only show 3 for the 3 slots we have available in the product slider. Interested to know though if a better approach could have been achieved with your answer @Marius.

Autres conseils

I think the main question here is what type is $this in your context. It its a simple block it's kind of normal to return null if the method does not exist in the block.
On an other view....here is how you can get the images associated to a product assuming the variable $_product is an instance of the product itself:

$gallery = $_product->getMediaGalleryImages();

So in your case you might need something like this:

<?php foreach ($featured_products as $_product)  : ?>
     <?php if (count($_product->getGalleryImages()) > 0): ?>
         <?php foreach ($_product->getGalleryImages() as $_image): ?>
             <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
         <?php endforeach; ?>
     <?php endif; ?>
<?php endforeach;?>

@zigojacko @Marius Recently I've come to a situation where I need to get all media gallery images from product collection without loading product model again as it will increase execution time and use extra memory. So I did R&D and found solution use below line before getMediaGalleryImages on the products you loaded in the collection.

$product->load('media_gallery');

Check code snippets below:

$products = Mage::getModel("catalog/product")->getCollection();
$galleryImagesLink = [];
$product->load('media_gallery');
$_images = $product->getMediaGalleryImages();
foreach ($_images as $image) {
    $galleryImagesLink[] = Mage::getModel('catalog/product_media_config')->getMediaUrl($image->getFile());
}

zigojacko asked for a better approach of his working code

Here it is: no need to load full product

<div class="images">
    <?php $_images = $product->load('media_gallery')->getMediaGalleryImages(); ?>    
    <?php if($_images){?>            
        <?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
            <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
        <?php } ?>
    <?php } ?>
</div>

Old question, but common problem... So maybe this helps other users

Even better, write your own method to get media_gallery values, so request will process kindly faster. Something like this...

protected $_resource = null;

public function getResource()
{
    if (is_null($this->_resource)) {
        $this->_resource = Mage::getSingleton('core/resource');
    }
    return $this->_resource;
}

protected function _getMediaPosition($valueId)
{
    $data = array();
    $readConnection = $this->getResource()->getConnection('core_read');
    $query = $readConnection->select()
        ->from($this->getResource()->getTableName('catalog_product_entity_media_gallery_value'))
        ->where('value_id = ?', $valueId);
    $results = $readConnection->fetchAll($query);
    if (! empty($results)) {
        $data = $results[0];
    }

    return $data;
}

public function getMediaGalleryImages($productId)
{
    $data = array();
    $gallery = array();

    $readConnection = $this->getResource()->getConnection('core_read');
    $query = $readConnection->select()
        ->from($this->getResource()->getTableName('catalog_product_entity_media_gallery'))
        ->where('entity_id = ?', $productId);
    $results = $readConnection->fetchAll($query);

    if (! empty($results)) {
        foreach ($results as $mediaGalleryRow) {
            $gallery[] = array_merge($mediaGalleryRow, $this->_getMediaPosition($mediaGalleryRow['value_id']));
        }
    }

    foreach ($gallery as $i => $element) {
        if ($element['disabled'] == 1) {
            continue;
        }

        $obj = new Varien_Object();
        $obj->setFile($element['value']);
        $data[$element['position'] . '_' . $i] = $obj;
    }

    ksort($data);
    return $data;
}

So, after adding that code in your block, template code would be

<div class="images">
    <?php $_images = $this->getMediaGalleryImages($product->getId()); ?>    
    <?php if($_images){?>            
        <?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
            <img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
        <?php } ?>
    <?php } ?>
</div>
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top