0

I have created a query that would allow me to sort my products based on the quantity they have been ordered. But when I print the query, the quantity ordered is coming in Ascending order. I want to make it work in descending order. The query is as below

$toDate =  date("Y-m-d h:m:s", strtotime('tomorrow') - 1);
$fromDate = date("Y-m-d h:m:s", strtotime("-1 months"));
$this->getSelect()->joinLeft(array('sfoi' => $this
->getTable('sales/order_item')),
 "e.entity_id = sfoi.product_id AND sfoi.created_at BETWEEN 
 '{$fromDate}' AND '{$toDate}'",                 
array('qty_ordered' => 'SUM(sfoi.qty_ordered)'))
            ->group('e.entity_id')                   
            ->order("qty_ordered {$dir}");        
            return $this;

Can anyone kindly let me know how to do this? I just want to change the order of "qty_ordered" From ASC to DESC

0

The query is not executed inside this function, so maybe the order is changed somewhere after. Anyway, you should debug the SQL by adding a log before the return, like:

Mage::log($this->getSelect()->__toString(), null, 'mydebug.log');

so you can check the exact syntax at this point. You should copy the logged string in your SQL editor to check if everything is fine. You can check if $dir == 'DESC': maybe it is not. If the executed query from the log is ok, it means that the select is reordered somewhere after this block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.