質問

クライアント用のホームページスライダーを拡張して、このスペースに製品を搭載できるようにしました。

その一部として、製品のメイン画像を取得したい3つの画像スロットがあり、メディアギャラリーから2つの画像(理想的にはランダムには、IDでは世界の終わりではありません)から2つの画像があります。

より良い理解を得るには、これまでに持っているもののスクリーンショットをご覧ください: -

featprodslider

以下を使用して、このモジュールのコレクションを構築します。-

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

製品の主な画像を取得することは問題ありません。これは次のものと完全に機能しています。

<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) ?>" />

また、上の画像に示すように、3つの画像スロットすべてがこのメイン画像を使用するようにするのは簡単です。

しかし、getgalleryimagesに電話しようとすると、これは常に戻ります NULL (例など): -

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

ホームページのギャラリー画像を呼び出すための最良のアプローチについて誰かがアドバイスしてください。コレクションビルドに含めることができるものはありますか、それともオブザーバーを追加する必要がありますか?

前もって感謝します。

役に立ちましたか?

解決 3

私たちは最終的にこれを解決しました、そして@Mariusが指摘しているように、見る必要がありました getMediaGalleryImages.

最後に行ったコードは次のとおりです。

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

ここでは、製品のメディア画像をロードし、製品スライダーで利用できる3つのスロットに3つのみを表示するようにします。しかし、あなたの答え@Mariusでより良いアプローチが達成されたかもしれないかどうかを知りたいです。

他のヒント

ここでの主な質問は、どのようなタイプかだと思います $this あなたの文脈で。それは単純なブロックであり、戻るのは普通のことです null メソッドがブロックに存在しない場合。
他のビューについて....ここに、変数を仮定して製品に関連付けられた画像を取得する方法があります $_product 製品自体のインスタンスです。

$gallery = $_product->getMediaGalleryImages();

あなたの場合、あなたはこのようなものが必要かもしれません:

<?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最近、製品モデルを再度読み込むことなく、製品モデルを再度読み込むことなく、製品ギャラリーのすべての画像を製品コレクションから取得する必要がある状況に到達しました。そこで、私はR&Dを作成し、コレクションにロードされた製品のgetMediagalleryimagesの前に、以下のソリューションの使用を見つけました。

$product->load('media_gallery');

以下のコードスニペットを確認してください。

$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 彼の作業コードのより良いアプローチを求めました

ここにあります:完全な製品をロードする必要はありません

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

古い質問ですが、一般的な問題...だから、これは他のユーザーに役立つかもしれません

さらに良いことに、Media_Galleryの値を取得する独自の方法を作成するため、リクエストは親切に処理されます。このようなもの...

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

したがって、ブロックにそのコードを追加した後、テンプレートコードは

<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>
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top