使用可配置产品编辑订单后,通过increment_id在Magento中检索单个订单

发布于 2024-11-03 21:14:22 字数 1050 浏览 1 评论 0原文

我在一家主要销售可配置产品的 Magento 商店中遇到了一个有趣的问题。我有一些代码可以打包订单信息并将其发送到外部仓库,然后由外部仓库履行订单。对于此请求,我使用 increment_id 作为标识符。我们定期询问仓库订单是否已发货,如果他们报告已发货,我们会通过 increment_id 在 Magento 中检索订单,并通过创建新的发货来完成订单。

现在,在编辑订单之前,这一切都可以正常工作。由于 Magento 对可配置产品的处理,编辑订单涉及取消当前订单并使用相同的 increment_id 重新创建订单。随后,我基于 increment_id 的交互现在失败了,因为当我执行此类操作时,似乎只检索到第一个(已取消的)订单,因为 increment_id 没有更长的唯一性:

<?php
$order = Mage::getModel('sales/order')->loadByIncrementId(100000001);
?>

通常,不可能有重复的 increment_id,所以我希望 Magento 优先考虑“活动”顺序,但事实并非如此。

现在,我找到了一种使用集合来解决此问题的方法:

<?php
$orders = Mage::getModel('sales/order')
  ->getCollection()
  ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
  ->addAttributeToFilter('increment_id', 100000001);

foreach ($orders as $order) {
  // Do what we need with the order
}
?>

但是,我很想知道是否有一种方法可以从集合中仅获取一条记录,从而无需使用 foreach 。这可以做到吗?

I've come across an interesting problem with a Magento store that deals primarily in selling configurable products. I have some code that packages up order information and sends it to an external warehouse who then fulfill the order. With this request, I'm using the increment_id as the identifier. Periodically, we ask the warehouse if the order has shipped and if they report it has, we retrieve the order in Magento via the increment_id and complete the order by creating a new shipment against it.

Now, this works fine until an order is edited. Due to Magento's handling of configurable products, editing an order involves cancelling the current order and recreating it anew with the same increment_id. Subsequently, my interactions based on the increment_id are now scuppered, since only the first (cancelled) order appears to be retrieved when I do something like this, as the increment_id is no longer unique:

<?php
$order = Mage::getModel('sales/order')->loadByIncrementId(100000001);
?>

Ordinarily, having a duplicate increment_id is not possible, so I would have expected Magento to give preference to the 'active' order, but this is not the case.

Now, I found a way to get around this with a collection:

<?php
$orders = Mage::getModel('sales/order')
  ->getCollection()
  ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
  ->addAttributeToFilter('increment_id', 100000001);

foreach ($orders as $order) {
  // Do what we need with the order
}
?>

However, I'd be interested to know if there's a way to get just one record back from the collection, negating the need for the foreach. Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡墨 2024-11-10 21:14:22

使用getFirstItem()

$orders = Mage::getModel('sales/order')
  ->getCollection()
  ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
  ->addAttributeToFilter('increment_id', 100000001)
  ->getFirstItem();

Use getFirstItem()

$orders = Mage::getModel('sales/order')
  ->getCollection()
  ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
  ->addAttributeToFilter('increment_id', 100000001)
  ->getFirstItem();
趁微风不噪 2024-11-10 21:14:22
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
失退 2024-11-10 21:14:22

虽然 Mukesh 的答案在技术上可行,但如果请求返回一个大型数据集,则在加载整个集合时可能会超时,这就是 getFirstItem() 函数第一行发生的情况。这里概述了这一点:避免 Magento 的 getFirstItem() 的 3 个原因

While the answer from Mukesh will technically work, if you have a large dataset returned by a request, it will potentially times out when loading the entire collection, which is what happens on the very first line of the getFirstItem() function. This is outlined here: Top 3 Reasons to Avoid Magento's getFirstItem()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文