2011-06-24 3 views
3

私は構成可能でシンプルな商品(複数色)のある店舗があります。現在、単純な製品イメージの1つを選択し、構成可能な製品に割り当てます。これがリストページに表示されます。問題は、その特定の色が在庫切れになった場合、利用できない製品を表す画像が残っていることです(手動でその画像を更新する必要があります)。magentoリストページのシンプルな商品画像を使用

リストページでシンプルな製品イメージを使用する方法はありますが、どのイメージが使用されているかを制御することはできますか?私はリストページで単純なイメージを使用する方法を知っていますが、私は簡単なイメージを指定する方法を理解できません(現在、私は単純な製品をつかんで、リストの最初のイメージからイメージを引っ張っています)。

構成可能な製品内で単純な製品を並べ替える方法が見つかった場合(つまり、製品Aでは単純な製品は黒、緑、青でソートされ、製品Bでは単純な製品が緑色にソートされ、青、黒)、私は残りの部分を把握することができると思う。

答えて

1

を見つけました。私は 'sort_order'と呼ばれる単純な製品の新しい属性を追加しました。そして、私はカタログ/製品のヘルパーをオーバーライドし、次のメソッドを追加しました:

public function getSortedSimpleProducts($product) { 
    $products = array(); 

    $allProducts = $product->getTypeInstance(true)->getUsedProducts(null, $product); 

    foreach ($allProducts as $product) { 
     if ($product->isSaleable()) { 
     $products[] = $product; 
     } 
    } 
    $sorted_products = array(); 
    $unsorted_products = array(); 

    foreach ($products as $simple_product) { 
     $sort_order = $simple_product->getData('sort_order'); 
     if ($sort_order) { 
     $sorted_products[$sort_order] = $simple_product; 
     } 
     else { 
     $unsorted_products[] = $simple_product; 
     } 
    } 

    $final_products = $sorted_products; 
    if (count($unsorted_products) > 0) { 
     $final_products = array_merge($sorted_products, $unsorted_products); 
    } 
    if (count($final_products) > 0) { 
     sort($final_products); 
    } 

    return $final_products; 

    } 

そして、この線を中心list.phtmlテンプレート、中:

<?php $i=0; foreach ($_productCollection as $_product): ?> 

私は、次のコードを追加しました

$image_product = $_product; 
$products = $this->helper('catalog/product')->getSortedSimpleProducts($_product); 
if (count($products) > 0) { 
    $image_product = $products[0]; 
} 
と更新私のイメージタグ:

<img src="<?php echo $this->helper('catalog/image')->init($image_product, 'small_image')->resize(189,238); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /> 

、私は(ビュー・ページ上の色の分類を決定する)新しいソート順でソートgetAllowProductsを作るためにMage_Catalog_Block_Product_View_Type_Configurableをオーバーライド:

public function getAllowProducts() { 
    if (!$this->hasAllowProducts()) { 
     $products = array(); 
     $allProducts = Mage::helper('catalog/product')->getSortedSimpleProducts($this->getProduct()); 
     foreach ($allProducts as $product) { 
     if ($product->isSaleable()) { 
      $products[] = $product; 
     } 
     } 
     $this->setAllowProducts($products); 
    } 
    return $this->getData('allow_products'); 
    } 

、その後media.phtmlファイル更新:

$childProducts = $this->helper('catalog/product')->getSortedSimpleProducts($_product); 

このように、製品イメージでも同じ種類が使用されます。

私は、これがパフォーマンスに大きな影響を与えないことを望んでいます(これが主要な要件であることがクライアントから分かりました)。クライアントがシンプルな製品のソート順を設定しない場合は、うまく機能しません。在庫がなくなると、ソート順に次の画像が表示されます。

批評は歓迎されるでしょう!

関連する問題