How to Export Guest Customers In Magento 2?

In this tutorial, you will go through how to easily export guest customers in Magento 2. This trick will be very useful and handy when you want to export guest customers in Magento 2.

There are times when you want to promote your products to a guest customer. You might want to send newsletters or promos to a guest customer to bring them back and buy your products or for marketing campaigns.

But Magento 2 doesn’t provide any way to export guest customer from admin panel. So, we will learn an easy way to export guest customer on your Magento website.

First of all, create a new PHP file named guest-customer.php in a root directory of your Magento installation.

Secondly, add following lines of code in guest-customer.php file and save it.

/* Error reporting */
set_time_limit(0);
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/* Magento's Bootstrap.php path
 * Mage Enabler users may skip these lines
 */
use Magento\Framework\App\Bootstrap;
 
/**
 * If your external file is in root folder
 */
require 'app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$_objectManage = $bootstrap->getObjectManager();

header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="guest-customer.csv"');

$orderCollection = $_objectManage->get('\Magento\Sales\Model\Order')->getCollection()
    ->addFieldToFilter('customer_is_guest', 1)
    ->addFieldToSelect('*');
$fp = fopen('php://output', 'w');
foreach ($orderCollection as $eachorder) {
    fputcsv($fp, array(
        $eachorder->getCustomerFirstname(),
        $eachorder->getCustomerLastname(),
        $eachorder->getCustomerEmail()
    ));
}
fclose($fp);

Finally, go to your favorite browser and type “http://mysite.com/guest-customer.php“.

Guest customer lists will be downloaded in CSV format. You can use this file to import in services like Mailchimp to send your promo newsletter.


I hope you like this post, Please feel free to comment below, suggestion and problems!

You May Also Like

Editorial Staff

About the Author: Editorial Staff

Editorial Staff at the MageBeginners is a team of Magento experts writing about Magento Tutorials, Themes, Extensions, Guides, Tricks and much more...

Leave a Reply

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