WooCommerce Custom Fields & Attributes
Before writing code
Fetch live docs:
- Web-search
site:developer.woocommerce.com product data custom fields for product custom fields guide
- Web-search
site:developer.wordpress.org plugins metadata for WordPress metadata API
- Web-search
woocommerce custom product attributes programmatically for attribute creation
Product Attributes
Global Attributes
Defined in WooCommerce > Attributes — shared across all products:
- Stored as custom taxonomies:
pa_{attribute_slug} (e.g., pa_color, pa_size)
- Terms are the possible values (Red, Blue, Small, Large)
- Can be used for filtering, variations, and layered navigation
- Managed via
wc_create_attribute(), wc_update_attribute(), wc_delete_attribute()
Custom (Local) Product Attributes
Per-product attributes — not global taxonomies:
- Stored in product meta as part of
_product_attributes array
- Visible on the product page under "Additional Information" tab
- Set via
$product->set_attributes() with WC_Product_Attribute objects
Setting Attributes Programmatically
Create WC_Product_Attribute objects:
set_id() — 0 for custom attributes, taxonomy ID for global
set_name() — taxonomy name (pa_color) or custom label
set_options() — array of term IDs (global) or string values (custom)
set_visible() — show on product page
set_variation() — used for variations
Product Meta
Core Meta Keys
| Key |
Description |
_price |
Current active price |
_regular_price |
Regular price |
_sale_price |
Sale price |
_sku |
Stock Keeping Unit |
_stock |
Stock quantity |
_stock_status |
instock / outofstock / onbackorder |
_weight, _length, _width, _height |
Dimensions |
_virtual, _downloadable |
Product type flags |
Custom Meta
$product->update_meta_data( '_my_custom_field', $value ) then $product->save()
$product->get_meta( '_my_custom_field' ) — retrieve value
- Prefix custom keys with
_ to hide from the Custom Fields meta box
Adding Product Edit Fields
Use hooks to add fields to the product edit screen:
woocommerce_product_options_general_product_data — General tab
woocommerce_product_options_inventory_product_data — Inventory tab
woocommerce_product_options_shipping_product_data — Shipping tab
woocommerce_product_options_advanced — Advanced tab
- Save with:
woocommerce_process_product_meta action
Field Rendering Functions
woocommerce_wp_text_input( $args ) — text input
woocommerce_wp_textarea_input( $args ) — textarea
woocommerce_wp_select( $args ) — dropdown select
woocommerce_wp_checkbox( $args ) — checkbox
woocommerce_wp_hidden_input( $args ) — hidden field
Custom Product Tabs
Adding to Product Page
Filter woocommerce_product_tabs to add, remove, or reorder tabs:
- Return array with
title, priority, callback keys
- Remove default tabs by unsetting keys:
description, additional_information, reviews
Adding to Product Edit (Admin)
Filter woocommerce_product_data_tabs to add tabs in the product editor:
- Return array with
label, target, class, priority
- Render panel content via
woocommerce_product_data_panels action
Order Meta
$order->update_meta_data( '_my_field', $value ) then $order->save()
$order->get_meta( '_my_field' ) — retrieve value
- HPOS-compatible: always use CRUD methods, never
update_post_meta()
Customer Meta
$customer->update_meta_data() / $customer->get_meta() via WC_Customer
- Or
update_user_meta() / get_user_meta() for WordPress user meta
Custom Taxonomies
Registering Custom Taxonomies
Use register_taxonomy() on init hook:
- Associate with
product post type for product classification
- Set
show_in_rest for REST API / block editor support
- Hierarchical (like categories) or flat (like tags)
Best Practices
- Use global attributes for values shared across products (color, size, brand)
- Use custom meta for product-specific data not used in filtering
- Prefix custom meta keys with underscore to hide from the generic meta box
- Always use CRUD methods (
update_meta_data / get_meta) for HPOS compatibility
- Sanitize all input before saving:
sanitize_text_field(), wc_clean(), absint()
- Escape all output:
esc_html(), esc_attr(), wp_kses_post()
Fetch the WooCommerce product data and metadata documentation for exact hook names, field function signatures, and attribute API before implementing.
1---2name: woo-custom-fields3description: Work with WooCommerce product attributes, custom meta fields, taxonomies, custom product tabs, and variation data. Use when adding custom data to products, orders, or customers.4---5
6# WooCommerce Custom Fields & Attributes
7
8## Before writing code
9
10**Fetch live docs**:
111. Web-search `site:developer.woocommerce.com product data custom fields` for product custom fields guide
122. Web-search `site:developer.wordpress.org plugins metadata` for WordPress metadata API
133. Web-search `woocommerce custom product attributes programmatically` for attribute creation
14
15## Product Attributes
16
17### Global Attributes
18
19Defined in WooCommerce > Attributes — shared across all products:
20- Stored as custom taxonomies: `pa_{attribute_slug}` (e.g., `pa_color`, `pa_size`)
21- Terms are the possible values (Red, Blue, Small, Large)
22- Can be used for filtering, variations, and layered navigation
23- Managed via `wc_create_attribute()`, `wc_update_attribute()`, `wc_delete_attribute()`
24
25### Custom (Local) Product Attributes
26
27Per-product attributes — not global taxonomies:
28- Stored in product meta as part of `_product_attributes` array
29- Visible on the product page under "Additional Information" tab
30- Set via `$product->set_attributes()` with `WC_Product_Attribute` objects
31
32### Setting Attributes Programmatically
33
34Create `WC_Product_Attribute` objects:
35- `set_id()` — 0 for custom attributes, taxonomy ID for global
36- `set_name()` — taxonomy name (`pa_color`) or custom label
37- `set_options()` — array of term IDs (global) or string values (custom)
38- `set_visible()` — show on product page
39- `set_variation()` — used for variations
40
41## Product Meta
42
43### Core Meta Keys
44
45| Key | Description |
46|-----|-------------|
47| `_price` | Current active price |
48| `_regular_price` | Regular price |
49| `_sale_price` | Sale price |
50| `_sku` | Stock Keeping Unit |
51| `_stock` | Stock quantity |
52| `_stock_status` | instock / outofstock / onbackorder |
53| `_weight`, `_length`, `_width`, `_height` | Dimensions |
54| `_virtual`, `_downloadable` | Product type flags |
55
56### Custom Meta
57
58- `$product->update_meta_data( '_my_custom_field', $value )` then `$product->save()`
59- `$product->get_meta( '_my_custom_field' )` — retrieve value
60- Prefix custom keys with `_` to hide from the Custom Fields meta box
61
62### Adding Product Edit Fields
63
64Use hooks to add fields to the product edit screen:
65- `woocommerce_product_options_general_product_data` — General tab
66- `woocommerce_product_options_inventory_product_data` — Inventory tab
67- `woocommerce_product_options_shipping_product_data` — Shipping tab
68- `woocommerce_product_options_advanced` — Advanced tab
69- Save with: `woocommerce_process_product_meta` action
70
71### Field Rendering Functions
72
73- `woocommerce_wp_text_input( $args )` — text input
74- `woocommerce_wp_textarea_input( $args )` — textarea
75- `woocommerce_wp_select( $args )` — dropdown select
76- `woocommerce_wp_checkbox( $args )` — checkbox
77- `woocommerce_wp_hidden_input( $args )` — hidden field
78
79## Custom Product Tabs
80
81### Adding to Product Page
82
83Filter `woocommerce_product_tabs` to add, remove, or reorder tabs:
84- Return array with `title`, `priority`, `callback` keys
85- Remove default tabs by unsetting keys: `description`, `additional_information`, `reviews`
86
87### Adding to Product Edit (Admin)
88
89Filter `woocommerce_product_data_tabs` to add tabs in the product editor:
90- Return array with `label`, `target`, `class`, `priority`
91- Render panel content via `woocommerce_product_data_panels` action
92
93## Order Meta
94
95- `$order->update_meta_data( '_my_field', $value )` then `$order->save()`
96- `$order->get_meta( '_my_field' )` — retrieve value
97- HPOS-compatible: always use CRUD methods, never `update_post_meta()`
98
99## Customer Meta
100
101- `$customer->update_meta_data()` / `$customer->get_meta()` via `WC_Customer`
102- Or `update_user_meta()` / `get_user_meta()` for WordPress user meta
103
104## Custom Taxonomies
105
106### Registering Custom Taxonomies
107
108Use `register_taxonomy()` on `init` hook:
109- Associate with `product` post type for product classification
110- Set `show_in_rest` for REST API / block editor support
111- Hierarchical (like categories) or flat (like tags)
112
113## Best Practices
114
115- Use global attributes for values shared across products (color, size, brand)
116- Use custom meta for product-specific data not used in filtering
117- Prefix custom meta keys with underscore to hide from the generic meta box
118- Always use CRUD methods (`update_meta_data` / `get_meta`) for HPOS compatibility
119- Sanitize all input before saving: `sanitize_text_field()`, `wc_clean()`, `absint()`
120- Escape all output: `esc_html()`, `esc_attr()`, `wp_kses_post()`
121
122Fetch the WooCommerce product data and metadata documentation for exact hook names, field function signatures, and attribute API before implementing.