0

Hi i am trying to echo with php the woocommerce product price in a specific area at my site. But it only works for single products. Not for variable products. What i must do?

  • where do you want to display the price? on single product's page or another page? – Akshat Oct 30 '18 at 22:45
  • I want to display it on product page. But all my products are variable not single products – Panagiotisou Koukouzelis Nov 1 '18 at 6:37
1

You can use either get_variation_prices( $for_display = false ) method or get_variation_regular_price( $min_or_max = 'min', $for_display = false )

Something like this:

global $product;
var_dump( $product->get_variation_prices() );

https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-product-variable.php#L97

/**
 * Get the min or max variation regular price.
 *
 * @param  string  $min_or_max Min or max price.
 * @param  boolean $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
 * @return string
 */
public function get_variation_regular_price( $min_or_max = 'min', $for_display = false ) {
    $prices = $this->get_variation_prices( $for_display );
    $price  = 'min' === $min_or_max ? current( $prices['regular_price'] ) : end( $prices['regular_price'] );
    return apply_filters( 'woocommerce_get_variation_regular_price', $price, $this, $min_or_max, $for_display );
}

https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-product-variable.php#L80

/**
 * Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.
 *
 * @param  bool $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
 * @return array Array of RAW prices, regular prices, and sale prices with keys set to variation ID.
 */
public function get_variation_prices( $for_display = false ) {
    $prices = $this->data_store->read_price_data( $this, $for_display );
    foreach ( $prices as $price_key => $variation_prices ) {
        $prices[ $price_key ] = $this->sort_variation_prices( $variation_prices );
    }
    return $prices;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.