Advertisement
Digitalraindrops

File Save

Jul 14th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  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.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  18.     header('Content-Description: File Transfer');  
  19.     header('Content-type: application/octet-stream');
  20.     header('"filename=' .$filename .'"');
  21.     header("Expires: 0");
  22.     header("Pragma: public");
  23.    
  24.     $data = array();
  25.     $data[] = 'Code';
  26.     $data[] = 'Description';
  27.    
  28.     $fh = @fopen( 'php://output', 'w' );
  29.    
  30.     fputcsv($fh, $data);
  31.        
  32.     foreach ( $terms as $term ) {
  33.         // Put the data into the stream
  34.         $data = array();
  35.         $data[] = $term->slug;
  36.         $data[] = html_entity_decode($term->name);      
  37.         fputcsv($fh, $data);
  38.        
  39.         $children = get_terms( 'product_cat',
  40.             array(
  41.                 'parent'    => $term->term_id,
  42.                 'hide_empty' => 1
  43.             )
  44.         );
  45.  
  46.         $count = count($children);
  47.         if ($count > 0) {
  48.             foreach ( $children as $child ) {
  49.                 $data = array();
  50.                 $data[] = $child->slug;
  51.                 $data[] = '- ' .html_entity_decode($child->name);
  52.                 fputcsv($fh, $data);
  53.             }
  54.         }  
  55.     }  
  56.     // Close the file
  57.     fclose($fh);
  58.     // Make sure nothing else is sent, our file is done
  59.     exit;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement