我有具有多个属性的产品,允许多维度的变化。我想在下拉菜单中显示价格差异,但它不是比较每个选项的绝对最低价格。
这是我正在使用的代码:
add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
global $product;
if ( empty( $term ) ) {
return $term;
}
if ( empty( $product->id ) ) {
return $term;
}
$variation_id = $product->get_children();
$price_min = $product->get_variation_regular_price();
foreach ( $variation_id as $id ) {
$_product = new WC_Product_Variation( $id );
$variation_data = $_product->get_variation_attributes();
foreach ( $variation_data as $key => $data ) {
if ( $data == $term ) {
$html = $term;
$price_diff = $_product->get_price() - $price_min;
$price_html = '';
if ($price_diff > 0 ) { $price_html = ' (+£' . number_format((float)$price_diff, 2, '.', '') . ')';}
$html .= $price_html;
return $html;
}
}
}
return $term;
}
但是,这只是显示当前属性的最低价格,而不是产品整体。
我希望这很清楚 - 理想情况下,在上图中,对于不增加成本的选项,$price_html 应该是空白的。该代码适用于具有单个属性选项的变体,但不适用于具有多个属性选项的变体。