On Woocommerce, with the code below I have been able to add a custom field inside my general product options tabs with a textbox option. I also need to display this custom field on my order pages in a custom column in my order items detail page (the page in my admin area where I can see which products my customer bought).
Here is my code:
// Add custom column headers here
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
$column_name = 'Plek';
echo '<th data-sort="float" style="text-align: right;" >' . $column_name . '</th>';
}
// Add custom column values here
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
// get the post meta value from the associated product
// $value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
$value = array_shift( wc_get_product_terms( $_product->post->ID, 'locatie', array( 'fields' => 'names' ) ) );
echo '<td>' . $value . '</td>';
}
// Display order notes Fields
add_action( 'woocommerce_product_options_general_product_data', 'pans_woo_add_custom_general_fields' );
function pans_woo_add_custom_general_fields() { global $woocommerce, $post;
echo '<div class="pans_ta_options_group">';
woocommerce_wp_textarea_input( array(
'id' => 'plek',
'label' => __( 'plek', 'woocommerce' ),
'placeholder' => 'Alleen zichtbaar voor Admins.',
) );
echo '</div>';
}
// Save order note Fields
add_action( 'woocommerce_process_product_meta', 'pans_woo_add_custom_general_fields_save' );
function pans_woo_add_custom_general_fields_save( $post_id ){
$woocommerce_textarea = $_POST['plek'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, 'plek', sanitize_textarea_field( $woocommerce_textarea ) );
}
The Product custom field value is displayed and saved correctly inside my general product options tabs (Product admin pages).
I also managed to get the column name done inside the order detail page.
But I am not able to display this product custom field value. Can anyone please give me some suggestions or help how to fix this?
What am I doing wrong?
2
Answers
I Have revisited your code as there are some errors and some outdated things in your code (Since WooCommerce 3). So try instead the following that will display your ‘Plek’ product custom field value on admin order pages:
Code goes in function.php file of your active child theme (or active theme). Tested and works
The code you have looks mostly correct, but there are a couple of potential issues. Here are some suggestions to troubleshoot the problem:
Verify the meta key: Make sure that the meta key used to store the custom field value matches in all instances. In your code, you’re using the meta key ‘plek’. Double-check that this key is consistent throughout the code.
Check the taxonomy and terms: It seems that you’re attempting to retrieve a term from the ‘locatie’ taxonomy and display it as the custom field value. Ensure that the ‘locatie’ taxonomy exists and has the desired terms associated with your products. If the taxonomy or terms are different, adjust the code accordingly.
Clear cache: If you’ve made changes to the code and they are not reflected, clear any caching plugins or caching mechanisms you have in place. This ensures that you are viewing the most up-to-date version of the page.
If you’ve confirmed the above points and the issue persists, it would be helpful to know what exactly is not working as expected. Are you not seeing the custom field in the product edit page, or is the value not being displayed in the order details page? Additionally, are there any error messages or unexpected behavior occurring?
Providing more specific details or any error messages encountered would help in identifying the problem and providing a more accurate solution.