Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Get the last order ID for the customers in a collection with a join order table
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 

Repost from http://stackoverflow.com/questions/17914640/get-the-last-order-id-for-the-customers-in-a-collection-with-a-join-order-table

This is what I have but it gets the first order id

/**      
 * @param string, datetime UTC
 * @param string, datetime UTC
 * @return Mage_Customer_Model_Resource_Customer_Collection         
 */
public function filterOrderId($from$to
{
    $this
->addAttributeToSelect(array('first_name''last_name''gender''dob'));
    
$this->joinTable(
        array(
'o' => 'sales/order_grid'), 
        
'customer_id=entity_id'
        array(
            
'order_id'                => 'increment_id',
            
'grand_total'             => 'grand_total'
        
),
        array(                
            
'created_at'              => array('from'=>$from,'to'=>$to),
            
'status'                  => array("nin"=>array('canceled''rejected''closed''complete'))
        ),                     
        
'inner'  
    
);

    
$this->getSelect()->group('o.customer_id');
    
//$this->addExpressionAttributeToSelect('max_id', 'MAX({{entity_id}})', 'entity_id'); //this does not work
    //$this->setOrder('max_id', 'desc');

    
return $this;
}

I think I need to add a subquery to the join, but I do not know how to do it.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top