0

I'm working within a product collection returning products and trying to order them. The problem is one of my product attributes (I find this out at 90% of the way through my project) is a quantity, i.e. 250, 5000 etc. However, I've just found out that despite these being numbers Magento treats them as strings, so therefore the collection returns the following quantities in this example:

50,100,250,500,1000,2000,5000

However, addAttributeToSort('quantity','ASC'); does this:

100,1000,2000,250,50,500,5000

I've done a var_dump() on the collection and ascertained that the values are being treated as strings, hence why this is probably happening. Unfortunately I've got over 6000 products with a lot of custom implementations and configurable products depending on this attribute, so am reluctant to change it. Searching on here I found that adding ORDER BY 'quantity' *1 does actually perform the sort correctly, however I can't seem to implement this clause in the standard addAttributeToSort function.

If anyone could help me implement this, I've tried addAttributeToSort('quantity','*1'); but that doesn't work, just errors.

Many thanks

UPDATE:

Here's the syntax for the query which is generated from the following code:

$collection = $this->getUsedProductCollection($product)
                   ->addAttributeToSelect('*')
                   ->addFieldToFilter('name', array( 'like' => '%' . $stock . '%' ));
$collection->getSelect()->order(new Zend_Db_Expr('quantity' *1)); 
count($collection);

'SELECT 'e'.*, 'link_table'.'parent_id', IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS 'name', 'price_index'.'price', 'price_index'.'tax_class_id', 'price_index'.'final_price', IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS 'minimal_price', 'price_index'.'min_price', 'price_index'.'max_price', 'price_index'.'tier_price' FROM 'catalog_product_entity' AS 'e' INNER JOIN 'catalog_product_super_link' AS 'link_table' ON link_table.product_id = e.entity_id INNER JOIN 'catalog_product_website' AS 'product_website' ON product_website.product_id = e.entity_id AND product_website.website_id = '1' INNER JOIN 'catalog_product_entity_varchar' AS 'at_name_default' ON ('at_name_default'.'entity_id' = 'e'.'entity_id') AND ('at_name_default'.'attribute_id' = '65') AND 'at_name_default'.'store_id' = 0 LEFT JOIN 'catalog_product_entity_varchar' AS 'at_name' ON ('at_name'.'entity_id' = 'e'.'entity_id') AND ('at_name'.'attribute_id' = '65') AND ('at_name'.'store_id' = 1) INNER JOIN 'catalog_product_index_price' AS 'price_index' ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 WHERE (link_table.parent_id = 3781) AND (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%PCL Labels%')'

2 Answers 2

1

try $collection->getSelect()->order(new Zend_Db_Expr('quantity' *1));

7
  • My current query is this, how would I implement the above code within this? $collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')->addFieldToFilter('name', array( 'like' => '%' . $stock . '%' ))->addAttributetoSort('quantity','ASC');
    – fRAiLtY-
    Sep 21, 2012 at 9:32
  • just add ->getSelect()->order(new Zend_Db_Expr('quantity' *1)) to the end of your magento collection expression. of course, remove addattributetosort
    – srgb
    Sep 21, 2012 at 9:33
  • or $collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')->addFieldToFilter('name', array( 'like' => '%' . $stock . '%' )); $collection->getSelect()->order(new Zend_Db_Expr('quantity' *1));
    – srgb
    Sep 21, 2012 at 9:35
  • Note there was a double quotation mark at the end of the query, I removed it now
    – srgb
    Sep 21, 2012 at 9:37
  • My query is now $collection = $this->getUsedProductCollection($product)->addAttributeToSelect('*')->addFieldToFilter('name', array( 'like' => '%' . $stock . '%' ))->getSelect()->order(new Zend_Db_Expr('quantity' *1)); which doesn't "error" as such, but doesn't return any results it appears. Is there a way of echo'ing the select? I conventionally use echo $collection->getSelect(); but since adding your line of code it doesn't recognise this method, I guess as it's already used in the line of code you suggested?
    – fRAiLtY-
    Sep 21, 2012 at 9:46
0

In the end I achieved this in PHP via ksort(). The database model when implementing any Zend functions was being overridden somewhere and I couldn't afford the time to figure it out.

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.