Praveen
Praveen

Reputation: 1005

How to display woocommerce variable options in custom loop

I want to display variable options in the radio button format with image, variable title, and variable description. I tried using the below code but not able to get variable options. Please help me with the code.

<?php $args = array(
    'posts_per_page'   => 1,
    'include'          => 83,
    'exclude'          => 87,
    'post_type'        => 'product',
    'post_status'      => 'publish',
    );?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <?php $price = get_post_meta( get_the_ID(), '_price', true ); ?>
    <p><?php echo wc_price( $price ); ?></p>
<?php endwhile; wp_reset_query(); ?>

Upvotes: 0

Views: 402

Answers (1)

Vasim Shaikh
Vasim Shaikh

Reputation: 4512

There is $woocommerce, which is is crucial. Several classes stored into this, see woocommerce class reference and additionally WC API Docs.

If you're looking for product information:

$product = wc_get_product( $post->ID );

is certainly a good entry point. If you're already on a product page or generally in the wc loop, you often have $product already available.

Another thing to add is, wc product(s) are are just a custom post type, so you can do a lot by using wordpress core functions - like:

$all_meta = get_post_meta( get_the_ID() );
echo $all_meta;

// Get variable issue you have product ID

global $product;
if( $product->is_type( ‘variable’ )) {

$attribute_keys = array_keys( $product->get_attributes() );
print_r($attribute_keys)
}

Upvotes: 1

Related Questions