Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting products from an order in Magento

Tags:

php

magento

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.

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:

 $_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?

like image 304
user1597438 Avatar asked Aug 22 '13 08:08

user1597438


People also ask

How can I get order details in Magento?

Order details contains the order information like Total summary, Customer Information, Billing, shipping and payment-related data in Magento 2. Using Dependency Injection (DI), You can use inject \Magento\Sales\Api\OrderRepositoryInterface class to get order details using order id.

How many orders can Magento handle?

Magento 2 can easily handle more than 1 million products.


1 Answers

While the answer given by Electric Jesus will work it contains a potential performance issue of loading products in the loop.

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:

$orderedItems = $order->getAllVisibleItems();
$orderedProductIds = [];

foreach ($orderedItems as $item) {
    $orderedProductIds[] = $item->getData('product_id');
}

$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addIdFilter($orderedProductIds);
like image 82
user487772 Avatar answered Oct 11 '22 13:10

user487772