简体   繁体   中英

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).

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>';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM