我使用此功能将价格添加到WooCommerce变体下拉菜单中。我试图用span标记来包装价格,但它将标记显示为文本。
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->get_id() ) ) {
return $term;
}
$variation_id = $product->get_children();
foreach ( $variation_id as $id ) {
$_product = new WC_Product_Variation( $id );
$variation_data = $_product->get_variation_attributes();
foreach ( $variation_data as $key => $data ) {
$display_price = \'\';
switch ($id) {
case 1:
$display_price = $_product->get_price() / 12;
break;
case 2:
$display_price = $_product->get_price();
break;
case 3:
$display_price = $_product->get_price() / 6;
break;
default:
}
if ( $data == $term ) {
$html = $term;
$html .= \'<span>\' . $display_price . \'</span>\';
return $html;
}
}
}
return $term;
}