you could use something like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$results = new WP_Query( $args );
$products = $results->posts;
$json_data = array();
foreach($products as $product){
$product = wc_get_product( $product->ID);
if($product->is_visible()){
$json_data[] = array(
'id'=> $product->get_id(),
'title'=> $product->get_title(),
);
}
}
$output= json_encode($json_data);
reutrn $output
this part get all the products (active, inactive, and all statuses)
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$results = new WP_Query( $args );
so if you want to use it, it is better to consider some verification for products for instance
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => array( 'publish' ),
);
$results = new WP_Query( $args );
in this line:
if($product->is_visible())
you could filter products, for instance out of stock or not visible etc
finally this line encode your data to json:
$output= json_encode($json_data);