在magento中为特色产品添加排序顺序

时间:2012-03-17 18:06:33

标签: php magento e-commerce

我正在使用以下代码来允许用户设置将在主页上显示的特色产品,但我需要对其进行扩展,以便他们也可以指定项目的排序顺序。默认情况下,它按照添加到Magento的顺序显示产品。

为此,我创建了一个名为“sort_order”的属性,允许用户添加一个数值,该值将在主页上显示排序顺序。例如,如果我有4个产品,那么排序顺序可以显示如下

product1 - 排序顺序3 产品2 - 排序顺序1 产品3 - 排序顺序2 产品4 - 排序顺序4

我整个上午一直在努力让这个工作。我认为我需要创建一种类型的数组,允许我按照属性“sort_order”的顺序订购产品,但我没有得到任何地方

如果有人可以提供一些建议,我将非常感激

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

1 个答案:

答案 0 :(得分:3)

无论如何,这里是属性sort函数..

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