0

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

https://www.rakeshjesadiya.com/get-wishlist-collection-of-customer-by-customer-id-magento-2/

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?

0

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(   
        $_wishllist->getData1(),
        $_wishllist->getData2(),
        $_wishllist->getData3()
    ), ",", '"');
}
fclose($file);
| improve this answer | |
  • 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 Jan 3 at 10:51
  • please paste you whole code. – Partab Saifuddin Zakir Jan 3 at 10:53
0

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;
   }
   ob_start();
   $df = fopen("php://output", 'w');

   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   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);
die();
| improve this answer | |

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.