skip to Main Content

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();

3

Answers


  1. 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 '&euro;'. $product->get_regular_price();?></i><b><?php echo '&euro;'.$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 '&euro;'. $product->get_regular_price();?> is used to display the products price and you can also change the price symbol as per your need.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. You can use the below function for your requirement.

    function show_product_price($atts){
        $atts = shortcode_atts( array( 
            'id' => null 
        ), $atts, 'woo_product_price' ); 
      
        if ( empty( $atts[ 'id' ] ) ) { 
            return ''; 
        } 
      
        $product = wc_get_product( $atts['id'] ); 
      
        if ( ! $product ) { 
            return ''; 
        } 
      
        return $product->get_price_html(); 
    }
    add_shortcode( 'woo_product_price', 'show_product_price');
    

    So this is a shortcode. You can add [woo_product_price id="your_product_id"] to show the price of a particular product in any page. All you have to do is to add the above mentioned shortcode to the page and provide an ID.

    This shortcode shows price of product including the currency. As an advantage, if the product is on sale, the sale price will also be shown.

    See the imagehere to get an idea on how it look.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search