Quote from Richard on October 30, 2020, 00:36
Hi, we have added a custom field to product variation via the functions.php and we would like to add this as a column in the editor.
We tried adding the custom field manually in the "Meta Fields" tab as it does not show when we query meta keys by product ID - no luck, did not work.
Here is what we added to the functions.php, we tried using both the "id" and "label" as the key. It displays as a separate column in the editor however, when we apply content to this column it does not reflect in the product variations. So we believe there is a key mis-match or something.
// 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );
function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => '_media_variation_product_id[' . $loop . ']',
'class' => 'short',
'label' => __( 'Media product Id', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_media_variation_product_id', true )
)
);
}
Hi, we have added a custom field to product variation via the functions.php and we would like to add this as a column in the editor.
We tried adding the custom field manually in the"Meta Fields" tab as it does not show when we query meta keys by product ID - no luck, did not work.
Here is what we added to the functions.php, we tried using both the"id" and"label" as the key. It displays as a separate column in the editor however, when we apply content to this column it does not reflect in the product variations. So we believe there is a key mis-match or something.
// 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );
function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => '_media_variation_product_id[' . $loop . ']',
'class' => 'short',
'label' => __( 'Media product Id', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_media_variation_product_id', true )
)
);
}