Lesson 3.2. Custom Fields and Product Type

Tuesday, 3:10 PM

A manager saves a product in the admin: the "Supplier code" is there.

At night the ERP updates stock for the same product through the REST API. In the morning the code is empty — on all 2,000 products.

Your save_post handler reads the value from $_POST. A REST request has an empty $_POST, so the code dutifully writes an empty string over the real one.

Goal

After the lesson you add product and variation fields through WooCommerce hooks and CRUD, and they never disappear, wherever the product is saved from: admin, REST, import.

Key concepts

  • General tab fields: the woocommerce_product_options_general_product_data hook + woocommerce_wp_text_input()
  • Saving: woocommerce_admin_process_product_object — you get a WC_Product, not an ID
  • A custom type: a class extends WC_Product + the product_type_selector and woocommerce_product_class filters
  • A variation field: woocommerce_product_after_variable_attributes + woocommerce_save_product_variation
  • Validate and sanitize on save, escape on output

Diagram

sequenceDiagram
  participant A as Admin
  participant F as Product form
  participant H as woocommerce_admin_process_product_object
  participant P as WC_Product
  A->>F: fills in "Supplier code"
  F->>H: POST
  H->>P: update_meta_data(sanitize_text_field(...))
  P->>P: save() through the data store

Code

The "Supplier code" field:

PHP
<?php
add_action( 'woocommerce_product_options_general_product_data', static function () {
	woocommerce_wp_text_input( [
		'id'          => '_supplier_code',
		'label'       => __( 'Supplier code', 'my-plugin' ),
		'desc_tip'    => true,
		'description' => __( 'Used for ERP sync.', 'my-plugin' ),
	] );
} );

add_action( 'woocommerce_admin_process_product_object', static function ( WC_Product $product ) {
	if ( isset( $_POST['_supplier_code'] ) ) { // the WooCommerce product form checks the nonce
		$product->update_meta_data( '_supplier_code', sanitize_text_field( wp_unslash( $_POST['_supplier_code'] ) ) );
	}
} );

A custom "Gift card" product type:

PHP
<?php
add_action( 'woocommerce_loaded', static function () {
	class WC_Product_Gift_Card extends WC_Product_Simple {
		public function get_type() {
			return 'gift_card';
		}
		public function is_virtual() {
			return true;
		}
	}
} );

add_filter( 'product_type_selector', static function ( array $types ): array {
	$types['gift_card'] = __( 'Gift card', 'my-plugin' );
	return $types;
} );

add_filter( 'woocommerce_product_class', static function ( string $class, string $type ): string {
	return 'gift_card' === $type ? 'WC_Product_Gift_Card' : $class;
}, 10, 2 );

Common mistakes

SymptomCauseFix
The value is wiped by REST or import savesSaving via save_post + update_post_meta from $_POSTwoocommerce_admin_process_product_object and CRUD, only when the field was sent
XSS in the field outputOutput without esc_html()Escape on output
The custom type has no "Price" tabTabs are shown by show_if_simple classesAdd a show_if_gift_card class via admin JS or the tabs filter

Weak and strong answer

Question: How do you save a custom product field?

Weak answer. On save_post with update_post_meta.

Strong answer. On woocommerce_admin_process_product_object: the hook gets a WC_Product, I call update_meta_data() with sanitizing, and WooCommerce calls save() itself. The field goes through the data store and the save hooks, and REST or imports don't break it.

Why the second is stronger: The candidate uses the WooCommerce hook, not the generic WordPress one, and explains why.

Practice

Fix the plugin from the start of the lesson in the IDE below: the field must save from the product form and survive REST updates. Verify it with Run.

On your own site, add the same field to each variation (woocommerce_product_after_variable_attributes + woocommerce_save_product_variation) and check that it appears in the REST API meta_data.

PHP

Interview Questions

save_post fires for any post and before WooCommerce saves its own data. woocommerce_admin_process_product_object gives you the product object and makes your changes part of the main save.

A _ prefix doesn't protect it from REST: meta_data returns all meta. Filter it with woocommerce_rest_prepare_product_object, or don't keep sensitive data in product meta.

When the product behaves differently: pricing, cart, shipping, post-payment handling. If only a few fields change, meta fields are enough.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.