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

Replace product variations prices with custom field value in WooCommerce

我想用我的自定义字段替换价格 (_wholesale_price),但不知道如何获取所选变体的 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 );

你应该试试这个:

// 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);
}

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

已测试并有效