1

フロントページにカスタムブロックロード製品があり、カスタム製品画像属性が設定されている4つの最新製品をロードします。

$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(4);

私がやろうとしているのはimage_feature_front_right、バックエンドで設定されているラベルを取得することですが、それができませんでした。フロントエンドに製品を表示するための私のコードは次のとおりです。

<?php foreach($_productCollection as $_product) : ?>
    <div class="fll frontSale">
        <div class="productImageWrap">
            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) ?>" />
        </div>
        <div class="salesItemInfo">
            <a href="<?php echo $this->getUrl($_product->getUrlPath()) ?>"><p class="caps"><?php echo $this->htmlEscape($_product->getName());?></p></a>
            <p class="nocaps"><?php echo $this->getImageLabel($_product, 'image_feature_front_right') ?></p>
        </div>
    </div>

私はそれがそれを行う方法であると読みました$this->getImageLabel($_product, 'image_feature_front_right')が、何も生み出しません。私は何が間違っているのですか?

ありがとう!

トレ

4

2 に答える 2

6

あなたは別のスレッドでこれと同じ質問をしたようですので、答えを探しているかもしれない他の人を助けるために、私はここでもそれを答えます:

これはある種のMagentoのバグだと思います。問題は、Magentoコアがcustom_image_label属性を設定していないことのようです。デフォルトの組み込み画像[image、small_image、thumbnail_image]の場合、これらの属性が設定されるので、次のようなことができます。

$_product->getData('small_image_label');

あなたがそれを見ると、2番目のパラメータとして渡して.を呼び出すものにMage_Catalog_Block_Product_Abstract::getImageLabel()「_label」を追加するだけです。$mediaAttributeCode$_product->getData()

電話をかける$_product->getData('media_gallery'); と、カスタム画像ラベルが利用可能であることがわかります。配列にネストされているだけです。したがって、この関数を使用します。

function getImageLabel($_product, $key) {
    $gallery = $_product->getData('media_gallery');
    $file = $_product->getData($key);
    if ($file && $gallery && array_key_exists('images', $gallery)) {    
        foreach ($gallery['images'] as $image) {
            if ($image['file'] == $file)
                return $image['label'];
        }
    }
    return '';
}

Magentoコアコードを拡張するのが賢明です(理想的Mage_Catalog_Block_Product_Abstractには、Magentoで抽象クラスをオーバーライドできるとは思いません)が、簡単なハックが必要な場合は、この関数をphtmlファイルに貼り付けてから次を呼び出します。

<?php echo getImageLabel($_product, 'image_feature_front_right')?>
于 2012-09-08T00:59:23.457 に答える
-1

カスタムブロックは、そのメソッドへのアクセスを許可するためにMage_Catalog_Block_Product_Abstractから継承する必要があります。

テンプレートのメソッドから直接コードを使用することもできます。

$label = $_product->getData('image_feature_front_right');
if (empty($label)) {
    $label = $_product->getName();
}
于 2012-05-07T21:53:53.430 に答える