2

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

4

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

3

Instead of :

$order->getBillingAddress()->getCountryId()

you can do

Mage::getModel('directory/country')->loadByCode($order->getBillingAddress()->getCountryId())->getName()
0

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.