WooCommerce product custom fields are possibly the most used customization from what I’ve seen over the years on clients’ websites.
Adding custom fields to the product backend is pretty straight forward (including the input fields to be used for editing their values), however there are two additional areas where you need to do more work in order to allow for custom field editing: the “Quick Edit” and the “Bulk Edit” sections (WordPress Dashboard > Products).
We already saw how to allow a new custom field to appear in the Bulk Edit section, so this time we’ll talk about the Quick Edit window. So, how do we add a custom field in there (WordPress Dashboard > Products > Hover on a given product > Quick Edit)?
Well, here’s a fully working snippet for you. Enjoy!
PHP Snippet: Add Custom Field to Quick Edit @ WooCommerce Products Admin
Please note that inside the snippet you need to replace “_custom_field” with the actual key of your custom field. Given you’ve probably added the custom field with a plugin, you should find its key inside the field settings. If you added it via code, then you’ve defined this key yourself.
/**
* @snippet Add Custom Field @ WooCommerce Quick Edit Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_product_quick_edit_start', 'bbloomer_show_custom_field_quick_edit' );
function bbloomer_show_custom_field_quick_edit() {
?>
<label>
<span class="title">Custom field</span>
<span class="input-text-wrap">
<input type="text" name="_custom_field" class="text" value="">
</span>
</label>
<br class="clear" />
<?php
}
add_action( 'manage_product_posts_custom_column', 'bbloomer_show_custom_field_quick_edit_data', 9999, 2 );
function bbloomer_show_custom_field_quick_edit_data( $column, $post_id ){
if ( 'name' !== $column ) return;
echo '<div>Custom field: <span id="cf_' . $post_id . '">' . esc_html( get_post_meta( $post_id, '_custom_field', true ) ) . '</span></div>';
wc_enqueue_js( "
$('#the-list').on('click', '.editinline', function() {
var post_id = $(this).closest('tr').attr('id');
post_id = post_id.replace('post-', '');
var custom_field = $('#cf_' + post_id).text();
$('input[name=\'_custom_field\']', '.inline-edit-row').val(custom_field);
});
" );
}
add_action( 'woocommerce_product_quick_edit_save', 'bbloomer_save_custom_field_quick_edit' );
function bbloomer_save_custom_field_quick_edit( $product ) {
$post_id = $product->get_id();
if ( isset( $_REQUEST['_custom_field'] ) ) {
$custom_field = $_REQUEST['_custom_field'];
update_post_meta( $post_id, '_custom_field', wc_clean( $custom_field ) );
}
}
add_action( ‘woocommerce_product_quick_edit_start’, ‘bbloomer_show_custom_field_quick_edit’ );
function bbloomer_show_custom_field_quick_edit() {
global $post;
Here, global $post returns the same post for all the products in the list. how can i get individual product id in the list?
Hello SF, what do you need that for exactly?
Great article helped me a lot.
I had to make a small modification though. Because I have more than one custom fields that I want to appear in quick edit, I had to change the id of each of my custom fields from ‘#cf_’ + post_id to ‘#cf_[something]’ + post_id and had to have that different for each individual custom field. Hope that helps someone.
Also is there a way to have radio button-based custom fields appear in the quick edit? How do you set up that?
Thank you for your feedback! In regard to the last question, I have no idea – but I don’t see why that wouldn’t be possible