WooCommerce Products Export to CSV via PHP

WooCommerce Products Export to CSV via PHP

<?php

require_once(‘../wp-load.php’);

global $wpdb;

 

// Get 10 most recent product IDs in date descending order.
$query = new WC_Product_Query( array(
‘limit’ => -1 ,
‘status’ => ‘publish’,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’
) );
$products = $query->get_products();

//print_r($products);

// while ( $products->have_posts() ) : $products->the_post();
// echo “<h3>”.get_the_title().”</h3>”;
// echo ‘<div><a href=”‘.get_permalink().'”>’ . woocommerce_get_product_thumbnail().'</a></div>’;
// endwhile;

//creating a mycsv.csv file
$cfile = fopen(‘/www/wwwroot/amouse.smart0012.xyz/mah/pro_media.csv’, ‘w’);

$header_data=array(‘id’,’proudct_id’,’url’,’proudct_name’,’media_title’,’date’,’type’);

fputcsv($cfile,$header_data);

foreach ( $products as $product ){

//echo ‘<pre>’;
//print_r($products);
//echo ‘</pre>’;

// echo $product->get_status(); // Product status
// echo $product->get_type(); // Product type
// echo $product->get_id(); // Product ID
// echo $product->get_title(); // Product title
// echo $product->get_slug(); // Product slug
// echo $product->get_price(); // Product price
// echo $product->get_catalog_visibility(); // Product visibility
// echo $product->get_stock_status(); // Product stock status

$pro_id = $product->get_id();

$pro_title = $product->get_title();

$attachment_ids = $product->get_gallery_image_ids();

foreach( $attachment_ids as $image_id ) {
//$image_id = $product->get_image_id();

$image_url = wp_get_attachment_url( $image_id );

$image_title = get_the_title( $image_id );

$image_date = get_the_date( ”, $image_id );

$image_type = get_post_mime_type($image_id);
$image_link = wp_get_attachment_url( $attachment_id );

$media_array = array($image_id,$pro_id, $image_url,$pro_title, $image_title, $image_date, $image_type );
fputcsv($cfile, $media_array);
}

// echo $product->get_date_created()->date(‘Y-m-d H:i:s’);
// echo $product->get_date_modified()->date(‘Y-m-d H:i:s’);

}

fclose($cfile);
wp_reset_query();

Source:
wc_get_products and WC_Product_Query
Export to CSV via PHP