Magento CSV Export All customer with Billing and Shipping Address

Magento CSV Export All customer with Billing and Shipping Address

In this post we will show you how to create Magento CSV Export All customer for Billing and Shipping Address. hear we will show you Export All customer to CSV with Billing and Shipping Address.

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
 
$customers_obj = Mage::getModel('customer/customer')->getCollection();
$customers_obj->addAttributeToSelect('*');
 
$customers_data_array[0] = array(    
    'email',
    'prefix',
	'suffix',
	'created_at',
	'is_active',
    'firstname',
    'middlename',
	'entity_id',
    'lastname',
    'group_id',
    'website_id',
    'store_id',   
    'is_subscribed',
    'customer_billing_firstname',
    'customer_billing_lastname',
    'customer_billing_street_1',
    'customer_billing_street_2',
    'customer_billing_city',
    'customer_billing_region_id',
    'customer_billing_region',
    'customer_billing_postcode',
    'customer_billing_country_id',
    'customer_billing_telephone',
    'customer_shipping_firstname',
    'customer_shipping_lastname',
    'customer_shipping_street_1',
    'customer_shipping_street_2',
    'customer_shipping_city',
    'customer_shipping_region_id',
    'customer_shipping_region',
    'customer_shipping_postcode',
    'customer_shipping_country_id',
    'customer_shipping_telephone'
);
 
$i = 1;
foreach ($customers_obj as $key => $customer_data) {
    $customers_data_array[$i]['entity_id'] = $customer_data->getData('entity_id');
    $customers_data_array[$i]['email'] = $customer_data->getData('email');
    $customers_data_array[$i]['prefix'] = $customer_data->getData('prefix');
    $customers_data_array[$i]['firstname'] = $customer_data->getData('firstname');
    $customers_data_array[$i]['middlename'] = $customer_data->getData('middlename');
    $customers_data_array[$i]['lastname'] = $customer_data->getData('lastname');
    $customers_data_array[$i]['suffix'] = $customer_data->getData('suffix');
    $customers_data_array[$i]['website_id'] = $customer_data->getData('website_id');
    $customers_data_array[$i]['store_id'] = $customer_data->getData('store_id');
    $customers_data_array[$i]['group_id'] = $customer_data->getData('group_id');
    $customers_data_array[$i]['created_at'] = $customer_data->getData('created_at');
    $customers_data_array[$i]['updated_at'] = $customer_data->getData('updated_at');
    $customers_data_array[$i]['is_active'] = $customer_data->getData('is_active');
 
    $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customers_data_array[$i]['email']);
    $subscriberStatus = $subscriber->isSubscribed();
 
    $customers_data_array[$i]['is_subscribed'] = ($subscriberStatus? 1 : 0);
 
    if($customer_data->getDefaultBilling()) {
        $customer_billingAddress = Mage::getModel('customer/address')->load($customer_data->getDefaultBilling());
        if($billingAddress->getId()) {
            $customers_data_array[$i]['customer_billing_firstname'] = $customer_billingAddress->getData('firstname');
            $customers_data_array[$i]['customer_billing_lastname'] = $customer_billingAddress->getData('lastname');
            $customers_data_array[$i]['customer_billing_street_1'] = $customer_billingAddress->getStreet1();
            $customers_data_array[$i]['customer_billing_street_2'] = $customer_billingAddress->getStreet2();
            $customers_data_array[$i]['customer_billing_city'] = $customer_billingAddress->getData('city');
            $customers_data_array[$i]['customer_billing_region_id'] = $customer_billingAddress->getData('region_id');
            $customers_data_array[$i]['customer_billing_region'] = $customer_billingAddress->getData('region');
            $customers_data_array[$i]['customer_billing_postcode'] = $customer_billingAddress->getData('postcode');
            $customers_data_array[$i]['customer_billing_country_id'] = $customer_billingAddress->getData('country_id');
            $customers_data_array[$i]['customer_billing_telephone'] = $customer_billingAddress->getData('telephone');
        }
    }
    if($customer_data->getDefaultShipping()) {
        $customer_shippingAddress = Mage::getModel('customer/address')->load($customer_data->getDefaultShipping());
        if($shippingAddress->getId()) {
            $customers_data_array[$i]['customer_shipping_firstname'] = $customer_shippingAddress->getData('firstname');
            $customers_data_array[$i]['customer_shipping_lastname'] = $customer_shippingAddress->getData('lastname');
            $customers_data_array[$i]['customer_shipping_street_1'] = $customer_shippingAddress->getStreet1();
            $customers_data_array[$i]['customer_shipping_street_2'] = $customer_shippingAddress->getStreet2();
            $customers_data_array[$i]['customer_shipping_city'] = $customer_shippingAddress->getData('city');
            $customers_data_array[$i]['customer_shipping_region_id'] = $customer_shippingAddress->getData('region_id');
            $customers_data_array[$i]['customer_shipping_region'] = $customer_shippingAddress->getData('region');
            $customers_data_array[$i]['customer_shipping_postcode'] = $customer_shippingAddress->getData('postcode');
            $customers_data_array[$i]['customer_shipping_country_id'] = $customer_shippingAddress->getData('country_id');
            $customers_data_array[$i]['customer_shipping_telephone'] = $customer_shippingAddress->getData('telephone');
        }
    }
    $i++;
}
 
$csv_file = fopen('customer_csv_export.csv', 'w');
foreach ($customers_data_array as $customers_data) {
    fputcsv($csv_file, $customers_data);
}
fclose($csv_file);
 
//echo "<pre>";
//var_dump($customers_data_array);
//echo "</pre>";
?>

You also like Get Order Details using Order and customer registration programmatically and Magento2 admin login user detail

See also  This post show code for execute/run query for Direct MySQL query for create, read, update and delete.

Leave a Comment

Your email address will not be published. Required fields are marked *

87  +    =  96

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US