2012-03-17 22 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');