想通了。我爲簡單的產品添加了一個名爲'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);
這樣產品圖像也會使用相同的排序。
我希望這不會對性能產生巨大影響(客戶端排序表明這是一個主要要求)。如果客戶沒有在簡單產品上設置排序順序,它會很好地降級。如果庫存缺貨,它會按排序順序顯示下一張圖片。
任何批評都會受到歡迎!