Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:55:38+00:00 2026-06-17T16:55:38+00:00

I am trying to create a block which shows the 5 top selling products,

  • 0

I am trying to create a block which shows the 5 top selling products, 5 most viewed products and 5 largest discounted products on a Magento 1.7.0.2 site.

The block is defined as follows in local.xml:

<reference name="content">
   <block type="core/template" name="movies_block" template="catalog/product-block.phtml" before="-">
      <action method="setData"><name>category_id</name><value>5</value></action>
   </block>
</reference>

(you pass it a category id, the idea being it can be quickly reused throughout my theme for categories of my choosing)

I have written and tested code for displaying top sellers and most viewed, but I’m struggling with a way to efficiently sort the top 5 most discounted. I’m defining a discount as the % difference between ‘price’ and ‘special_price’.

My code for this is as follows:

$visibility = array(
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$catNum = $this->getCategoryId(); // grab cat id from xml
$category = Mage::getModel('catalog/category')->load($catNum);

// load products from $category
$_productCollection = Mage::getResourceModel('catalog/product_collection')
                          ->addAttributeToSelect('*')
                          ->addAttributeToFilter('visibility', $visibility)
                          ->addCategoryFilter($category);

$saleProducts = array();        
$i=0;                                           
foreach($_productCollection as $product): 
   if($product->getSpecialPrice()): // if a product has a special price
      /* calculate discount as percentage and add it to $product array of each */
      $product['discount'] = $product->getSpecialPrice()/$product->getPrice()*100;
      $saleProducts[$i] = $product; // put discounted products into separate array
      $i++;
   endif;
endforeach;

This leaves an array $saleProducts, of products which have a ‘special_price’. However, I want just the top 5 most discounted (highest value in $product[‘discount’] first).

I’ve been attempting to sort the array using:

function cmp($a, $b) {
   return ($a['discount'] - $b['discount']);
}

usort($saleProducts, "cmp");

But as soon as I add anything to my code with ‘function’ in it, the whole block no longer displays… happens even if I just write:

function sortByDiscount($a, $b) {};

anywhere in the .phtml file, is Magento preventing me from creating a function? Anybody else tackled this particular problem? I have a feeling my way might be quite slow, especially since I used other product collections with filters earlier on in the same .phtml file, and this block needs to work with categories of up to 20,000 products.

Any suggestions welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:55:40+00:00Added an answer on June 17, 2026 at 4:55 pm

    Anonymous functions are supported from PHP 5.3 on so you won’t be able to use them.

    As far as I know phtml file is included into it’s block class so you can’t write a new function in a phtml file. You will have to extend the block class or create a new helper class and then call

    usort( $saleProducts, array('foo', 'bar'));
    

    where foo is your class name and bar is the function name (and the comparator function should be static).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a chrome extension which shows tweets with hashtag perkytweets in
I'm trying to create a block which represents an order and that block has
I am trying to create a new process (which shouldn't block the current program)
I'm trying to create a UIView which shows a semitransparent circle with an opaque
I'm trying to create a function that shows a modal dialog which when called
I am trying to create a function that will block access to some of
I'm trying to create a parsing system for c#, to block my program from
I am trying to block all default methods except create and update in my
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I need to create a dynamically generated window which shows an image and various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.