I get the product variations each as their own button with below code that I have pieced together. Echo $product->name outputs something like "Product - variation" but I need "Variation price". Price would be entered in description field in the dashboard. I have gone through several related questions and answers but I'm not skilled enough to apply the solutions to my code so help would be appreciated!
<?php
$params = array('posts_per_page' => -1, 'post_type' => 'product_variation', 'order' => 'ASC');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<form class="cart" action="<?php the_permalink() ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr($product->id); ?>">
<button <?php post_class('button') ?> type="submit"><strong><?php echo $product->name ?> </strong></button>
</form>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<div>
<?php _e( 'No Products' ); ?>
</div>
<?php endif; ?>
I get the product variations each as their own button with below code that I have pieced together. Echo $product->name outputs something like "Product - variation" but I need "Variation price". Price would be entered in description field in the dashboard. I have gone through several related questions and answers but I'm not skilled enough to apply the solutions to my code so help would be appreciated!
<?php
$params = array('posts_per_page' => -1, 'post_type' => 'product_variation', 'order' => 'ASC');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<form class="cart" action="<?php the_permalink() ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr($product->id); ?>">
<button <?php post_class('button') ?> type="submit"><strong><?php echo $product->name ?> </strong></button>
</form>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<div>
<?php _e( 'No Products' ); ?>
</div>
<?php endif; ?>
Share
Improve this question
asked Mar 13, 2020 at 10:52
Ilkka GustafssonIlkka Gustafsson
111 silver badge4 bronze badges
1 Answer
Reset to default 1OK, so soon after posting I came up with a working solution. With this code I get the price straight from the variation price field, not from the description field as I mentioned in my question.
<?php
$params = array('posts_per_page' => -1, 'post_type' => 'product_variation', 'order' => 'ASC');
$wc_query = new WP_Query($params);
global $product;
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<form class="cart" action="<?php the_permalink() ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr($product->id); ?>">
<button <?php post_class('button') ?> type="submit"><strong><?php echo implode("", $product->get_variation_attributes()); ?> </strong><span style="display: inline-block"><?php echo $product->get_price(); ?></span></button>
</form>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<div>
<?php _e( 'No Products' ); ?>
</div>
<?php endif; ?>