4
votes

I made a custom plugin with a custom API endpoint to retrieve WooCommerce booking data.

Currently when I use wc_get_products I get a 200 status but the array is empty.

How do I tap into WooCommerce?

I've added this to my file:

add_action( 'woocommerce_init', array( 'woocommerce_loaded' ) );

but it does nothing.

When I run this:

function get_data() {

               $args = array( 'limit' => -1, 'return' => 'ids', 'type' => 'booking'  );

               $products = wc_get_products( $args );

               return $products;
           }

I get back the 11 IDs of the 11 products I have. But no other details? When I remove the IDs and do just 'limit' and 'type', I get back empty availability rules and no other data.

How can I get an array of products returned similar to the WooCommerce API endpoints for product?

2

2 Answers

0
votes
function get_data() {

    $args = array('limit' => -1, 'return' => 'objects', 'type' => 'booking');

    $products = wc_get_products($args);

    return $products;
}

Try like this

0
votes

wc_get_products returns object so you should get its data.

Try this

function get_data() {
    $args = array(
        'limit' => -1,
        'return' => 'objects',
        'type' => 'booking'
    );

    $products = [];
    $result = wc_get_products($args);
    foreach ($products as $product) {
        $products[] = $product->get_data();
    }

    return $products;
}