$product = new WC_Product( $product_id ); // assuming $product_id is a valid product ID if ( $product->hasAttributes() ) { echo "This product has attributes"; } else { echo "This product does not have attributes"; }
$args = array( 'post_type' => 'product' ); $products = new WP_Query( $args ); while ( $products->have_posts() ) { $products->the_post(); $product = new WC_Product( get_the_ID() ); if ( $product->hasAttributes() ) { echo get_the_title() . " has attributes"; } else { echo get_the_title() . " does not have attributes"; } } wp_reset_postdata();In this example, we use the `WP_Query` class to retrieve all products and then loop through them using a while loop. Inside the loop, we create a new product object using the `WC_Product` class and pass in the ID of the current product (`get_the_ID()`). We then use the `hasAttributes` method to check if the product has any attributes and display an appropriate message. Finally, we reset the post data using `wp_reset_postdata`. From these examples, it's clear that the `hasAttributes` method is part of the WooCommerce plugin for WordPress, since we're using the `WC_Product` class to work with products.