2011-06-24 47 views
3

我有一個商店,配置和簡單的產品(多種顏色)。目前,我們選擇一個簡單的產品圖像並將其分配給可配置產品,這就是列表頁面上顯示的內容。問題是,如果該特定顏色缺貨,我們會停留在代表產品不可用(並且不得不手動更新該圖像)的圖像上。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) ?>" /> 

然後我推翻Mage_Catalog_Block_Product_View_Type_Configurable爲了通過新的排序順序(它決定了視圖頁上的顏色分選),以使getAllowProducts排序:

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

這樣產品圖像也會使用相同的排序。

我希望這不會對性能產生巨大影響(客戶端排序表明這是一個主要要求)。如果客戶沒有在簡單產品上設置排序順序,它會很好地降級。如果庫存缺貨,它會按排序順序顯示下一張圖片。

任何批評都會受到歡迎!

相關問題