升级@Abhinav的代码以在没有默认设置的情况下显示最低价格:
/**
* Show default variation price
* If no default variation, shows min price
*/
add_filter(\'woocommerce_variable_price_html\', \'custom_variation_price\', 10, 2);
function custom_variation_price($price, $product) {
$available_variations = $product->get_available_variations();
$selectedPrice = \'\';
$dump = \'\';
$defaultArray = array();
foreach ($product->get_default_attributes() as $key => $val) {
// $dump = $dump . \'<pre>\' . var_export($key, true) . \'</pre>\';
// $dump = $dump . \'<pre>\' . var_export($val, true) . \'</pre>\';
$defaultArray[\'attribute_\' . $key] = $val;
}
// $dump = $dump . \'<pre>\' . var_export($defaultArray, true) . \'</pre>\';
if (empty($defaultArray)) {
$price = $product->get_variation_price( \'min\', true ); // no default variation, show min price
} else {
foreach ($available_variations as $variation) {
// $dump = $dump . \'<pre>\' . var_export($variation[\'attributes\'], true) . \'</pre>\';
$isDefVariation = false;
$result = array_diff($defaultArray, $variation[\'attributes\']);
if (empty($result)) {
$isDefVariation = true;
$price = $variation[\'display_price\'];
break;
}
}
}
$selectedPrice = wc_price($price);
// $dump = $dump . \'<pre>\' . var_export($available_variations, true) . \'</pre>\';
return $selectedPrice . $dump;
}