6

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?

5
  • The code looks OK to me.
    – Marius
    Commented Jan 23, 2014 at 11:59
  • @Marius: yes... this code is ok. but is it the right approach? cant I do like as shown in the link? Commented Jan 23, 2014 at 12:01
  • 1
    The code AND the approach look ok to me. As far as the links you mentioned they are useless in your case. They talk about using collections (lists of objects). You don't have any collections in your code and you don't need them.
    – Marius
    Commented Jan 23, 2014 at 12:06
  • @Marius: you right. but does it can be done in that way. I asked to go through those links for this. Commented Jan 23, 2014 at 12:09
  • See my answer in case you want it differently. But I would use your approach also.
    – Marius
    Commented Jan 23, 2014 at 12:17

2 Answers 2

6

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 = '[email protected]';

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

4
  • +1 for the answer. $collection->getFirstItem(); where do it came from? Also, how could i differentiate between addAttributeToFilter and addFieldToFilter? Commented Jan 23, 2014 at 12:22
  • @MohammadFaisal That's a method defined in Varien_Data_Collection, the class that is parent to all collections.
    – Marius
    Commented Jan 23, 2014 at 12:23
  • previously I tried that too but the thing I was missing is $collection->getFirstItem();. Commented Jan 23, 2014 at 12:26
  • So I'm assuming you could make a form and pass the data with GET or POST for the input, correct?
    – sparecycle
    Commented Aug 7, 2014 at 16:01
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.