WooCommerce: Add Hidden Custom Field

Do you want a simple field in your WooCommerce backend (the product edit page) where you can write down the product’s physical location (like “Closet A, Shelf 3”, “Living Room”, etc.)? Luckily, this is very easy to do!

Where This Applies

Let’s say you’re selling stickers in your own webshop for example. All of your stickers are stacked in boxes on several shelves. Instead of going through all the boxes to check which sticker was stored in what box, what you would do, is give each box a label. Then you add that label when adding your sticker to your WooCommerce shop. So when a customer purchases a sticker, all you need is the info from that custom field – not made visible to the public – to easily find your sticker on your shelf.

How to Add a Hidden Custom Field

We’re going to do this step-by-step using Code Snippets:

  • Install & activate the Code Snippets plugin if you haven’t already.
  • Go to Snippets → Add New.
  • Give it a title like: Product Location Field.
  • Paste the following code:


// Add the custom field to the product edit page
add_action('woocommerce_product_options_general_product_data', function() {
    woocommerce_wp_text_input([
        'id'          => '_product_location',
        'label'       => __('Product Location', 'woocommerce'),
        'desc_tip'    => 'true',
        'description' => __('Where is the product stored in your home?', 'woocommerce')
    ]);
});

// Save the custom field
add_action('woocommerce_process_product_meta', function($post_id) {
    $product_location = isset($_POST['_product_location']) ? sanitize_text_field($_POST['_product_location']) : '';
    update_post_meta($post_id, '_product_location', $product_location);
});

  • Set it to “Run snippet everywhere”.
  • Click Activate and Save.

Now when you go edit a product in WooCommerce, you’ll see your new “Product Location” field.

So there you go! Now you now how to add a hidden custom field in WooCommerce!

Mari's Note

♥ I hope my post was useful to you! If this post helped you in any way, please consider supporting me by making a small donation via my Ko-Fi page!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply