php 在 Magento 中从订单中获取产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18375441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting products from an order in Magento
提问by user1597438
In my magento project, under My Account > My Orders (logged on customer), I am able to view the order details along with the products I ordered. Now, for each of the ordered product, I'd like to retrieve a specific attribute however, from my understanding, the code snippet at the beginning of sales/order/items/renderer/default.phtml
which is $_item = $this->getItem();
is the order itself so if I use something like $_item->getId()
, I'm getting the order id and not the product's.
在我的 magento 项目中,在我的帐户 > 我的订单(登录客户)下,我可以查看订单详细信息以及我订购的产品。现在,每个订购的产品,我想不过来检索特定属性,从我的理解,一开始的代码片段sales/order/items/renderer/default.phtml
是$_item = $this->getItem();
是,如果我使用类似的命令本身如此$_item->getId()
,我要出货id 而不是产品的。
I tried researching and ended up with this code:
我尝试研究并最终得到以下代码:
$orders = Mage::getModel('sales/order')->load($_item->getId());
foreach($orders as $order):
$is = $order->getAllItems();
foreach($is as $i):
echo $i->getProductId();
endforeach;
endforeach;
Hoping I could use the product id to get the other attributes of the said product however, I 'm getting an error with this code with no way of telling what the error is. I've also tried something like this:
希望我可以使用产品 id 来获取所述产品的其他属性,但是,我收到此代码的错误,无法说明错误是什么。我也试过这样的事情:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('name', $name);
foreach($_productCollection as $_product):
$_temp = $_product->getResource()->getAttribute('name_en')->getFrontend()->getValue($_product);
endforeach;
But I keep getting 0 when I try to check the count of items in the product collection. How can I retrieve a custom attribute for the product in this page?
但是当我尝试检查产品集合中的项目数时,我一直得到 0。如何在此页面中检索产品的自定义属性?
回答by Seth Malaki
This should work better:
这应该工作得更好:
$orderIncrementId = '100000010';
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$items = $order->getAllVisibleItems();
foreach($items as $i):
echo $i->getProductId();
endforeach;
Take a look here for more info: http://www.magentocommerce.com/boards/viewthread/18629/
在这里查看更多信息:http: //www.magentocommerce.com/boards/viewthread/18629/
回答by user487772
While the answer given by Electric Jesuswill work it contains a potential performance issue of loading products in the loop.
虽然Electric Jesus给出的答案会起作用,但它包含在循环中加载产品的潜在性能问题。
The correct implementation would be first to get IDs of ordered products and then load all of them at once. Assuming you already have your order loaded:
正确的实现是首先获取订购产品的 ID,然后一次性加载所有产品。假设您已经加载了订单:
$orderedItems = $order->getAllVisibleItems();
$orderedProductIds = [];
foreach ($orderedItems as $item) {
$orderedProductIds[] = $item->getData('product_id');
}
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addIdFilter($orderedProductIds);
回答by julius yusdianto
$_orders = $this->getOrders();
$orderitems = $_order->getAllVisibleItems();
foreach ($_orders as $_order):
$orderitems = $_order->getAllVisibleItems();
foreach ($orderitems as $orderitem):
$product = $orderitem->getProduct();
echo $product->getId();
endforeach;
endforeach;
I have the same problem and this is my solution
我有同样的问题,这是我的解决方案