2

I'm loading a collection of order items like this:

    $collection = Mage::getResourceModel('sales/order_item_collection')
        ->addAttributeToSelect('*');

And I'd like to join to add the order's status to the collection, so that I can filter out incomplete orders via addFieldToFilter('status', 'complete').

How do I join these two things so that I can filter on the parent order's status?

I would paste my various attempts but I think that would probably just add confusion.

6

You can use the join method on the getSelect object

$collection = Mage::getResourceModel('sales/order_item_collection')
        ->addAttributeToSelect('*');

$collection->getSelect()->join( array('orders'=> sales_flat_order), 'orders.order_id = main_table.order_id', array('orders.status')); // join on orderid and retrieve the status column

Small side note, it's better to specifically state which fields you need instead of using addAttributeToSelect('*')

1

hello try like this...

$order_collection = Mage::getModel('sales/order')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status','Complete' );
  • I'm sorry, that isn't actually what I'm looking for. You've loaded the sales/order collection, when what I need is the sales/order_item collection. – skyler Mar 17 '14 at 16:26
0

Try to following code. This adds to order customer id, email address and order status to your order item collection.

$collection = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('*')
;

$collection
    ->getSelect()
    ->join(
        array('orders'=> 'sales_flat_order'),
        'orders.entity_id=main_table.order_id', 
        array(
            'orders.customer_email',
            'orders.customer_id',
            'orders.status as order_status'
        )
    )
;

You can read the additional fields like you normally would.

foreach ( $collection as $orderItem ) {
    $status = $orderItem->getOrderStatus();    
}

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.