2

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Bilal
  • 31
  • 2
  • Where do you use your last function?… please add some more context as your question is actually unclear… – LoicTheAztec Mar 13 '21 at 11:37
  • 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 price – Bilal Mar 13 '21 at 12:19
  • I have edited your question then, and answered. Some feed back on the answer below will be appreciated please. – LoicTheAztec Mar 13 '21 at 12:40

1 Answers1

1

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;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi @loicTheAztec Thanks for ans & efforts. I try both version but its not working and showing a critical error on front-end. I think I didn't asked rightly, Let me try explain again; check: https://stackoverflow.com/questions/45806249/change-product-prices-via-a-hook-in-woocommerce-3/45807054#45807054 you have answered this also, what I needed is to get the custom added variation field value to be multiplied. 2) For theme version: ``function get_price_multiplier() { return 2; // x2 for testing // I NEED TO LOAD CUSTOM FIELD VALUE HERE}`` – Bilal Mar 13 '21 at 20:48
  • Actually it is not retrieving the custom_field ID nor get_meta is working and also I tried with get_the_ID() but they all are returning null – Bilal Mar 14 '21 at 00:17
  • I have explained the requirement here; https://stackoverflow.com/questions/66735034/woocommerce-product-variation-custom-field-and-its-price-calculation – Bilal Mar 21 '21 at 16:53