Hot answers tagged

45

EAV collections work with attributes, the sorting method is a little different here as well $componentQuantityCollection->addAttributeToSort('sku', 'ASC'); For non-EAV collections use one of the following methods $kitCollection->getSelect()->order('related_sku DESC'); $kitCollection->setOrder('related_sku', 'DESC');


43

You cannot change the default order direction for product lists from configuration. You can however change the direction from layout XML. Here are a few examples that you can put in your layout XML file, e.g. local.xml: <?xml version="1.0"?> <layout version="0.1.0"> <!-- Change default direction for simple searches --> <...


25

Note: It has been brought to my attention that this solution does not work for Magento 1.9.2. In order to save others wasted time I'd like to point this out at the top of this post. If I develop my own solution or find someone else's solution that works for 1.9.2 I will update this post at that time. Notice: The solution laid out here extends a block class ...


23

If you want to use an attribute like created_at that it isn't in admin->stores->(attribute) product, because attributes defined in admin have the setting Sorting in Product Listing = Yes/No, you have to work with these two files: \vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php \vendor\magento\module-catalog\Model\Config.php In ...


22

We can achieve it by using Plugins. Please create following files in your module. app/code/Package/CustomToolbar/etc/di.xml <type name="Magento\Catalog\Model\Config"> <plugin name="Package_CustomToolbar::addCustomOptions" type="Package\CustomToolbar\Plugin\Model\Config" /> </type> <type name="Magento\Catalog\Block\Product\...


21

You can add this code in the __construct method of your grid block after calling parent::__construct();: $this->setDefaultSort('sort_order'); $this->setDefaultDir('ASC'); EDIT: For frontend you can use this: $collection = Mage::getResourceModel('banners/manage_collection'); $collection->setOrder('sort_order','DESC');


20

Sadly Magento will validate the order options in Varien_Data_Collection_Db _setOrder function. But you can get the select of the collection and add a new expression to build the order as you like. /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */ $collection = Mage::getModel('catalog/product') ->getCollection() ->...


17

I found the answer after some intense searching on Stack Overflow all i had to do was add these lines to my layout.xml <catalogsearch_result_index> <reference name="search_result_list"> <action method="setDefaultDirection"><param>asc</param></action> <action method="setSortBy"><param>...


15

You can add sort order like this: $kitCollection->getSelect()->order('related_sku DESC'); More information: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento Hope can help you.


11

Just to add my two cents, the other two answers did well to point me in fix's direction, but I thought I'd attack it at the source rather than the block presentation point. You can achieve the same result by extending the Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection model's _loadPrices() method, which despite the name is where ...


11

We'll need to fix this in 2 parts. The first part is actually tracking which simple products in the cart come from grouped products. The second is the sorting. First for the tracking. We'll add an extra column in sales_flat_quote_item to store that information. To do this properly write your own module, a tutorial about that you can find here on Tutsplus....


9

There are two ways to do it 1) Using Magento Admin Panel 2) Using Custom Coding 1) To add using Magento Backend you need to follow below steps a) Goto Catalog->Attributes->Manage Attributes b) Select the attribute you want to add in Sort By option. Suppose you need to add a Sort By Brand Option. c) Goto Brand Attribute and check for "Used for Sorting ...


8

Short answer: The sorting doesn't work for Yes/No attributes. Long Answer: The problem: When calling addAttributeToSort on a product collection, if the attribute has a source model then Magento calls addValueSortToCollection from that source model to sort the products. The Yes/No attributes use the source model ...


8

To sort products by rating you can use the following code, taken from this module: $collection->joinField( 'rating_summary', // alias 'review/review_aggregate', // table 'rating_summary', // field 'entity_pk_value=entity_id', // bind array( 'entity_type' => 1, ...


8

The available sorting methods are addOrder(), setOrder(), unshiftOrder(), addAttributeToSort() and getSelect()->order() Only setOrder($field, $dir) is available regardless of collection type and system settings. I suggest you get used to using setOrder(). If you need to enforce an order, but an order is already set on the collection, use unshiftOrder($...


8

You need to a custom extension with plugins, I followed this app/code/Company/CustomSort/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Model\Config"> <plugin name="...


8

quick workaround can be add a condition in theme templates file Magento_Catalog/templates/product/list/toolbar/sorter.phtml like if $key != 'position' then don't add in drop down options : <select id="sorter" data-role="sorter" class="sorter-options"> <?php foreach ($block->getAvailableOrders() as $_key => $_order): ?> <?...


7

Ok, I try to explain my solution, I give here most important points, but you should have some experiences like magento layouts, blocks, create custom module. 1. Create attribute which you assumed in your question. 2. Configure your products attaching them to particular category and attribute value created above. 3. Create custom block given in example (/app/...


7

for -> Recently Viewed see here for ->Sorting by rating Copy the file app/code/core/Mage/Catalog/Block/Product/List.php to app/code/local/Mage/Catalog/Block/Product/List.php in list.php find for this line $this->_productCollection =$layer->getProductCollection(); which will be in around line no 86 add the following code after that $this->...


7

I would suppose that search is implemented using MySQL FULLTEXT index. Here is a quote from MySQL official documentation regarding to how that index works: Rows returned are automatically sorted with the highest relevance first. Relevance values are nonnegative floating-point numbers. Zero relevance means no similarity. Relevance is computed based on the ...


7

You will need to add sort order to search criteria Add \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder as dependency in constructor. public function getCollection() { $sortOrder = $this->sortOrderBuilder->setField('position')->setDirection('DESC')->create(); $searchCriteria = $this->searchCriteriaBuilder->...


6

Ok. I should wait an hour more before posting a question. This seams to work. I've rewritten Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSort() and made it look like this (not a pretty code but works, layered navigation and all): <?php class Namespace_Catalog_Model_Resource_Product_Collection extends ...


6

you cannot modify the attribute created_at in the backend. So you need to create a module that just activates the created_at attribute for sorting. For this you will need the following files: app/etc/modules/[Namespace]_[Module].xml - the declaration file <?xml version="1.0"?> <config> <modules> <[Namespace]_[Module]>...


6

Question is tagged CE 1.7.0.2 but this solution is useful for those on 1.9.1.1 so I'm adding it here. We had an opposite problem where we needed to show the cheapest products first when sorted by Price. For the OP's first concern, this doesn't apply because in Magento CE 1.9 it already defaults to descending so this is actually not needed. The second ...


6

Never mind, I figured it out. I can just call Varien_Data_Collection_Db::setOrder twice, e.g.: $collection->setOrder('bill_to', 'DESC'); $collection->setOrder('created_at', 'ASC'); This example would first sort the collection by the bill to attribute and then the created at attribute.


6

Typical! After hours of searching I make a post...only to find the answer 5 minutes later. Modify the local file app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php and make line 41 read: $this->setDefaultSort('created_at');


6

The reason you got two lists with ascending order is because of PHP sort() function. The default sorting behavior for this function is non-natural ordering, so it gives you unpredictable results. Try natcasesort() instead of sort(). See more detail about PHP sort() on below link: http://php.net/manual/en/function.sort.php OR try to sorting your ...


6

Go to Stores > Configuration > Sales > Payment Methods there you find 'Sort Order' field corresponding to all payment methods. So, provide greater number to paypal and lesser number to sagepay to rearrange it.


5

Make sure the archive attribute is added to the flat product data by setting Used in Product Listing to yes. Also it's probably better to load the product collection via the catalog/category model instead of joining another table. Also, to have yes come as last make the order ascending instead of descending $category = Mage::getModel('catalog/category')-&...


5

First add a dummy attribute called 'discount_percent' and make it "used for sorting" (just as Giuseppe said in the other answer). It's not important what type it is or what values it has. it's easier that way. If you don't want to add the attribute you have to rewrite the method Mage_Catalog_Model_Config::getAttributeUsedForSortByArray to include your ...


Only top voted, non community-wiki answers of a minimum length are eligible