Magento Forum

   
Page 2 of 3
Product listing sort by
 
saloua
Jr. Member
 
Total Posts:  6
Joined:  2009-03-05
 

sorry , i have no time to describe good, but let me share how to add SORT BY DATE.
My method more simple then above’s all…

How to set visible in Atrribute manager :
/*for created_at id 100, for updated_at id 101 */
UPDATE `eav_attribute` SET `is_visible` = ‘1’ WHERE `eav_attribute`.`attribute_id` =100 LIMIT 1 ;

Then fill out Labels and set sorting:
goto catalog/ attribute/ manage attributes/ created_at / set ‘sorting’=yes / set Label/ save

Set default sort:
goto system/config/ catalog / default sort by = created_at

And finally remove attribute from manager. Sorting still changed
UPDATE `eav_attribute` SET `is_visible` = ‘0’ WHERE `eav_attribute`.`attribute_id` =100 LIMIT 1 ;

 
Magento Community Magento Community
Magento Community
Magento Community
 
AndreaShining
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2009-03-22
 
saloua - 15 October 2009 02:11 AM

/*for created_at id 100, for updated_at id 101 */

not really....if you have updated magento from an older versione created_id and updated_id could have different ids

so

use clause: WHERE attribute_code = “created_at”

or

WHERE attribute_cose = “updated_at”

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

I want to do the same thing but add ‘Stock Availability’ as a sort option (most stock to least stock).

Is there an easy way to accomplish this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
anjordan77
Sr. Member
 
Total Posts:  79
Joined:  2009-11-18
 

Anyone figure out how to sort by stock so in stock items are first and out of stock last?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Amasty
Mentor
 
Avatar
Total Posts:  3769
Joined:  2009-11-10
 
-

Anyone figure out how to sort by stock so in stock items are first and out of stock last?

This feature is not available out of the box, but you can install the Improved Sorting extension.  It shows out of stock products last.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lemaryn
Member
 
Avatar
Total Posts:  61
Joined:  2009-01-30
Ukraine
 

I added action method \"setDefaultDirection\" as desc in layout catalog.xml

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                    <
block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                        <
action method="setDefaultDirection"><dir>desc</dir></action
                        <
block type="page/html_pager" name="product_list_toolbar_pager"/> 
                    </
block>
                   
                    <
action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                </
block>

 
Magento Community Magento Community
Magento Community
Magento Community
 
S34N
Jr. Member
 
Total Posts:  22
Joined:  2010-04-23
 
lemaryn - 24 June 2010 07:08 AM

I added action method \"setDefaultDirection\" as desc in layout catalog.xml

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                    <
block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                        <
action method="setDefaultDirection"><dir>desc</dir></action
                        <
block type="page/html_pager" name="product_list_toolbar_pager"/> 
                    </
block>
                   
                    <
action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                </
block>

i tried this, but nothing happened!

do i have to place the code somewhere special, or just at the end of the catalog.xml!?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kissthesky
Jr. Member
 
Total Posts:  17
Joined:  2010-06-22
 
viditech - 02 July 2009 05:54 AM

moki - The “Used for sorting in product listing” property is grayed out for me as well and states “depends on design theme” after the drop box.  Were you able to find out how to enable this function?  Thank you!

Did you find the solution?
Does any one have solution to this? I’m using magento v1.4.1.1 and magento absolute theme.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kissthesky
Jr. Member
 
Total Posts:  17
Joined:  2010-06-22
 

The “Used for sorting in product listing” property is grayed out for me as well and states “depends on design theme” after the drop box. 
Does any one have solution to this? I’m using magento v1.4.1.1 and magento absolute theme.

 
Magento Community Magento Community
Magento Community
Magento Community
 
modernm
Member
 
Total Posts:  38
Joined:  2010-01-11
 
saloua - 15 October 2009 02:11 AM

sorry , i have no time to describe good, but let me share how to add SORT BY DATE.
My method more simple then above’s all…

How to set visible in Atrribute manager :
/*for created_at id 100, for updated_at id 101 */
UPDATE `eav_attribute` SET `is_visible` = ‘1’ WHERE `eav_attribute`.`attribute_id` =100 LIMIT 1 ;

Then fill out Labels and set sorting:
goto catalog/ attribute/ manage attributes/ created_at / set ‘sorting’=yes / set Label/ save

Set default sort:
goto system/config/ catalog / default sort by = created_at

And finally remove attribute from manager. Sorting still changed
UPDATE `eav_attribute` SET `is_visible` = ‘0’ WHERE `eav_attribute`.`attribute_id` =100 LIMIT 1 ;

In 1.4.1 I had to do the following instead…

UPDATE `catalog_eav_attribute` SET `is_visible` = ‘1’ WHERE `catalog_eav_attribute`.`attribute_id` =104 LIMIT 1 ;

Then fill out Labels and set sorting:
goto catalog/ attribute/ manage attributes/ created_at / set ‘sorting’=yes / set Label/ save

Set default sort:
goto system/config/ catalog / default sort by = created_at

And finally remove attribute from manager. Sorting still changed
UPDATE `catalog_eav_attribute` SET `is_visible` = ‘0’ WHERE `catalog_eav_attribute`.`attribute_id` =104 LIMIT 1 ;

Best thing to do is check the eav_attribute table and find the proper attribute name you want to sort by. Then update that to is_visable in the catalog_eav_attribute table long enough to set the sorting value to yes.

 
Magento Community Magento Community
Magento Community
Magento Community
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

Sort by stock:

1. Set up a new text attribute called ‘quantity’. Make sure ‘Used for Sorting in Product Listing’ is set to ‘Yes’.

2. Change the default sort order from ‘asc’ to ‘desc’ (else lowest stocks will show by default).

3. Run the following script from cron/Task Scheduler hourly (big thank you to Blakew) - remember to set your Magento path accordingly:

<?php
/******************************************************************************

Update custom Magento stock attribute

Copyright 2010, SpeedBird

******************************************************************************/
/* Load Mage */
set_include_path('/var/www/magento/app/');
require_once 
"Mage.php";
umask(0);
Mage::app('default');

/* Load product collection */
$productsCollection Mage::getModel("catalog/product")->getResourceCollection()->load();

/****************************************************************************** 

Functions to manipulate attribute options - thanks to BlakeW

******************************************************************************/
function attributeValueExists($arg_attribute$arg_value)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_options_modelMage::getModel('eav/entity_attribute_source_table') ;

        
$attribute_code         $attribute_model->getIdByCode('catalog_product'$arg_attribute);
        
$attribute              $attribute_model->load($attribute_code);
        
        
$attribute_table        $attribute_options_model->setAttribute($attribute);
        
$options                $attribute_options_model->getAllOptions(false);
        
        foreach(
$options as $option)
        
{
            
if ($option['label'== $arg_value)
            
{
                
return $option['value'];
            
}
        }
        
        
return false;
    
}


function addAttributeValue($arg_attribute$arg_value)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_options_modelMage::getModel('eav/entity_attribute_source_table') ;

        
$attribute_code         $attribute_model->getIdByCode('catalog_product'$arg_attribute);
        
$attribute              $attribute_model->load($attribute_code);
        
        
$attribute_table        $attribute_options_model->setAttribute($attribute);
        
$options                $attribute_options_model->getAllOptions(false);
        
        if(!
attributeValueExists($arg_attribute$arg_value))
        
{
            $value[
'option'= array($arg_value,$arg_value);
            
$result = array('value' => $value);
            
$attribute->setData('option',$result);
            
$attribute->save();
        
}
        
        
foreach($options as $option)
        
{
            
if ($option['label'== $arg_value)
            
{
                
return $option['value'];
            
}
        }
        
return true;
    
}
 
 
function getAttributeValue($arg_attribute$arg_option_id)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_table        Mage::getModel('eav/entity_attribute_source_table');
        
        
$attribute_code         $attribute_model->getIdByCode('catalog_product'$arg_attribute);
        
$attribute              $attribute_model->load($attribute_code);
        
                                  
$attribute_table->setAttribute($attribute);
                                  
        
$option                 $attribute_table->getOptionText($arg_option_id);
        
        return 
$option;
    
}


/****************************************************************************** 

Function to update the custom quantity attribute value 

******************************************************************************/
function updateProductQuantity($qty,$_pid)
{
    $product 
Mage::getModel('catalog/product')->load($_pid);
    
    if(!
$qty){
        $qty 
'0';
    
}else{
        $qty 
= (string)$qty;
    
}
    
    
/* Ask magento if the quantity attribute already has a value. If it does, return its attribute_id. */
    
$qty_id attributeValueExists('quantity'$qty);

    
/* quantity is already set, so simply set the new quantity */
    
if($qty_id
    
{
        $product
->setQuantity($qty);
        
$product->save();
    
}
    
/* The quantity value doesn't exist in Magento, so add it, then set it */
    
else
    
{
        $qty_id 
addAttributeValue('quantity'$qty);
        
$product->setQuantity($qty);
        
$product->save();
    
}


}

/******************************************************************************

Product Section

******************************************************************************/

$stocktotals 0;
/* Loop through products and set custom stock attribute from Magento qty */
foreach($productsCollection as $_product){
    set_time_limit
(600);
    
$_product->load($_product->getId());
    
$_pid $_product->getId();
    
$stock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); 
    
$stock_avail $_product->load($_product->getId())->getQuantity(); 
    
    
/* Only do something if the values are not the same */
    
if($stock_avail != $stock){
        
/* Call function to set stock */
        
updateProductQuantity($stock,$_pid);
    
}
}

exit(0);
?>

The above takes a long time to complete - especially if you have lots of attributes. If anyone can improve on it feel free to post your solution here.

 
Magento Community Magento Community
Magento Community
Magento Community
 
kickerpen
Jr. Member
 
Total Posts:  5
Joined:  2011-05-04
 
GuyGadbois - 01 April 2009 08:17 AM

I prefer to use date (created_at) as default, tried lots of stuff including messing around with catalog.xml but ended up with this (temporary) solution. Also hard coded sort options in the array. It’s not perfect but anyway…

(replace Extension with appr. name)

app/etc/modules/Extension_Catalog.xml

<?xml version="1.0"?>
<config>
     <
modules>
        <
Extension_Catalog>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Extension_Catalog>
     </
modules>
</
config>

app/code/local/Extension/Catalog/etc/config.xml

<?xml version="1.0"?>

<config>
   <
modules>
      <
Extension_Catalog>
         <
version>0.1.0</version>
      </
Extension_Catalog>
   </
modules>
   <global>
      <
blocks>
         <
catalog>
             <
rewrite>
               <
product_list_toolbar>Extension_Catalog_Block_Product_List_Toolbar</product_list_toolbar>
            </
rewrite>
         </
catalog>
      </
blocks>
   </global>
</
config>

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

<?php
class Extension_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

    
public function getAvailableOrders()
    
{
        
// return $this->_availableOrder;
        
        
$t = array(
            
'created_at'     => $this->__('Date'),                // sort by Date option (default)
//            'position'      => $this->__('Best Value'),
            
'name'          => $this->__('Name'),
            
'price'         => $this->__('Price')
        );
        
        return 
$t;
    
}

    
public function setDefaultOrder($field)
    
{
//        if (isset($this->_availableOrder[$field])) {
//            $this->_orderField = $field;
//        }

        
$this->_orderField 'created_at';
        
        return 
$this;
    
}

    
public function setDefaultDirection($dir)
    
{
//        if (in_array(strtolower($dir), array('asc', 'desc'))) {
//            $this->_direction = strtolower($dir);
//        }

        
$this->_direction 'desc';

        return 
$this;
    
}    
}

thank u. this is great post

 
Magento Community Magento Community
Magento Community
Magento Community
 
kickerpen
Jr. Member
 
Total Posts:  5
Joined:  2011-05-04
 

smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
louboutin18
Jr. Member
 
Total Posts:  13
Joined:  2011-04-19
 

the only thing to be done is override the tool bar block. no need to add a new module.

 
Magento Community Magento Community
Magento Community
Magento Community
 
justbeez
Jr. Member
 
Total Posts:  7
Joined:  2008-04-09
 

For those looking to change the default sort order to desc, it’s as simple as applying this Custom Layout Update to your category:

<reference name="product_list_toolbar">
    <
action method="setDefaultDirection"><dir>desc</dir></action>
</
reference>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 3