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?
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.
Magento 2 can easily handle more than 1 million products.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With