Php echo woocommerce price

admin2025-06-06  1

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?

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?

Share Improve this question edited Oct 31, 2018 at 8:38 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Oct 30, 2018 at 21:24 Panagiotisou KoukouzelisPanagiotisou Koukouzelis 11 silver badge1 bronze badge 2
  • where do you want to display the price? on single product's page or another page? – Akshat Commented Oct 30, 2018 at 22:45
  • I want to display it on product page. But all my products are variable not single products – Panagiotisou Koukouzelis Commented Nov 1, 2018 at 6:37
Add a comment  | 

1 Answer 1

Reset to default 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/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/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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749215226a317328.html

最新回复(0)