0

I would select all woocommerce products in my wordpress solution trough wc_get_products() and pass the result in a JSON variable returned by the function created.

How did you develop it? I can show screenshots if it is necessary. Thanks for the attention.

0

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);
  • Thank you very much. I'll verify and then I'll let you know. – Salvatore Riccardi Dec 30 '18 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.