Knade
Knade

Reputation: 115

Magento - Sort by Position and Name?

Is it possible to sort by A the position and then B these products alphabetically shown.

I have new products and sale products coming into a category and they show all the new ones first then all the sale ones.

But I need to sort this by name.

Upvotes: 6

Views: 37654

Answers (4)

Coffee123
Coffee123

Reputation: 156

In response to clockworkgeek,

A product can have multiple attributes. When you assign each product a single position, it relates to 1 attribute. In addition, you would be required to decide a position for each product.

The problem here is that the dropdown on the product listings page offers you the opportunity to pick one of multiple attributes. For example:

Size: S, M, L, XL

When you choose an attribute, the default is to sort that attribute alphabetical which does not make sense (L, M, S, XL). Instead, I would like to use the same position of the attribute-option (s=1, m=2, l=3, xl=4) for sorting.

I am under the impression that doing this would be possible by performing a join and order by in Magento collections. Do you know how to get those values or do sorting like that?

Upvotes: 0

pasujemito
pasujemito

Reputation: 332

The best way to go about it without changing any core files is to copy the Toolbar.php file located:

/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

then create a new directory path (if you haven't created one) under:

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

Now replace the following from line 232:

    if ($this->getCurrentOrder()) {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }

to

if ($this->getCurrentOrder()) {
if(($this->getCurrentOrder())=='position'){ //defines the sort option
//sort by position (ascending) and entity_id (descending)
$this->_collection->addAttributeToSort('position','asc')->addAttributeToSort('entity_id','desc');
} else {
$this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
}
}

Lastly, reindex and refresh cache on your Magento backend and your ready to go. If you need to define more then one sort option copy and paste the code below just before } else {

if(($this->getCurrentOrder())=='######'){ //defines the sort option
//sort by ###### (ascending) and ###### (descending)
$this->_collection->addAttributeToSort('######','asc')->addAttributeToSort('######','desc');

Upvotes: 3

Bale
Bale

Reputation: 19

Instead of coding, You can do it from the admin end .Login to your adminpanel Goto System--->Configuration and choose Catalog from the left tab.

Then click the Fronend tab on the right. Goto Product Lisiting Sort by and choose Name from the dropdown and save the configuration.

To Make the sorting relatively easy. There is an extension available in the magento market. This extension will helps you to drag and drop the products to the position as per our wish. I provided the extension link below

http://www.magentocommerce.com/magento-connect/product-sorting-sequence-drag-and-drop.html

Upvotes: 0

clockworkgeek
clockworkgeek

Reputation: 37700

In admin

Go to Manage Categories, select the category, then on it's products tab give each one a position number. They will be sorted according to that order.

Programmatically

You can do this by calling a product collection's addAttributeToSort method for each ordering.

For example, wherever you see in a template, $_category->getProductCollection() or $_product->getCollection() you can then add ->addAttributeToSort('position')->addAttributeToSort('name') immediately after it.

addAttributeToSort() also takes a direction as a second parameter so you can have either addAttributeToSort('name', 'asc') or addAttributeToSort('name', 'desc').

Upvotes: 18

Related Questions