admin管理员组文章数量:1389761
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; ?>
本文标签: wp queryHow do I get Woocommerce product variation name and variation description in a WPQuery
版权声明:本文标题:wp query - How do I get Woocommerce product variation name and variation description in a WP_Query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744672147a2618888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论