TomJones999
TomJones999

Reputation: 853

Woocommerce: shortcode / PHP to display current price of a variable product?

I'm customizing the product page of a site that will have variable products.

Currently, using the Booster for Woocommerce plugin, which gives me convenient shortcodes like [wcj_product_regular_price] and [wcj_product_sale_price], that nicely display the regular and sale prices of the product.

Problem is, when it comes to variable products, they both show $ 0 until variations are selected.

Is there a Woocommerce native shortcode (or a PHP code) to display the current price (or the price range) of a variable product? Like, if the product's variations range from $ 20 to $ 50, how can I display something like "This product's price ranges between {shortcode_I_don't_know}, please choose options to see the exact pricing"?

Upvotes: 1

Views: 2714

Answers (2)

TomJones999
TomJones999

Reputation: 853

OK, got it figured out.

Edwin's answer to use the Booster shortcode [wcj_product_price] (instead of [wcj_product_regular_price] and [wcj_product_sale_price]) outputs the min-max range, i.e. $40-$80.

For people who don't use Booster, there's a way to use Woocommerce's built-in variables:

Minimum Price: $ <?php echo ($product->get_variation_sale_price()); ?>
Maximum Price: $ <?php echo ($product->get_variation_sale_price('max')); ?>

Output:

  • Minimum Price: $ 40.00
  • Maximum Price: $ 80.00

Bonus: if you don't want the trailing zeroes, use floatval:

Minimum Price: $ <?php echo floatval(($product->get_variation_sale_price())); ?>
Maximum Price: $ <?php echo floatval(($product->get_variation_sale_price('max'))); ?>

Output:

  • Minimum Price: $ 40
  • Maximum Price: $ 80

For future reference, here are the WooCommerce native PHP variables for variable products:

$product->get_variation_regular_price(); // Min regular price
$product->get_variation_sale_price(); // Min sale price
$product->get_variation_price(); // Min price

$product->get_variation_regular_price('max'); // Max regular price
$product->get_variation_sale_price('max'); // Max sale price
$product->get_variation_price('max'); // Max price

Upvotes: 1

Edwin Sturt
Edwin Sturt

Reputation: 16

Try these shortcode

[wcj_product_price] shortcode displays a price for WooCommerce product

Upvotes: 0

Related Questions