'Get all WooCommerce products within own plugin

Within a plugin how do I get products using wc_get_products()? Or is there another way to do it?

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
      // Put your plugin code here
      add_action( 'woocommerce_loaded', 'generate_product_qr_codes');
}
function generate_product_qr_codes {
$args = array(
    'post_status' => 'publish',
    'limit' => 100
  );
  $products = wc_get_products( $args );
}

The problem is that not products is returned (empty array).



Solution 1:[1]

Try this

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        // Put your plugin code here
        add_action( 'woocommerce_loaded', 'generate_product_qr_codes' );
    }
    
    function generate_product_qr_codes() {
    
        $args        = array( 'post_type' => 'product', 'posts_per_page' => -1 );
        $products    = get_posts( $args );
        echo '<pre>$products:-';
        print_r( $products );
        echo '</pre>';
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujuonly