Zoric
Zoric

Reputation: 61

Replace product variations prices with custom field value in WooCommerce

I want to replace the price with my custom field (_wholesale_price) but cant figure out how to get the ID of the selected variation

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

Upvotes: 1

Views: 949

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You should try this:

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

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works

Upvotes: 2

Related Questions