Saving custom field data from Admin WooCommerce product data metabox

In Woocommerce I use some custom fields for product specifications, and save the specifications in the post_meta.

I’m trying to make an if loop to write down in the post_meta another product specification.

The code I now use is:

add_action( 'woocommerce_product_options_general_product_data', 'BTW_field' );
function BTW_field() {
    woocommerce_wp_radio( 
        array( 
            'id'          => '_BTW', 
            'default'     => '21% BTW', 
            'required' => true,
            'options' => array(
                'Prijs is incl. 21% BTW'   => '21% BTW',
                'Margeproduct'   => 'Marge Product',
            )
        )
    );
}

add_action( 'woocommerce_process_product_meta', 'BTW_save' );
function BTW_save( $post_id ){  
    $BTW = $_POST['_BTW'];
    if( !empty( $BTW ) )
        update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}

An now I try to rewrite the BTW_save function so it will save another post_meta.

function BTW_save( $post_id ){  
$BTW = $_POST['_BTW'];
    if( !empty( $BTW ) ){
        update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
    }
    if ($BTW == "Margeproduct (vrijgesteld van BTW)"){
        $BTW2 = "Margeproduct*"
    } else {
        $BTW2 = "21%"
    }
    update_post_meta( $post_id, '_BTW_NAME', esc_attr( $BTW2 ) );
}

I don’t know how I can check if $BTW is equal to the post_meta _BTW and how I can rewrite it so $BTW2 will also save in the post meta as _BTW_NAME.

Updated: As you are setting 2 different values, it could be better to use a select field instead.

Also I have make some changes in your code regarding correct variable naming and field keys naming (You should be able to rename them easily keeping in mind that lowercase and underscores are recommended).

Here is the code:

add_action( 'woocommerce_product_options_general_product_data', 'add_btw_field' );
function add_btw_field() {
    global $post;

    // Get the selected value
    $value = get_post_meta( $post->ID, '_btw', true );
    if( empty( $value ) ) $value = 'btw'; // Default value

    woocommerce_wp_select( array(
        'id'       => 'btw_select',
        'label'    => __( 'BTW-prijsopties', 'woocommerce' ),
        'options'  => array(
            'btw'  => __( '21% BTW', 'woocommerce' ),
            'marge'   => __( 'Marge Product', 'woocommerce' ),
        ),
        'value'   => $value, // Displaying the selected value
    ) );
}

add_action( 'woocommerce_process_product_meta', 'save_btw_field' );
function save_btw_field( $post_id ){

    if( empty( $_POST['btw_select'] ) ) return; // exit (in case of)

    update_post_meta( $post_id, '_btw', esc_attr( $_POST['btw_select'] ) );

    if ( $_POST['btw_select'] == 'btw' )
        $label = __( 'BTW 21%', 'woocommerce' );
    else
        $label = __( 'Margeproduct (vrijgesteld van BTW)', 'woocommerce' );

    update_post_meta( $post_id, '_btw_label', esc_attr( $label ) );
}

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

Tested and works. You will get something like that:

By default when creating or updating a product, both custom fields will be saved for ‘btw’ option as product meta data…

You will be able to get both product post_meta custom fields values using get_post_meta():

// Set HERE the product ID (or get it dynamically)
$product_id = 37;
$btw = get_post_meta( $product_id, '_btw', true ); // Values can be 'yes' or 'no'
$btw_label = get_post_meta( $product_id, '_btw_label', true );

// Output (testing):
echo $btw_label;