I realize this should be simple for most coders but I am not a coder!
I want to display product price including Currency symbol somewhere on my page/text.
I have done this before but have totally forgotten how to do it! I have copied the code below but am unsure where to place it, also do I replace post_id with my product ID? any help from you guys appreciated
$product = wc_get_product( $post_id );
$product->get_regular_price();
$product->get_sale_price();
$product->get_price();
CodePudding user response:
You can do that simply by.
<?php echo $_product->get_price_html(); ?>
where $_product
is wc_get_product( $product_id )
.
This ensures that it is compatible for future updates of woocommerce and extensions.
CodePudding user response:
You can display the products with prices anywhere in your site using this code
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'occasions', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="occasion_block">
<div class="occsn_img">
<a href="<?php the_permalink(); ?>"><?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?></a>
</div>
<div class="occsn_cont">
<a href="<?php the_permalink(); ?>"><span class=""h4_span><?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?> </span></a>
<div class="occsn_fetr">
<?php if($product->is_on_sale()){?>
<span class="h5_span"><i><?php echo '€'. $product->get_regular_price();?></i><b><?php echo '€'.$product->get_sale_price();?></b></span>
<?php }?>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
'product_cat'
=> is what your product category should be shown or else remove that if you need to display all the products
<?php echo '€'. $product->get_regular_price();?>
is used to display the products price and you can also change the price symbol as per your need.