I have adapted the code in the Display selected variation custom fields value in WooCommerce product additional information tab post in order to be able to have custom dimension fields in both the product shipping tab area and the product variations tab area of product info. Here is my adapted code:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
function add_product_options_other_dimensions(){
global $product_object;
$product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => 'real_length',
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_width',
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_height',
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
function save_product_options_other_dimensions( $post_id ){
if( isset( $_POST['real_length'] ) )
update_post_meta( $post_id, 'real_length', esc_attr( $_POST['real_length'] ) );
if( isset( $_POST['real_width'] ) )
update_post_meta( $post_id, 'real_width', esc_attr( $_POST['real_width'] ) );
if( isset( $_POST['real_height'] ) )
update_post_meta( $post_id, 'real_height', esc_attr( $_POST['real_height'] ) );
}
// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
$variation_real_length = get_post_meta($variation->ID,"real_length", true );
if( ! $variation_real_length ) $variation_real_length = "";
$variation_real_width = get_post_meta($variation->ID,"real_width", true );
if( ! $variation_real_width ) $variation_real_width = "";
$variation_real_height = get_post_meta($variation->ID,"real_height", true );
if( ! $variation_real_height ) $variation_real_height = "";
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input( array(
'id' => 'real_length' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
'value' => $variation_real_length
) );
woocommerce_wp_text_input( array(
'id' => 'real_width' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
'value' => $variation_real_width
) );
woocommerce_wp_text_input( array(
'id' => '_circuit' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
'value' => $variation_real_height
) );
echo '</p>';
}
// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $loop ){
$the_actual_lenght = $_POST["actual_length_$loop"];
if( isset($the_actual_lenght) )
update_post_meta( $variation_id, 'the_actual_lenght', esc_attr($the_actual_lenght) );
$the_actual_width = $_POST["actual_thickness_$loop"];
if( isset($the_actual_width) )
update_post_meta( $variation_id, 'the_actual_width', esc_attr($the_actual_width) );
$the_actual_height = $_POST["actual_height_$loop"];
if( isset($the_actual_height) )
update_post_meta( $variation_id, 'the_actual_height', esc_attr($the_actual_height) );
}
I have two problems though. For one, the code seems to be a little flawed in that it does not seem to save the custom length/width/height fields under the product variation area. I have checked and the flaw is in the original post’s code too.
Secondly, the answer on that post does not detail how to code this so that the inputted information can show on the front end within the Additional Product Information table of Woocommerce product listings.
My question is: how do I alter this code to get it to save the product variation fields and to get the custom field values to display on the Woocommerce Additional Information Tab?
I have been working on this solution for days now and any help would be greatly appreciated.
2
Answers
I ended up paying someone to solve this for me but I figured that I would post the answer here. If anyone out there is looking for a solution to add a second set of dimensions to simple product shipping area and variations area and then have that display in the additional information tab, then here is the code solution:
This solution is particularly useful for anyone looking to have both "shipping dimensions" (which are provided by default by Woocommerce) and "product dimensions" which would be displayed to the customer.
In the meantime you have answered your question yourself (+1) but for the first part of your question:
This may be useful for you or other users
To add custom fields to the product shipping tab you can use:
To save fields you can use the
woocommerce_admin_process_product_object
hook, opposite the outdatedwoocommerce_process_product_meta
hook:So far adding and saving the fields to the product shipping tab.
To add and save the fields to the variations tab area, you can use the answer below. But, here are some remarks:
woocommerce_product_after_variable_attributes
hook is replaced bywoocommerce_variation_options_dimensions
as it is better positioned for your questionwoocommerce_admin_process_variation_object
replaces the outdatedwoocommerce_save_product_variation
form-row-last
toform-row-first
Result: