Magento – Programmatically Export Sales order customer details to excel format

countriesexportmagento-1magento-1.7orders

I tried to export sales order customer details, its working fine, but i want customer country code(country name) not id. below i mention my full code.

<?php
require_once("app/Mage.php");
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$orders = Mage::getModel('sales/order')->getCollection();

$orders->addAttributeToFilter('grand_total', array('gt' => '0.00'));
$orders->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE);

$fp = fopen('file.csv', 'w');
foreach($orders as $order) {
    $fields = array($order->getId(), $order->getBillingAddress()->getFirstname(),$order->getBillingAddress()->getLastname(), $order->getBillingAddress()->getEmail(), $order->getBillingAddress()->getCity(),  $order->getBillingAddress()->getTelephone(), $order->getBillingAddress()->getCountryId(), $order->getStatus());
    fputcsv($fp, $fields);
}
?>

enter image description here

Best Answer

Finally i got solution. This is my final code:

<?php
  require_once("app/Mage.php");
  Mage::app();
  Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
  $orders = Mage::getModel('sales/order')->getCollection();
  $orders->addAttributeToFilter('grand_total', array('gt' => '0.00'));
  $orders->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE);
  $fp = fopen('file.csv', 'w');
  foreach($orders as $order) {
     $countryCode = $order->getBillingAddress()->getCountryId();
     $country = Mage::getModel('directory/country')->loadByCode($countryCode);
     $countryName = $country->getName();
     $fields = array($order->getId(), $order->getBillingAddress()->getFirstname(),$order->getBillingAddress()->getLastname(), $order->getBillingAddress()->getEmail(), $order->getBillingAddress()->getCity(),  $order->getBillingAddress()->getTelephone(), $countryName, $order->getStatus());
     fputcsv($fp, $fields);
  }
?>

Please try this, Its working for me

Related Topic