2

How can I get all customers orders and store it on an array ?

I've got this query, is it possible to get all the products ?

$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $customer->getId())
->addAttributeToSelect('created_at')
->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
->load();

I really need help please!

user3497472
  • 193
  • 1
  • 11

1 Answers1

1
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addFieldToSelect(array('name', 'qty_ordered'))
->addFieldToFilter('customer_id', $customer->getId())
->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
->toArray();

what you then want is $orderItems['items']

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
  • When I added that and "echo $orders['items']" it returned this "array(1) { [0]=> array(1) { ["created_at"]=> string(19) "2014-05-20 14:47:58" } } " – user3497472 May 21 '14 at 15:23
  • interessing, maybe try addFieldToSelect('name')->addFieldToSelect('qty_ordered') – Fabian Blechschmidt May 21 '14 at 15:28
  • I had a mistake with my code, now with your first answer and second one I get There has been an error processing your request

    Exception printing is disabled by default for security reasons.

    Error log record number: 274534525038

    – user3497472 May 21 '14 at 15:34
  • than take this great debugging post by sonassi and debug: http://magento.stackexchange.com/a/429/217 – Fabian Blechschmidt May 21 '14 at 15:35
  • I already tried to debug but no success. What I've found was if I remove "->toArray()" the error doesn't appear. – user3497472 May 21 '14 at 15:38
  • did you read the post? – Fabian Blechschmidt May 21 '14 at 15:50
  • Yes and I do all the steps and then I don't know what to do. I'm used to eclipse debug mode, and don't know how this one works – user3497472 May 21 '14 at 15:51
  • if you turned on the developer mode in magento you should get a real error message not the printing is disabled nonsense. And the error is in the var/report/<errornumber> file – Fabian Blechschmidt May 21 '14 at 15:58
  • I've done those 2 steps "ini_set('display_errors', 1);" and "if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE']) || true) { Mage::setIsDeveloperMode(true); }" and enabled debug profiler in magento admin and it still gives me that error – user3497472 May 21 '14 at 16:08
  • Have a look into /var/reports/274534525038 – Fabian Blechschmidt May 21 '14 at 16:09
  • This is what I get http://pastebin.com/c3EQ6aPh – user3497472 May 21 '14 at 16:12
  • ah of course. The table sales_flat_order_item has no column customer_id. You need to join into sales_flat_order before. Checkout \Mage_Core_Model_Resource_Db_Collection_Abstract::join and use the method on the collection. – Fabian Blechschmidt May 21 '14 at 16:17
  • And how can I do that ? – user3497472 May 21 '14 at 16:26
  • 1
    $orderItems = Mage::getResourceModel('sales/order_item_collection') ->join(array('order' => 'sales/order'), 'main_table.order_id = order.entity_id', 'order.entity_id') ->addFieldToSelect(array('name', 'qty_ordered')) ->addFieldToFilter('customer_id', $customer->getId()) ->toArray(); should add the join correctly – David Manners Jul 24 '14 at 07:31