0

I have configurable product create with attribute size, I try to list this attribute size in the product description, but addAttributeToSort() not working. My code is:

if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);

    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addAttributeToSort('size','ASC')->addFilterByRequiredOptions();

right now I have something like this:

100g
10g
25g

enter image description here

If I use DESC instead ASC I have this sorting:

25g
10g
100g

The entire code:

if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);

    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addAttributeToSort('size','ASC')->addFilterByRequiredOptions();
    foreach($simple_collection as $simple_product){
        echo $simple_product->getAttributeText('size');
    }
endif;

enter image description here

5
  • this is right sorting, because its a string sorting, because in your dropdown attribute you have set values with g so it takes string instead of integer Commented Jan 11, 2017 at 13:55
  • thank you but how I can change this to have the correct sorting?
    – Robert
    Commented Jan 11, 2017 at 14:02
  • go to catalog->manage attribute and search for size and change the dropdown option value Commented Jan 11, 2017 at 14:05
  • please take a look I add an image to see the size attribute
    – Robert
    Commented Jan 11, 2017 at 14:20
  • remove g from name and your sort will work fine on frontend Commented Jan 11, 2017 at 14:56

3 Answers 3

2

I assume that size is a dropdown attribute.
I'm not sure exactly how the sorting works but it can be one of the 2 cases.

Case 1.
The sorting is done via values of that attribute. And the actual values stored in the database for are integers. They are the ids of the options.
So most probably the id of the option 100g is smaller than the id of the option 10g and the sorting is done for these ids.

Case 2.
The sorting is done somehow by using the labels of the options.
In this case, what you see in the result is perfectly normal since the comparison is done for strings and the string 100g is "smaller" than the string 10g.

Either way, I propose you a solution that works in both cases.
Since you need only to display the labels somewhere, why not just collect the labels as you do now, add them to an array and then sort that array as you want.
Again, you might hit the problem that the array elements will be sorted as strings, but you can overcome this by removing g from the end of each element, sorting then adding g back.

Here is the code you can use.
It works for kg and g at the same time and it sorts them properly.
I mean 1500g comes between 1kg and 2kg.

if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);

    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addAttributeToSort('size','ASC')->addFilterByRequiredOptions();
    $toSort = array();
    foreach($simple_collection as $simple_product) {
        $unit = trim($simple_product->getAttributeText('size'));
        //check of it's kg unit
        if (substr($unit, -2) == 'kg') {
            $toSort[] = array(
                'sort_value' => (float)str_replace('kg', '', $unit) * 1000, 
                'real_value' => $unit,
                'product' => $simple_product
            );
        } else {
            //it's gram unit
            $toSort[] = array(
                'sort_value' => (float)str_replace('g', '', $unit), 
                'real_value' => $unit,
                'product' => $simple_product
            );
        }
    }
    usort($toSort, function($a, $b){
        if ($a['sort_value'] == $b['sort_value']) {
            return 0;
        }
        return ($a['sort_value'] < $b['sort_value']) ? -1 : 1;
    });

    foreach ($toSort as $item) {
        /** @var Mage_Catalog_Model_Product $product */
        $product = $item['product'];
        //use getFinalPrice instead of price. Maybe there are discount rules applied to it.
        echo $item['real_value'].': '.Mage::helper('core')->currency($product->getFinalPrice()).'<br />';
    }
endif;
24
  • Hi Marius thank you, can you tell me please or show me an example how I can do this with my code?
    – Robert
    Commented Jan 11, 2017 at 14:01
  • aaa now I understand what you say, to remove that g letter from size attribute, hmm is not a good idea, I can't do this, that must be there because I have kg too
    – Robert
    Commented Jan 11, 2017 at 14:06
  • maybe you can transform everything to grams by multiplying with 1000 the values that have kg. Then transform them back after sorting. Not sure about the way to go.
    – Marius
    Commented Jan 11, 2017 at 14:08
  • yes I know, is the only way? this can be done only in this way?
    – Robert
    Commented Jan 11, 2017 at 14:15
  • I didn't claim this is the only way to do it, but I don't know any other way. And it's going to be complicated to sort them if you have kg and g at the same time. Maybe split them into 2 arrays one for g and one for kg, sort each one and merge them again
    – Marius
    Commented Jan 11, 2017 at 14:16
0
 if($_product->getTypeId() == "configurable"):
        $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);

    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

    $simple_collection->setOrder('size', 'ASC');
3
  • what does sku have to do with what the OP is asking?
    – Marius
    Commented Jan 11, 2017 at 13:56
  • Hi, I don't understand what you do here, but is the same thing like in my code.
    – Robert
    Commented Jan 11, 2017 at 14:02
  • your code is not work, I have tried addSort/setSort and also unshiftSort and none of them change anything.
    – Robert
    Commented Jan 11, 2017 at 14:17
0

this is right sorting, because its a string sorting, because in your dropdown attribute you have set values with gso it sort as string instead of integer

go to catalog->manage attribute and search for size and change the dropdown option value

remove g from all options and your sort will work fine.

1
  • if you take a look at my discussion with Marius, I just tell him the same thing, I remove the g and is the same, please take a look I add my entire code here at the very bottom of my post.
    – Robert
    Commented Jan 11, 2017 at 15:00

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.