2

I'm working on a magento site (Magento ver. 1.5.1.0) - Plese note in answering I am new to Magento. I am trying to get an option to sort by bestsellers I have managed to do this by adding local files to override the core files as follows:

httpdocs/app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php:

 public function getAvailableOrders()
{
    //return $this->_availableOrder;
    //Custom Order list Edit
    $this->_availableOrder = array(
        'qty_ordered' => $this->__('Best Sellers'),
        'entity_id' => $this->__('Latest arrivals'),
        'name' => $this->__('Name'),
        'price' => $this->__('Price')
    );
    //Custom Available Order -Edit finish
    return $this->_availableOrder;
}

And httpdocs/app/design/frontend/default/localsite/template/catalog/product/list/toolbar.phmtl:

<fieldset class="sort-by">
    <label><?php echo $this->__('Sort by') ?></label>
    <select onchange="setLocation(this.value)">
        <option value="<?php echo $this->getOrderUrl('entity_id', 'desc') ?>"<?php if($this->isOrderCurrent('entity_id') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
            Newest Products
        </option>
        <option value="<?php echo $this->getOrderUrl('qty_ordered', 'desc') ?>"<?php if($this->isOrderCurrent('qty_ordered') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
            Best Sellers
        </option>
        <option value="<?php echo $this->getOrderUrl('price', 'asc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
            Lowest Price
        </option>
        <option value="<?php echo $this->getOrderUrl('price', 'desc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
            Highest Price
        </option>
        <option value="<?php echo $this->getOrderUrl('name', 'asc') ?>"<?php if($this->isOrderCurrent('name') && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
            Name A-Z
        </option>
        <option value="<?php echo $this->getOrderUrl('name', 'desc') ?>"<?php if($this->isOrderCurrent('name') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
            Name Z-A
        </option>
    </select>
</fieldset>

This works in the sense that I now have a bestsellers sort option which sorts in the same way as the bestsellers on the dashboard. Unfortunately the bestsellers section of the dashboard and consequently my sort list is entirely wrong.

Does anyone know how to fix this or failing that does anyone know another attribute I can sort by to achieve the same result - if I go to Dashboard>reports>products>Products Ordered I can get the order I want with a wide date range - any way to recreate this in the sort options.

Is there a list of available sort options out there somewhere?

******************UPDATE*******************

I have now noticed that the products listed in the bestsellers are all older products. The last 100 or so products which have been added are not included in the bestsellers list. These same products are also listed seperately if I sort by a to Z either in the dashboard or using the above sort option so that they go new products A to Z followed by older products A to Z. Any ideas what could be causing this?

Any tips, advice, help much appreciated.

1
  • @Josh Caswell Thanks for your interest in the matter and sorry for the slow reply - I have updated the question in response to your comment - hopefully this new info will help.
    – SwiftD
    Feb 21, 2012 at 16:05

1 Answer 1

1

Since catalog products don't have an attribute called qty_ordered, you'll not have much luck that way. Whatever ordering you're seeing happen is either a secondary sorting column in action or the natural order coming from the database.

The backend uses the Mage_Reports module for view counts and the like. The most obvious way of getting to your goal seems to be adding a custom attribute to your products and writing an event observer to update it when sales are made. If you make sure to set "Used for Sorting in Product Listing" it should be available for sorting automagically without having to override any core classes.

Other than that, the only things I can think of involve a lot of complicated class overrides that would very likely break something on upgrade.

5
  • Thank you for your answer. As I said in my question I am new to Magento so I am not really sure how to implement what you are suggesting. I will do a bit of research and see how far I can get with the suggestions made.
    – SwiftD
    Feb 27, 2012 at 14:03
  • I found this article to be quite helpful in understanding how to work the event observer system. Basically, you'll need to create a simple module that contains a config.xml, a setup script to add your attribute, and an event observer. I know it seems daunting, but it's not as hard as it looks. Good luck!
    – wierdo
    Feb 27, 2012 at 21:02
  • Thank you for the link, I will have a crack at it this week and hopefully return in triumph.
    – SwiftD
    Feb 29, 2012 at 19:04
  • I got some way with this approach but the client is now switching off magento so i never really got a chance to see this through. Thanks for your help
    – SwiftD
    Apr 12, 2012 at 17:34
  • Thanks for the follow up. Too bad you didn't get a chance to make it go. Them's the breaks, I guess.
    – wierdo
    Apr 19, 2012 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.