I have following task to do (I have 0 magento experience, and I am told to do this):

Make standalone script that reads every order from Magento, and filter those that have purchased this product id => 297. Then write the customer and order information into a csv file on the server.

I know how to make CVS file, but I don't know how to work with magento. How can I do this?

  • Why does it have to be a stand alone script?
    – espradley
    Sep 18, 2014 at 13:45
  • There is custom backend, that is independent from magento, and this script will be the part of synchronization between two DBs - magento and backend. CVS script will be imported via function from the backend.
    – Sasha
    Sep 18, 2014 at 13:49

1 Answer 1



require_once('app/Mage.php'); //Path to Magento
$productId = 297;

$orders = Mage::getModel('sales/order')->getCollection();
     ->joinInner(array('order_item' => Mage::getSingleton('core/resource')->getTableName('sales/order_item'), 'order_item.order_id = main_table.entity_id' ))
     ->where('product_id=?', $productId)
     ->order('main_table.entity_id DESC');

foreach($orders as $order){
     // create csv file here
     $billingAddress = $order->getBillingAddress()
     $shippingAddress = $order->getShippingAddress()

See Exporting order details magento

  • I am getting this error:Fatal error: Uncaught exception 'Exception' with message 'Item (Mage_Sales_Model_Order) with the same id "12"
    – Sasha
    Sep 18, 2014 at 14:25
  • Add ->group('main_table.entity_id') see update Sep 18, 2014 at 14:35
  • Didn't have time yesterday to thank you :). The code is working :)
    – Sasha
    Sep 19, 2014 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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