1

I have a Magento store installed on localhost over WAMP server. I'm using Magento 1.7.

I need to do a table whose first column contains all the products from all the orders. I can't manage to get the product names; I found that there is more than one way to get product names from previous orders in Magento, but I'm not sure if I'm coding it properly.

My code for doing so is:

<?php 
    $_orders = $this->getOrders();
    foreach ($_orders as $_order)
    {
        $items = $_order->getAllItems();
        foreach($items as $item) {$name = $item->getName();}
        echo $name;
    }
?>

Any ideas on what is wrong? Maybe I'm missing something or there is a better way to code it?

Any ideas/comments are much appreciated :-)

1

You can use the next variant:

/** @var Mage_Sales_Model_Mysql4_Order_Collection $_orders */
$_orders = Mage::getModel('sales/order')->getCollection(); // replace with yours $this->getOrders();
/** @var Mage_Sales_Model_Order $_order */
foreach ($_orders as $_order) {
    /** @var Mage_Sales_Model_Mysql4_Order_Item_Collection $itemsCollection */
    $itemsCollection = Mage::getModel('sales/order_item')->getCollection();
    $itemsCollection->setOrderFilter($_order);
    $itemsCollection->addFieldToSelect('name');
    $names[$_order->getId()] = $itemsCollection->getColumnValues('name');
}

Where result array keys is order id and value is array of the order item names:

var_dump of result

PS: do not forget to replace the line $_orders = Mage::getModel('sales/order')->getCollection() with yours $this->getOrders()

| improve this answer | |
  • Thanks for your answer. Is there a chance that orders can only be obtained if inside the template/sales/order folder? Because if I use the code there, I get some results, while if using it in dashboard.phtml, under app/design/frontend/default/custom_theme/template/customer/account, I get no results. – LeCodeur Dec 12 '16 at 9:56
  • @LeCodeur The result depends only on $this->getOrders() – Siarhey Uchukhlebau Dec 12 '16 at 10:04
  • @LeCodeur If my answer has helped you, please accept it – Siarhey Uchukhlebau Dec 12 '16 at 17:11
  • I tried your code but it did not work; I found a workaround putting some code in another file, but I'll definetely have to improve it. I'll get back to this. My main problem is that I don't know why $this->getOrders() seems not to work in dashboard.phtml but it does in recent.phtml. – LeCodeur Dec 13 '16 at 11:35
  • @LeCodeur You should look how the getOrder method is implemented in the corresponding block. – Siarhey Uchukhlebau Dec 13 '16 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.