Magento 1.8 – Get Order Status from Order ID, Email, and Customer’s Last Name

collection-filteringmagento-1.8resource-model

I'm currently looking for a method from which I can get the order status based on the order id, customer email and customer last name.

I'd written this code so far

public function orderHistoryAction(){
    $req = $this->getRequest();
    $oid = $req->getParam('oid', '');
    $lnm = $req->getParam('lnm', '');
    $eml = $req->getParam('eml', '');

    $order = Mage::getModel('sales/order')->loadByIncrementId($oid);
    if ($order->getId()) {
        if ((strcasecmp($lnm, $order->getCustomerLastname()) == 0) && (strcasecmp($eml, $order->getCustomerEmail()) == 0)) {
            echo $order->getStatusLabel();
        } else {
            echo 'invalid';
        }
    } else {
        echo 'invalid';
    }
}

and is working well for both customer and guest. But this doesn't seems to me the right approach. I supposed to used something like written in this blog or this blog. If anybody could make me correct?

Best Answer

If you want to use collections you can get your order object like this:

$collection = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('increment_id', $oid)
    ->addAttributeToFilter('customer_lastname', $lnm)
    ->addAttributeToFilter('customer_email', $eml);
$order = $collection->getFirstItem();
if ($order->getId()) { 
    echo $order->getStatusLabel();
}
else {
    echo 'invalid';
}

But it's kind of the same thing that you did.
I even thing your approach is faster.
In your case you run a select like this.

SELECT * FROM sales_flat_order WHERE increment_id = '100000005';

In the approach I described you run something like this:

SELECT * FROM sales_flat_order WHERE increment_id = '100000005' AND customer_lastname='DOE' AND customer_email = 'john.doe@example.com';

There is no need for the additional conditions since the increment_id must be unique.