I’ve seen similar questions asked, and have tried all the solutions, but none of them seems to work for my project.
All my products have extra specifications, that needs to change when the customer choose a different variation on the product page. And I need the specifications to be displayed in the products attributes table So far, I’ve tried a couple of different things, but my code so far looks like this. I can get the extra data to save from the edit product page, but I can’t figure out how to get it to display on the product page.
Functions.php
// 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 ){
woocommerce_wp_text_input( array(
'id' => 'diameter[' . $loop . ']',
'class' => 'short',
'label' => __( 'Diameter', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Diameter description help.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'diameter', true )
) );
woocommerce_wp_text_input( array(
'id' => 'thickness[' . $loop . ']',
'class' => 'short',
'label' => __( 'Thickness', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Thickness description help.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'thickness', true )
) );
}
// 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, $i ) {
$diameter = $_POST["diameter"][$i];
if ( isset( $diameter ) )
update_post_meta( $variation_id, 'diameter', esc_attr( $diameter ) );
$thickness = $_POST["thickness"][$i];
if ( isset( $thickness ) )
update_post_meta( $variation_id, 'thickness', esc_attr( $thickness ) );
}
I’ve tried displaying the values in “product-attributes.php” and “additional-information.php” seeing as I want the new values to be displayed along side the original attributes.
My product-attributes-php
<?php echo get_post_meta( $product->get_id(), 'diameter', true ); ?>
<?php echo get_post_meta( $product->get_id(), 'thickness', true ); ?>
Is there anyway to display these values in either the product attribute table or right after it in the additional information tab, on the product page?
Sorry in advance if my question is dumb, it's my first time posting. And I just can't get it working