Home > Net >  How can I check the woocommerce product created/have or not?
How can I check the woocommerce product created/have or not?

Time:11-16

How can I check woo-commerce products are created/have or not? I've added the code and I think also it's not in perfect condition. Actually, I want to check that, has any woo-commerce products. If it has then shown or not then show a notice. How can I do this?

$crtp_args = array('post_type' => 'product'); $sk_all_products = new WP_Query( $crtp_args );

while ( $sk_all_products->have_posts() ) : $sk_all_products->the_post();

global $product;

if(get_the_ID() == false){

    echo esc_html__('You don\'t have any products. Please add your products.', 'super-men');

}else{

    echo get_the_title();

}

endwhile;

wp_reset_query();

CodePudding user response:

You can simply check if your query has posts or not.

Try out this:

$sk_all_products = new WP_Query($crtp_args);

if ($sk_all_products->have_posts()) {
    while ($sk_all_products->have_posts()) : $sk_all_products->the_post();

        $product = wc_get_product(get_the_ID());

        echo $product->get_name();
        
    endwhile;
} else {

    echo esc_html__('You don\'t have any products. Please add your products.', 'super-men');

}
  • Related