2013-07-26 48 views
0

我在我的產品上添加了yes/no屬性,這可以讓我在首頁上顯示推薦的產品(屬性設置爲yes)。現在,我想在我的主頁上顯示一個隨機產品。這是現在我的代碼:顯示Magento主頁中的隨機產品

$show_num_items = 2; 
$show_index_array = array(); 
$index_iterator = 1; 

$_productCollection = Mage::getResourceModel('reports/product_collection') 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('type_id', 'configurable'); 

if($_productCollection->getSize()): 
    $show_index_array = range(0,($_productCollection->getSize()-1)); 
    shuffle($show_index_array); 
    $show_index_array = array_slice($show_index_array, 0, $show_num_items); 

    foreach($_productCollection as $_product): 
     $_recommended = $_product->getData('recommend_product'); 
     if($_recommended == 1): 
      if(in_array($index_iterator, $show_index_array)): ?> 
       <div><img src="<?php echo $this->helper('catalog/image')->init($_product, 'image'); ?>" />  
       </div> 
       <dl> 
        <dt><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></dt> 
        <dd><?php echo $_product->getResource()->getAttribute('short_description')->getFrontend()->getValue($_product); ?></dd> 
        <dd><a href="<?php echo $_product->getProductUrl(); ?>">Read More</a></dd> 
       </dl> 
      <?php endif; 
       $index_iterator++; 
     endif; 
    endforeach; 
endif;    

這不是乾淨的,我知道,它的作品,但也有在它不返回其返回2.任何產品和實例的情況下,我怎樣才能讓這個回報1產品在任何時候?

回答

2

嘗試用$show_num_items = 1;$index_iterator = 0;,並添加->addAttributeToFilter('recommend_product', 1);到您的收藏

+0

其實,這是我做什麼,但原來沒有的產品出現了,所以我試圖改變值 – user1597438

+0

沒有注意到你的產品是否檢驗在收集完成後推薦使用。這樣,隨機選擇的產品可能不被推薦,因此不會顯示任何內容。我相應地編輯了我的答案 – blmage

+0

真棒!非常感謝。 – user1597438

相關問題