Answers for "woocommerce_save_product_variation"

0

woocommerce_save_product_variation

function variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input(
        array(
            'id'          => '_text_field[' . $variation->ID . ']',
            'label'       => __( 'My Text Field', 'woocommerce' ),
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );

}
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if ( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }
}

add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
Posted by: Guest on March-23-2022

Browse Popular Code Answers by Language