makemelive makemelive - 1 year ago 80
PHP Question

Magento Product Custom Ordering

I have a magento based website (

Here you can see a best seller section , I have added a custom field "Featured" from which the products are tagged to featured that are shown in best sellers.

My problem is that I want to re-arrange the products as per the sort weight custom field I have defined in the product catalog.

I have attached a custom field sort_order to the product catalog and every product has its sort weight/number like 1,2,3,4 etc. I want to show the product according to the numbering in ascending order.

I have a for each loop which gives me all the attributes of products just want a help how can a loop all the products inside for each in ascending order of given numbers (1,2,3 etc). Below is my for each code:

<?php foreach ($_productCollection as $_product): //print_r($_product); ?>

<?php if($_product->featured == 1): ?>

<div class="reco-sgl">

<div class="product-image-wrapper borderlnright">
<div class="single-products">
<div class="productinfo text-center">
<a href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<p><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></p>
<?php echo $this->getFeaturedProductHtml() ?>

<h4><?php echo $this->getPriceHtml($_product, true) ?></h4>
<a href="<?php echo $_product->getProductUrl() ?>" class="btn btn-default add-to-cart">Add to Cart</a> </div>


<?php endif; ?>
<?php endforeach ?>

Please help .

Answer Source

You have to create 2 product attributes one is YES/NO type.

  1. "featured_product" for Featured products
  2. "sort_order" for custom sorting number like 1,2,3,4 etc

    $limitnumber = 10;
    $products = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')->addAttributeToFilter('featured_product', array('eq' => '1'))->addAttributeToSort('sort_order', 'asc');
    // version magento CM
    foreach ($products->getItems() as $product){
        echo $product->getName();
        // your code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download