0

How can I get order id from purchased downlodable product ?

Please check my below code :

$purchased = Mage::getResourceModel('downloadable/link_purchased_collection')
        ->addFieldToFilter('customer_id', $customer_id)
        ->addOrder('created_at', 'desc');

$purchasedItems = Mage::getResourceModel('downloadable/link_purchased_item_collection')
        ->addFieldToFilter('purchased_id', array('in' => $purchasedIds))
        ->addFieldToFilter('status',
            array(
                'nin' => array(
                    Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING_PAYMENT,
                    Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PAYMENT_REVIEW
                )
            )
        )
        ->setOrder('item_id', 'desc');


foreach ($items as $item)
{ 
    -- Here I wand to get Order id of each items ---
}
0

This is not the best solution because it involves having a load in a loop, but it's better than nothing.

foreach ($items as $item)
{ 
    $orderItemId = $item->getOrderItemId();
    $orderItem = Mage::getModel('sales/order_item')->load($orderItemId);
    $orderId = $orderItem->getOrderId();//this is what you need
}
2

I think simple query easily works for you

    $readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
    $sfoi = Mage::getSingleton("core/resource")->getTableName('sales_flat_order_item');
    foreach ($items as $item)
    { 
        $itemId=$item->getId();
        $query="SELECT order_id FROM $sfoi WHERE item_id=$itemId";
        $results = $readConnection->fetchAll($query);
        $orderId=$results[0]['order_id'];    
    }

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.