从订单ID,电子邮件和客户的姓氏中获取订单状态
-
16-10-2019 - |
题
我目前正在寻找一种可以根据该方法获得订单状态的方法 order id
, customer email
和 customer last name
.
到目前为止我写了这个代码
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';
}
}
并且对两者都很好 顾客 和 来宾. 。但这在我看来不是正确的方法。我应该用像写的东西 这个博客 或者 这个博客. 。如果有人可以使我正确?
解决方案
如果您想使用集合,则可以这样获取这样的订单对象:
$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';
}
但这是您所做的同样的事情。
我什至是您的方法更快。
在您的情况下,您可以选择这样的选择。
SELECT * FROM sales_flat_order WHERE increment_id = '100000005';
在我描述的方法中,您运行了这样的事情:
SELECT * FROM sales_flat_order WHERE increment_id = '100000005' AND customer_lastname='DOE' AND customer_email = 'john.doe@example.com';
由于 increment_id
必须是唯一的。
其他提示
就像Marius所说的那样,您不需要此特定情况的收藏。如果您必须列出几个订单状态,则将使用一个集合。为了使您的代码更加通用(以便您可以将其与集合一起使用),我会稍微更改界面:
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
...
}
另外,也许是一个小改进:我想你想要 strncasecmp.