该函数get_variation_prices()
是WC_Product_Variable
Class 的一个方法,它专门作为可变产品实例对象的方法工作。它给出了所有变化价格的多维数组。
要获得最小和最大变化价格,您必须使用WC_Product_Variable
方法:
get_variation_regular_price()
或者get_variation_regular_price('max')
get_variation_sale_price()
或者get_variation_sale_price('max')
get_variation_price()
或者get_variation_price('max')
现在在您的代码中:
- 您首先需要获取
WC_Product
实例对象。
- 检查产品类型。
- 删除
global $product;
,因为它是空的且无用。
- (您可能需要根据您的要求进行其他更改)
现在有多种查询产品的方法:
1)使用 a WP_Query
(就像你实际做的那样):
function wtProductResults($data){
global $woocommerce;
$products = new WP_Query([
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //can be set to ID
'terms' => $data['cat'] //if field is ID you can reference by cat/term number
) )
]);
$productsResults = [];
$currency = get_woocommerce_currency_symbol();
if ( $products->have_posts() ) :
while ( $products->have_posts() ) : $products->the_post();
$product_cat = get_term( $data['cat'], 'product_cat', 'category', "OBJECT" );
// Get an instance of the WC_Product object
$product = wc_get_product( get_the_ID() );
if( $product->is_type('variable') ) {
// Min variation price
$regularPriceMin = $product->get_variation_regular_price(); // Min regular price
$salePriceMin = $product->get_variation_sale_price(); // Min sale price
$priceMin = $product->get_variation_price(); // Min price
// Max variation price
$regularPriceMax = $product->get_variation_regular_price('max'); // Max regular price
$salePriceMax = $product->get_variation_sale_price('max'); // Max sale price
$priceMax = $product->get_variation_price('max'); // Max price
// Multi dimensional array of all variations prices
$variationsPrices = $product->get_variation_prices();
$regularPrice = $salePrice = $price = '';
$variationPrice = [
'min' => $product->get_variation_price(),
'max' => $product->get_variation_price('max')
];
}
// Other product types
else {
$regularPrice = $product->get_regular_price();
$salePrice = $product->get_sale_price();
$price = $product->get_price();
$variationPrice = ['min' => '', 'max' => ''];
}
array_push( $productsResults , [
'title' => get_the_title(),
'productId' => get_the_id(),
'permalink' => get_the_permalink(),
'thumbnail' => get_the_post_thumbnail(),
'excerpt' => get_the_excerpt(),
'regularPrice' => $regularPrice,
'price' => $price,
'salePrice' => $salePrice,
'category' => $product_cat->name,
'variationPrice' => $variationPrice,
]);
endwhile;
wp_reset_postdata();
endif;
return $productsResults;
}
2) 使用 aWC_Product_Query
代替:
function wtProductResults($data){
global $woocommerce;
$products = wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'category' => array($data['cat']),
) );
$productsResults = [];
$currency = get_woocommerce_currency_symbol();
if ( sizeof($products) > 0 ) :
foreach ( $products as $product ) :
$term_name = get_term( $data['cat'], 'product_cat' )->name;
if( $product->is_type('variable') ) {
// Min variation price
$regularPriceMin = $product->get_variation_regular_price(); // Min regular price
$salePriceMin = $product->get_variation_sale_price(); // Min sale price
$priceMin = $product->get_variation_price(); // Min price
// Max variation price
$regularPriceMax = $product->get_variation_regular_price('max'); // Max regular price
$salePriceMax = $product->get_variation_sale_price('max'); // Max sale price
$priceMax = $product->get_variation_price('max'); // Max price
// Multi dimensional array of all variations prices
$variationsPrices = $product->get_variation_prices();
$regularPrice = $salePrice = $price = '';
$variationPrice = [
'min' => $product->get_variation_price(),
'max' => $product->get_variation_price('max')
];
}
// Other product types
else {
$regularPrice = $product->get_regular_price();
$salePrice = $product->get_sale_price();
$price = $product->get_price();
$variationPrice = ['min' => '', 'max' => ''];
}
array_push( $productsResults , [
'title' => $product->get_name(),
'productId' => $product->get_id(),
'permalink' => $product->get_permalink(),
'thumbnail' => $product->get_image(),
'excerpt' => $product->get_short_description(),
'regularPrice' => $regularPrice,
'price' => $price,
'salePrice' => $salePrice,
'category' => $term_name,
'variationPrice' => $variationPrice,
]);
endforeach;
endif;
return $productsResults;
}