In WooCommerce possiamo esportare i prodotti in formato JSON.
La soluzione รจ la seguente:
function my_wc_json_export() {
$output = array();
$args = array(
'post_type' => 'product',
'numberposts' => -1
);
$loop = get_posts( $args );
if( !empty( $loop ) ) {
foreach( $loop as $p ) {
$id = $p->ID;
$product = new WC_Product( $id );
if( $product->is_visible() && $product->is_in_stock() && $product->is_purchasable() ) {
$data = array();
$img = wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' );
$data['id'] = $id;
$data['sku'] = $product->get_sku();
$data['name'] = $product->get_name();
$data['description'] = strip_tags( $product->get_short_description() );
$data['price'] = $product->get_price();
$data['image'] = $img[0];
$data['regular_price'] = $product->get_regular_price();
$data['sale_price'] = $product->get_sale_price();
$output[] = $data;
}
}
}
return json_encode( $output );
}