I am using the following piece of code to allow users to set featured products that will display on the homepage but I need to extend this so that they can also dictate the sort order of the items. By default it displays the producst in the order they were added to Magento.
To do this I have created a attribute called “sort_order” that allows the users to add a numerical value that will dictate the sort order when displayed on the homepage. For example if I have 4 products then the sort order could be displayed as followed
product1 – sort order 3
product 2 – sort order 1
product 3 – sort order 2
product 4 – sort order 4
I have been trying all morning get this working. I presume I need to create an array of some sort that allows me to then order the products as dicated by the attribute “sort_order” but i’m not getting anywhere
If anyone can offer some advice I would be very grateful
<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>
anyway, here is the attribute sort function..