Display product prices with a shortcode by product ID in WooCommerce

16,895

Solution 1

Updated (takes into account if your prices are displayed with or without taxes)

With Woocommerce there is already formatting price function wc_price() that you can use in your code. Also you need to get the sale price

To get this working when there is a sale price or without it try this code (commented):

function custom_price_shortcode_callback( $atts ) {

    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'product_price' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
        // Get an instance of the WC_Product object
        $product = wc_get_product( intval( $atts['id'] ) );

        // Get the product prices
        $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
        $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
        $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

        // Your price CSS styles
        $style1 = 'style="font-size:40px;color:#e79a99;font-weight:bold;"';
        $style2 = 'style="font-size:25px;color:#e79a99"';

        // Formatting price settings (for the wc_price() function)
        $args = array(
            'ex_tax_label'       => false,
            'currency'           => 'EUR',
            'decimal_separator'  => '.',
            'thousand_separator' => ' ',
            'decimals'           => 2,
            'price_format'       => '%2$s %1$s',
        );

        // Formatting html output
        if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
            $html = "<del $style2>" . wc_price( $regular_price, $args ) . "</del> <ins $style1>" . wc_price( $sale_price, $args ) . "</ins>"; // Sale price is set
        else
            $html = "<ins $style1>" . wc_price( $price, $args ) . "</ins>"; // No sale price set
    }
    return $html;
 }
 add_shortcode( 'product_price', 'custom_price_shortcode_callback' );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


USAGE (for example product ID 37):

[product_price id="37"]

This code is tested and works. You will get this:

enter image description here

Solution 2

An alternative, if anyone needs simple text output of prices, is the following:

function wc_price_by_id_shortcode( $atts ) {
    $atts = shortcode_atts( array( 'id' => null, ), $atts, 'bartag' );

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){ 
        $_product = wc_get_product( $atts['id'] ); 
        $price = wc_price( $_product->get_price() );
      } 
  return $price; 
} 
add_shortcode( 'product_price', 'wc_price_by_id_shortcode' );

The shortcode is then [product_price id=XX] (replacing XX with the product id)

To add styling to the price, add a span class by adding the line:

$html .= "<span class="price_by_id_shortcode">". $price . "</span>";,

change the line return $price; to return $html;,

then add your style formatting to your css as needed.

(N.B. this only outputs current price, not regular price.)

Share:
16,895
Gaou
Author by

Gaou

Updated on June 14, 2022

Comments

  • Gaou
    Gaou almost 2 years

    IN WooCommerce I am using the code of this tread to display with a short code the product prices from a defined product ID. But it don't really do what I want. Here is that code:

    function so_30165014_price_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
    'id' => null,
    ), $atts, 'bartag' );
    
    $html = '';
    
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
     $_product = wc_get_product( $atts['id'] );
     $number = number_format($_product->get_price(), 2, '.', ',');
     $html = "$" . $number;
    
     }
     return $html;
     }
     add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
    

    I have a poor knowledge in php coding. But I've seen that there this other thread to display product prices:

    $_product->get_regular_price();
    $_product->get_sale_price();
    $_product->get_price();
    

    I Have tried to mix these code into the big code, and replaced get_price()… It works, but what I want is to display prices is something like this :

    What I want to display

    So the Regular price crossed out, and the Sale price next to it, like in this screenshot. If there is no Sale price, it display only the regular price.

    Also I have some other problems:

    • I need to display the price is in , not in $, so I have replaced the currency symbol from $ (dollars) to (euros) with this code: $html = "€" . $number;

    • I need to display the currency symbol after the price, like : 37 € (with a blank space between), not like $37.

    How can I make it work in a clean normal way?

  • Gaou
    Gaou over 6 years
    oups, I've didn't seen your reply. So sorry ! Thank you very much
  • Gaou
    Gaou over 6 years
    and thank's for the comments, I can understand what this code mean. Thank you
  • Mike
    Mike about 6 years
    Can you please update the code so it will accept active currency, not the default. I am using multicurrency swither and cannot figure this out, otherwise, your function works perfectly.
  • LoicTheAztec
    LoicTheAztec about 6 years
    @Mike This code is not maid for Multicurrency switcher plugin… so you will have to ask a new question…
  • alexkodr
    alexkodr over 5 years
    Really great answer. Can you maybe update this so the price includes tax?
  • alexkodr
    alexkodr over 5 years
    I figured it out. For anyone else that wants to know just change any reference of get_price to get_price_including_tax
  • LoicTheAztec
    LoicTheAztec over 5 years
    @alexgomy Updated the answer using wc_get_price_to_display() dedicated function on prices.
  • Marius
    Marius almost 4 years
    A little bit of correction: to add styling the line should be: $html .= '<span class="price_by_id_shortcode">'. $price . "</span>"; (' ', instead of " ") otherwise snippet plugins will parse errors, WP too. And the shortcode should be [product_price id="XX"]. Simple and works as expected. Woocommerce native do not have a price shortcode by default which is unexplained. If you write a blog post and later the price will change you must comeback to all your posted articles to edit the price which is a madness. Instead of price to update itself on your articles if you update the product price.
  • chelder
    chelder over 3 years
    Note: related question to show the currency depending on which country the user is: Display product prices in the local currency with a shortcode by product ID in WPML WooCommerce Multilingual
  • chelder
    chelder over 3 years
    @LoicTheAztec not sure if my answer below is the version you need. I've asked a new question to show the currency depending on which country the user is: Display product prices in the local currency with a shortcode by product ID in WPML WooCommerce Multilingual
  • LoicTheAztec
    LoicTheAztec over 3 years
    @chelder Your answer is not really related to this thread as OP didn't ask about language support and multicurrency (wpml plugin). So it might be better to remove it from this thread and include it on your own question thread. When done let notify me (I will add a link below my answer).
  • chelder
    chelder over 3 years
    @LoicTheAztec hmm I guess you are right, but no time for that. You can ask to remove it from here if you think it's the best ;)