Display WooCommerce variable product price on other pages

I’m using WooCommerce on a WordPress site that I’m building and I need to be able to display a specific product’s price throughout the site. Normally that wouldn’t be an issue, but in this instance it’s a product which has 2 variations, so I need to show both of them (e.g. £4.99 – £9.99). How can I retrieve these values and echo them out?

Related posts

Leave a Reply

1 comment

  1. Put the following in your theme’s functions.php file:

    function so_28073705( $product_id ) {
    
        $wc_product_variable  =  new WC_Product_Variable( $product_id );
    
        $variation_price_html  =  $wc_product_variable->get_price_html( );
    
        return $variation_price_html;
    
    }
    

    When you want to use it:

    <?php echo so_28073705( <product_id> ); ?>
    

    returns:

    <span class="amount">$low-price</span>–<span class="amount">$high-price</span>