0

I'm using this code to export customers to CSV.

I would like to add an extra column that is the CUSTOMER GROUP. Currently, I added "group_id" and it works but only shows the ID (a number). I would like to show the text.

For this you can create your php script, create one php file on root directory of your website and use following code.for ex: if file name is exportCustomer.php and your url is like http://magentowebsite.com then after placing this file in root you need to visit url like http://magentowebsite.com/exportCustomer.php then you will get a csv in your root directory with name as customers.csv

<?php
require_once('app/Mage.php');
umask(0);
if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}
// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL);
try {
    Mage::getConfig()->init();
    Mage::app();   
} catch (Exception $e) {
    Mage::printException($e);
}
ini_set('memory_limit','500M');
$customerCount = 0;
try{
    //configure the collection filters.
    $collection = Mage::getResourceModel('customer/customer_collection')
    ->addAttributeToSelect('firstname')
    ->addAttributeToSelect('lastname')
    ->addAttributeToSelect('email')
    ->addAttributeToSelect('store_id');
    //Add a page size to the result set.
    $collection->setPageSize(50);
    //discover how many page the result will be.
    $pages = $collection->getLastPageNumber();
    $currentPage = 1;
    //This is the file to append the output to.
    $fp = fopen('customers.csv', 'w');
    $addedKeys = false;
    do{
         //Tell the collection which page to load.
         $collection->setCurPage($currentPage);
         $collection->load();
         foreach ($collection as $customer){
            //write the collection array as a CSV.
            $customerArray = $customer->toArray();
            $customerREquiredArray['First name'] = $customerArray['firstname'];
            $customerREquiredArray['Last name'] = $customerArray['lastname'];
            $customerREquiredArray['Email'] = $customerArray['email'];
            $customerREquiredArray['Store ID'] = $customerArray['store_id'];
            if($addedKeys == false){
                $keys = array_keys($customerREquiredArray);
                fputcsv($fp, $keys);
                $addedKeys = true;
            }
            //var_dump($customerArray); echo "\n\n";
            fputcsv($fp, $customerREquiredArray);
            //fwrite($fp,print_r($customerArray,true) . chr(10) );
            $customerCount++;
         }
         $currentPage++;
         //make the collection unload the data in memory so it will pick up the next page when load() is called.
         $collection->clear();
         //break; //DEBUG
         echo "Finished page $currentPage of $pages \n"; 
    } while ($currentPage <= $pages);
    fclose($fp);
} catch (Exception $e) {
    //$response['error'] = $e->getMessage();
    Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
1

You need to add a custom Join to your collection:

$collection->getSelect()->join(array('cg' => 'customer_group'),
    'e.group_id = cg.customer_group_id',
    array('customer_group_code'));

The final version of your export file will be then:

<?php
require_once('app/Mage.php');
umask(0);
if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}
// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL);
try {
    Mage::getConfig()->init();
    Mage::app();
} catch (Exception $e) {
    Mage::printException($e);
}
ini_set('memory_limit','500M');
$customerCount = 0;
try{
    //configure the collection filters.
    $collection = Mage::getResourceModel('customer/customer_collection')
        ->addAttributeToSelect('firstname')
        ->addAttributeToSelect('lastname')
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('store_id');

    $collection->getSelect()->join(array('cg' => 'customer_group'),
        'e.group_id = cg.customer_group_id',
        array('customer_group_code'));


    //Add a page size to the result set.
    $collection->setPageSize(50);
    //discover how many page the result will be.
    $pages = $collection->getLastPageNumber();
    $currentPage = 1;
    //This is the file to append the output to.
    $fp = fopen('customers.csv', 'w');
    $addedKeys = false;
    do{
        //Tell the collection which page to load.
        $collection->setCurPage($currentPage);
        $collection->load();
        foreach ($collection as $customer){
            //write the collection array as a CSV.
            $customerArray = $customer->toArray();
            $customerREquiredArray['First name'] = $customerArray['firstname'];
            $customerREquiredArray['Last name'] = $customerArray['lastname'];
            $customerREquiredArray['Email'] = $customerArray['email'];
            $customerREquiredArray['Store ID'] = $customerArray['store_id'];
            $customerREquiredArray['Customer Group'] = $customerArray['customer_group_code'];
            if($addedKeys == false){
                $keys = array_keys($customerREquiredArray);
                fputcsv($fp, $keys);
                $addedKeys = true;
            }
            //var_dump($customerArray); echo "\n\n";
            fputcsv($fp, $customerREquiredArray);
            //fwrite($fp,print_r($customerArray,true) . chr(10) );
            $customerCount++;
        }
        $currentPage++;
        //make the collection unload the data in memory so it will pick up the next page when load() is called.
        $collection->clear();
        //break; //DEBUG
        echo "Finished page $currentPage of $pages \n";
    } while ($currentPage <= $pages);
    fclose($fp);
} catch (Exception $e) {
    //$response['error'] = $e->getMessage();
    Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
  • I would like to add an extra column that is the STORE. Currently I can get the store id with ->addAttributeToSelect('store_id') but what I want is not the ID, but the label, the name I set up. – Luca Frank Guarini Feb 18 '17 at 17:07
  • In this case you have to add an additional JOIN for the table core_store – Enzo Perrotta Feb 21 '17 at 12:02
0

You need to add the folowing code line:

$customerREquiredArray['CUSTOMER GROUP']= Mage::getModel('customer/group')->load($customerArray['group_id'])->getCustomerGroupCode();

This line of code will add the CUSTOMER GROUP column to the csv file, make sure to place it in line #47 in the foreach loop.

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.