2
votes

This is how, I add an admin custom field for product Variations:

add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
     
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( array(
        'id' => 'custom_field[' . $loop . ']',
        'class' => 'short',
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'custom_field', true )
    ) );
}

Save custom field on product variation save:

add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
    $custom_field = $_POST['custom_field'][$i];
    if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}

Store custom field value into variation data

add_filter( 'woocommerce_available_variation', 'add_custom_field_variation_data' );
function add_custom_field_variation_data( $variations ) {
    $variations ['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
    return $variations;
}

The value is saving and displaying in the single product page but the below function is not returning the value

Trying to retrieve Value of the custom field in a function:

add_filter('woocommerce_variation_prices_price', 'get_custom_variable_price' )
function get_custom_variable_price() {

    global $post;
    $custom_value = get_post_meta( $variations[ 'variation_id' ], 'custom_field', true );
    $custom_value = (float)$custom_value;
    return $custom_value;   
}

I need to get just the value (which is float number):

1
Where do you use your last function?… please add some more context as your question is actually unclear…LoicTheAztec
I am using last function on 'woocommerce_variation_prices_price'. The purpose is to get the custom value to be multiplied by the variation product priceBilal
I have edited your question then, and answered. Some feed back on the answer below will be appreciated please.LoicTheAztec

1 Answers

1
votes

There are some mistakes and missing things in your las function (see below).

Now there are 2 ways to get the custom field value in that hooked function:

1). Using WC_Data get_meta() method (since WooCommerce 3):

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( $variation->get_meta( 'custom_field' ) );
    if( $value > 0 ) {
        $price = $value; 
    }
    return $price;
}

2). Or using WordPress get_post_meta() function (the old way):

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( floatval( get_post_meta( $variation->get_id(), 'custom_field', true ) );
    if( $value > 0  ) {
        $price = $value; 
    }
    return $price;
}

Code goes in functions.php file of the active child theme (or active theme). Tested, works for both.

Important note: You may not see the result in front end, as variable product price range is cached in WooCommerce to improve performances (see the linked thread below for better understanding).

Related thread: Change product prices via a hook in WooCommerce 3+


Addition related to your others functions

Since WooCommerce 3 you could update your 2nd and 3rd function as follows:

add_action( 'woocommerce_admin_process_variation_object', 'save_custom_field_variation_value', 10, 2 );
function save_custom_field_variation_value( $variation, $i ) {
    if( isset($_POST['custom_field'][$i]) ) {
        $variation->update_meta_data( 'custom_field', floatval( sanitize_text_field($_POST['custom_field'][$i]) ) );
    }
}

and 3rd function

add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_value_to_variation_data', 10, 3 );
function add_variation_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
    if ( $value = $variation->get_meta( 'custom_field' ) ) {
        $variation_data['custom_field'] = '<div class="woocommerce_custom_field">' .__("Custom Field") . ': <span>' . $value . '</span></div>';
    }
    return $variation_data;
}