2012-03-17 51 views
0

我正在使用下面這段代碼來允許用戶設置將在主頁上顯示的精選產品,但我需要擴展它以便他們也可以決定項目的排序順序。默認情況下,它按照添加到Magento的順序顯示產品。在magento中加入排序方式到精選產品

爲此,我創建了一個名爲「sort_order」的屬性,允許用戶添加一個數值,該值在主頁上顯示時將指定排序順序。例如,如果我有4個產品然後排序順序可被顯示爲跟隨

產品1 - 排序次序3 產物2 - 排序次序1個 產物3 - 排序次序2 產物4 - 排序次序4

我一直在嘗試整個上午得到這個工作。我相信我需要建立某種形式的,讓我再訂購產品的屬性「SORT_ORDER」作爲dicated的數組,但我不成氣候

如果任何人都可以提供一些建議,我將非常感激

<div id="home-featured"> 
    <h2><?php echo $this->__('Featured') ?></h2> 
    <?php 
     // some helpers 
     $_helper = $this->helper('catalog/output'); 
     $storeId = Mage::app()->getStore()->getId(); 
     $catalog = $this->getLayout()->createBlock('catalog/product_list')->setStoreId($storeId); 

     // get all products that are marked as featured 
     $collection = Mage::getModel('catalog/product')->getCollection(); 
     $collection->addAttributeToSelect('featured_product'); 
     $collection->addFieldToFilter(array(
      array('attribute' => 'featured_product', 'eq' => true), 
     )); 

     // if no products are currently featured, display some text 
     if (!$collection->count()) : 
    ?> 
    <p class="note-msg"><?php echo $this->__('There are no featured products at the moment.') ?></p> 
    <?php else : ?> 
    <div class="category-products"> 
    <?php 
     $_collectionSize = $collection->count(); 
     $_columnCount = 4; 
     $i = 0; 

     foreach ($collection as $_product) : 
     $_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($_product->getId()); 
    ?> 
    <article> 
     <div class="product-image"><a href="<?php echo Mage::helper('fullurl')->getFullProductUrl($_product); ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->keepFrame(false)->resize(170); ?>" width="170" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a></div> 
     <div class="featured-info"> 
     <h3><a href="<?php echo Mage::helper('fullurl')->getFullProductUrl($_product); ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h3> 
     <p><?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description ') ?></p> 
     <span class="link-dreambuilder"><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" ><?php echo $this->__('+ Add to Dream Builder') ?></a></span> </div> 
    </article> 
    <?php endforeach ?> 
    </div> 
    <?php endif ?> 
</div> 
+0

哪裏是你試過排序功能?我在上面沒有看到... – 2012-03-17 18:53:04

回答

3

反正這裏是屬性排序功能..

addAttributeToSort($attribute, $dir='asc');