Php echo woocommerce price

时间:2018-10-30 作者:Panagiotisou Koukouzelis

您好,我试图在我的网站上的特定区域与php回应woocommerce产品的价格。但它只适用于单一产品。不适用于可变产品。我必须做什么?

1 个回复
SO网友:sandrodz

您可以使用get_variation_prices( $for_display = false ) 方法或get_variation_regular_price( $min_or_max = \'min\', $for_display = false )

类似这样:

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;
}

结束

相关推荐