Ste*_*ano 2 php wordpress product woocommerce price
我有一个产品列表,但在显示可变产品的价格范围时遇到了问题。
如您所见,如果价格是正常价格,一切正常。然而,如果产品的价格可变,则显示的价格为 0 欧元
那么,显示价格的代码是这样的:
<?php
if (isset($ebookstore_theme_option['woo-list-price']) &&
$ebookstore_theme_option['woo-list-price'] == 'enable') {
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
Run Code Online (Sandbox Code Playgroud)
关于如何显示可变范围价格的任何想法?
对于 WooCommerce 中的可变产品,您需要:
因此,您应该需要获取产品对象的实例。一般来说,您可以通过以下方式获得:
global $product;
Run Code Online (Sandbox Code Playgroud)
如果它不起作用,您将使用 intead:
global $post;
$product = wc_get_product( $post->ID );
Run Code Online (Sandbox Code Playgroud)
现在,您将使用以下内容:
<?php
global $product;
if ( isset( $ebookstore_theme_option['woo-list-price'] ) && $ebookstore_theme_option['woo-list-price'] == 'enable' ){
// For all product types that are not "variable products"
if( ! $product->is_type('variable') ){
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
// For variable products
else {
$min_price = $product->get_variation_price( 'min' );
$max_price = $product->get_variation_price( 'max' );
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$min_price.' - '.esc_attr($currency).''.$max_price.'</strong>';
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用这些WC_Product_Variable方法
可能你不知道,woocommerce 有几个格式化价格函数:
wc_price( $price );wc_format_price_range( $from, $to );wc_format_sale_price( $regular_price, $sale_price );他们都将包括货币