Php 在WooCommerce中将产品变体价格替换为自定义字段值

Php 在WooCommerce中将产品变体价格替换为自定义字段值,php,wordpress,woocommerce,price,variations,Php,Wordpress,Woocommerce,Price,Variations,我想用自定义字段(“批发价”)替换价格,但无法确定如何获取所选变体的ID function new_variable_price_format( $price, $product ) { echo '<pre>'; var_dump($product); echo '</pre>'; // return get_post_meta( $ID, '_wholesale_price', true); } add_filter( 'wo

我想用自定义字段(“批发价”)替换价格,但无法确定如何获取所选变体的ID

function new_variable_price_format( $price, $product ) {

    echo '<pre>';
    var_dump($product);
    echo '</pre>';


    // return get_post_meta( $ID, '_wholesale_price', true);

}

add_filter( 'woocommerce_variable_sale_price_html', 'new_variable_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'new_variable_price_format', 10, 2 );
函数新\变量\价格\格式($price,$product){
回声';
var_dump($产品);
回声';
//return get_post_meta($ID,“批发价”,true);
}
添加过滤器('woocommerce\u variable\u sale\u price\u html','new\u variable\u price\u format',10,2);
添加过滤器('woocommerce\u variable\u price\u html','new\u variable\u price\u format',10,2);
您应该尝试以下方法:

这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中


已测试且有效

感谢您提供此片段:)
// Min and max variable prices
add_filter( 'woocommerce_variable_sale_price_html', 'new_variable_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'new_variable_price_format', 10, 2 );
function new_variable_price_format( $formated_price, $product ) {

    $price = get_post_meta( $product->get_id(), '_wholesale_price', true);
    return wc_price($price);
}

// Selected variation prices
add_filter('woocommerce_product_variation_get_sale_price', 'custom_product_get_price', 10, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_get_price', 10, 2 );
function custom_product_get_price( $price, $product ){
    return get_post_meta( $product->get_id(), '_wholesale_price', true);
}