I have successfully got the wishlist collection of customer by referring the below link:


Now, I have to create the wishlist collection as a CSV file. Since, I'm new to this, I have no idea where to start.

Could anyone please help me on this?

2 Answers 2


If you got all wishlist collection, and create you CSV, you have to do similar something like this below, also I've did this to get product collection in a CSV, you have to customize this code according to your collection data:

$_filename = 'WishlistCollection.csv';
$file = fopen(YOUR_FILE_PATH . '/' . $_filename, 'w');
fputcsv($file, self::HEADINGS, ",", '"');

foreach ($collection as $_wishllist) {
    fputcsv($file, array(   
    ), ",", '"');
  • Thanks. Glad to know that you have got the product collection as a CSV. My query is that on after I collect the wishlist data, where I have to customize in order to get a CSV file?
    – Mohammad
    Commented Jan 3, 2020 at 10:51
  • please paste you whole code. Commented Jan 3, 2020 at 10:53

Below script will help you out to directly download the wishlist details within csv for respective customer.

$csvGenerate = array();
$csvGenerate[] = array("Product Name","Product ID");

$customerId = 'respective_customer_id'; //CUSTOMER_ID
$wishlistCollection = $block->getWishlistByCustomerId($customerId);
if(count($wishlistCollection)) {
    foreach ($wishlistCollection as $_item) {
      $temp1 = array();
      $temp1[] = $_item->getProduct()->getName();
      $temp1[] = $_item->getProduct()->getId();
      $csvGenerate[] = $temp1;
} else {
  echo __("No Item found in your wishlist");

function array2csv(array &$array)
   if (count($array) == 0) {
     return null;
   $df = fopen("php://output", 'w');

   foreach ($array as $row) {
      fputcsv($df, $row);
   return ob_get_clean();

function download_send_headers($filename) {
    // disable caching
    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");

    // force download  
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // disposition / encoding on response body
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");

download_send_headers("wishlist_details" . date("Y-m-d") . ".csv");
echo array2csv($csvGenerate);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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