Advertisement
Digitalraindrops

WP Save CSV in Uploads

Jul 15th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Create the Catalogue Code File
  2. function sp_create_catalogues() {
  3.  
  4.     $terms = get_terms( 'product_cat',
  5.         array(
  6.             'parent'    => 0,
  7.             'hide_empty' => 1
  8.         )
  9.     );
  10.     $count = count($terms);
  11.     if ($count == 0) return;
  12.        
  13.     $file_options = get_option('sp_file'); 
  14.     $shortname = isset( $file_options['catalogues'] ) && !is_null( $file_options['catalogues'] ) ? $file_options['catalogues'] : 'Catalogues.csv'; 
  15.     $filename = SP_FILE_PATH .$shortname;
  16.      
  17.     $data = array();
  18.     $data[] = 'Code';
  19.     $data[] = 'Description';
  20.    
  21.     $fh = @fopen( $filename, 'w' );
  22.    
  23.     fputcsv($fh, $data);
  24.        
  25.     foreach ( $terms as $term ) {
  26.         // Put the data into the stream
  27.         $data = array();
  28.         $data[] = $term->slug;
  29.         $data[] = html_entity_decode($term->name);      
  30.         fputcsv($fh, $data);
  31.        
  32.         $children = get_terms( 'product_cat',
  33.             array(
  34.                 'parent'    => $term->term_id,
  35.                 'hide_empty' => 1
  36.             )
  37.         );
  38.  
  39.         $count = count($children);
  40.         if ($count > 0) {
  41.             foreach ( $children as $child ) {
  42.                 $data = array();
  43.                 $data[] = $child->slug;
  44.                 $data[] = '> ' .html_entity_decode($child->name);
  45.                 fputcsv($fh, $data);
  46.             }
  47.         }  
  48.     }  
  49.     // Close the file
  50.     fclose($fh);
  51.     $currpage = site_url() .'/wp-admin/tools.php?page=export';
  52.     wp_redirect($currpage);
  53.     // Make sure nothing else is sent, our file is done
  54.     exit;  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement