Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Like Marius said, you don't need a collection for this specific case. If you have to list several order status, then you would use a collection. To make your code more generic (so that you can use it with collections), I would change the interface a little bit:

public function getOneOrderHistoryAction() {
    // extract the parameters from $this->request and pass on
    ...
    return $this->getOrderHistory($oid, $nlm, $eml);
}

public function getOrderHistory($oid, $nlm, $eml) {
    // use the arguments provided
    ...
}    

Also, a small improvement maybe: I think you want strncasecmp.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top