Magento Forum

   
Page 3 of 3
Product listing sort by
 
greenamit
Jr. Member
 
Total Posts:  24
Joined:  2011-10-05
 
SpeedBird - 06 December 2010 05:33 AM

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.

I did all the above but when I try to run the script I get the following errors:

Fatal error: Uncaught exception ‘Mage_Core_Model_Store_Exception’ in /home/greenami/public_html/tik2go/app/code/core/Mage/Core/Model/App.php:1318
Stack trace:
#0 /home/greenami/public_html/tik2go/app/code/core/Mage/Core/Model/App.php(830): Mage_Core_Model_App->throwStoreException()
#1 /home/greenami/public_html/tik2go/app/code/core/Mage/Core/Model/App.php(481): Mage_Core_Model_App->getStore()
#2 /home/greenami/public_html/tik2go/app/code/core/Mage/Core/Model/App.php(269): Mage_Core_Model_App->_initCurrentStore(’default’, ‘store’)
#3 /home/greenami/public_html/tik2go/app/Mage.php(583): Mage_Core_Model_App->init(’default’, ‘store’, Array)
#4 /home/greenami/public_html/tik2go/qtysorter.php(14): Mage::app(’default’)
#5 {main}
thrown in /home/greenami/public_html/tik2go/app/code/core/Mage/Core/Model/App.php on line 1318

Any idea what is wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mauson
Jr. Member
 
Total Posts:  7
Joined:  2011-03-24
 
modernm - 03 December 2010 07:51 AM

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.

This works PERFECT, I´ve been trying different methods for HOURS, and this is definitively the best solution. Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Raphael Petrini
Member
 
Total Posts:  48
Joined:  2010-06-08
 

Yep the best method is definitely the one saloua proposed :

1. get the attribute_id of your ‘updated_at’ attribute (that is the best attribute to sort by new)

select attribute_id from eav_attribute where attribute_code 'updated_at';

2. make this attribute visible in the backend (replace the id with the result of your previous SQL query

UPDATE `catalog_eav_attributeSET `is_visible` = '1' WHERE `catalog_eav_attribute`.`attribute_id` = 105;

3. go to the backend Catalog>Attributes>Manage Attributes>Edit the ‘updated_at’ attribute> Set the ‘Used for Sorting in product listing’ to Yes. Also, change the label displayed in the fronted in the second tab with what you want “New” or “Newest to Oldest”. Save

4. you may have to reindex your attributes index

5. go to System>Configuration>Catalog>Catalog>Frontend>Product Listing Sort By> Choose ‘updated_at’ (or whatever your backend label is)

6. hide the attribute in the backend (to avoid unwanted issues in case you’ve got several admins) (once again, don’t forget to replace the id with the one corresponding to your attribute)

UPDATE `catalog_eav_attributeSET `is_visible` = '0' WHERE `catalog_eav_attribute`.`attribute_id` = 105;

7. be sure your default sorting direction is set to ‘desc’, edit the catalog.xml file in your theme and add the following line:

<action method="setDefaultDirection"><dir>desc</dir></action>
between:
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
and:
<block type="page/html_pager" name="product_list_toolbar_pager"/>
(you have to do it twice, once inside the <catalog_category_layered> block and once inside the <catalog_category_default> block

8. flush the cache, grab a beer and enjoy

 
Magento Community Magento Community
Magento Community
Magento Community
 
maria_atwix
Sr. Member
 
Avatar
Total Posts:  129
Joined:  2012-01-23
 

If you face the similar situation we hope that this article will help http://www.atwix.com/magento/custom-sorting-product-listing/
Our team has some experience of this topic and we are glad to share information

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