Get ALL Products from WooCommerce store

I am trying to get all the products in my Woocommerce online store. I want to get this data, for all the products, and print it as json. But the page keeps showing up blank.

Itried the below to get all the products ids, so that all the products would show.

Read More
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'p' => '*');

I also tried this:

    $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 470);

   $loop = new WP_Query( $args );

And this:

    $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1);

   $loop = new WP_Query( $args );

All produce the same result, with the page showing up blank, getting no data.

I have used similar code to this before, such as to show all the products in a particular category and it worked. But I can’t seem to get this one to work, to show all the products in the store.

Please any help?

Related posts

Leave a Reply

2 comments

  1. WooCommerce 2.1+ now ships with it’s own REST API. You should definitely check it out.
    Notice that you must use https or OAuth to access it, so you might want to use a client like this one (I’m not related to this library).

    It’s not clear what data you want to retreive from your store, but from your setup above, you could try this simple demo example:

    add_action( 'init', function() {
        add_rewrite_endpoint( 'wooexport', EP_ROOT );
    });
    
    add_action( 'template_redirect', function() {
        if( 'json' === get_query_var( 'wooexport' ) ) {
            $posts = get_posts( array( 'post_type' => 'product' ) ); 
            wp_send_json_success( $posts );
        }  
    });
    

    where you must remember to flush the rewrite rules, for example by resaving the permalinks settings. You can then modify the query arguments to your needs and access it from example.com/wooexport/json/.

    Most likely you got a PHP syntax error, resulting in a white screen?. So always use WP_DEBUG on your test site, check for example Debugging in WordPress from the Codex.

  2. I will suggest you to use export option in wordpress to get data from your current site and then use import option to get data in the new site that you want.Its simple no coding needed at all.