0

I found this code below to export order data from magento, but I also need the order detail information for each order as well. All I really need added to this is the Items Ordered with Product name & SKU. I'm fairly new to Magento and PHP so any help would be helpful.

<?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();
$fp = fopen('export.csv', 'w');

$header = array("Customer ID", "First Name", "Last Name", "Email", "City",  "Telephone", "Country", "Postcode", "Status");
fputcsv($fp, $header);

foreach($orders as $order) {
    $billingAddress = $order->getBillingAddress();
    $countryCode = $billingAddress->getCountryId();
    $country = Mage::getModel('directory/country')->loadByCode($countryCode);
    $countryName = $country->getName();

    $fields = array($order->getId(), $billingAddress->getFirstname(),$billingAddress->getLastname(), $billingAddress->getEmail(), $billingAddress->getCity(),  $billingAddress->getTelephone(), $countryName, $billingAddress->getPostcode(), $order->getStatus());
    fputcsv($fp, $fields);
}?>

After some more research, I was able to complete the script to tailor to my needs. But due to my lack of knowledge in PHP I'm unable to get the code to collect all the custom options for each order. It looks to be displaying just the last selection from the custom options instead of all displaying all the selections. Any help?

<?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();
$fp = fopen('export.csv', 'w');

$header = array("Customer ID", "First Name", "Last Name", "Email", "City",  "Telephone", "Country", "Postcode","Product", "SKU", "Options", "Status");
fputcsv($fp, $header);

foreach($orders as $order) {
    $block = Mage::app()->getLayout()->createBlock('sales/order_item_renderer_default');
    $billingAddress = $order->getBillingAddress();
    $countryCode = $billingAddress->getCountryId();
    $country = Mage::getModel('directory/country')->loadByCode($countryCode);
    $countryName = $country->getName();
    $items = $order->getAllVisibleItems();
        foreach($items as $item){
            $productName = $item->getName();
            $sku = $item->getSku();
            $block->setItem($item);
            $_options = $block->getItemOptions();
            foreach ($_options as $option) {
                $str = $option['label'] . ' : ' . $option['value'];
            }
        }

    $fields = array($order->getId(), $billingAddress->getFirstname(),$billingAddress->getLastname(), $billingAddress->getEmail(), $billingAddress->getCity(),  $billingAddress->getTelephone(), $countryName, $billingAddress->getPostcode(), $item->getName(), $item->getSku(), $str, $order->getStatus());
    fputcsv($fp, $fields);
}?>
0

you can try below 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('status', Mage_Sales_Model_Order::STATE_COMPLETE);
        $fp = fopen('export.csv', 'w');

        $header = array("Customer ID", "First Name", "Last Name", "Email", "City",  "Telephone", "Country", "Postcode", "Status");
        fputcsv($fp, $header);

        foreach($orders as $order) {
            $billingAddress = $order->getBillingAddress();
            $countryCode = $billingAddress->getCountryId();
            $country = Mage::getModel('directory/country')->loadByCode($countryCode);
            $countryName = $country->getName();

            $fields = array($order->getId(), $billingAddress->getFirstname(),$billingAddress->getLastname(), $billingAddress->getEmail(), $billingAddress->getCity(),  $billingAddress->getTelephone(), $countryName, $billingAddress->getPostcode(), $order->getStatus());
            fputcsv($fp, $fields);
        }
    ?>
  • Thanks for the reply, but this is the code that I started out with. – Randy Jan 12 at 21:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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