Creating custom fields: User fields, Product fields, Checkout fields, Order fields

You are here:
Estimated reading time: 1 min

The WooCommerce Custom Fields plugin is bundled in Rey.

Note: Please know this plugin is different in purpose and functionality versus ACF (Advanced Custom Fields).

WooCommerce Custom Fields allows you to create custom product, checkout, order and user fields, provide and gather additional information in a structured way, and sell configurable products, product add-ons and extra product options. Build, manage and optimize your online store for a premium user experience, and make every field work for you.

To install it, head over to Rey > Plugins Manager and install it from there eg: https://d.pr/i/ds5w1t . You can create the field types you want afterwards

You can find out more on how to Manage the fields, but for more informations and documentation, you can head over to its Knowledge Base , but please know that for personalised support you will need to purchase your own license for it.

Creating just a single text field without installing a plugin

To add a custom text field before the Add to cart button in the product page, display it on the cart page, and save it in the order, please copy the code below and paste it into the child theme’s functions.php and customize for your needs:

add_action('init', function(){
	// Replace the text label
	function custom__get_text_label(){
		return 'The text label';
	}
	// Replace the field unique key. Make sure to avoid adding spaces or symbols.
	function custom__get_field_key(){
		return 'custom_field_key';
	}
	// Add the custom text field before the Add to cart button
	add_action('woocommerce_before_add_to_cart_button', function () {
		printf('<p class="custom-text-field-wrapper">
			<label for="%2$s">%1$s</label>
			<input type="text" name="%2$s" id="%2$s" />
		</p>', custom__get_text_label(), custom__get_field_key() );
	});
	// Save the custom text field value when the product is added to the cart
	add_filter('woocommerce_add_cart_item_data', function ($cart_item_data, $product_id, $variation_id) {
		$field_key = custom__get_field_key();
		if (!empty($_POST[$field_key])) {
			$cart_item_data[$field_key] = reycore__clean($_POST[$field_key]);
		}
		return $cart_item_data;
	}, 10, 3);
	// Display the custom text field value on the cart page
	add_filter('woocommerce_get_item_data', function ($item_data, $cart_item) {
		$field_key = custom__get_field_key();
		if (isset($cart_item[$field_key])) {
			$item_data[] = array(
				'key' => custom__get_text_label(),
				'value' => $cart_item[$field_key]
			);
		}
		return $item_data;
	}, 10, 2);
	// Save the custom text field value as order item meta
	add_action('woocommerce_checkout_create_order_line_item', function ($item, $cart_item_key, $values, $order) {
		$field_key = custom__get_field_key();
		if (isset($values[$field_key])) {
			$item->add_meta_data($field_key, reycore__clean($values[$field_key]));
		}
	}, 10, 4);
	// Display the custom text field value in the admin order details
	add_action('woocommerce_admin_order_item_meta_start', function ($item_id, $item, $product) {
		$value = wc_get_order_item_meta($item_id, custom__get_field_key(), true);
		if ($value) {
			printf('<p><strong>%s:</strong> %s</p>', custom__get_text_label(), $value );
		}
	}, 10, 3);
	// Display the custom text field value in the customer order details
	add_action('woocommerce_display_item_meta', function ($html, $item, $args) {
		return str_replace(custom__get_field_key(), custom__get_text_label(), $html);
	}, 10, 3);
});
Was this article helpful?
Dislike 1
Views: 1822

Suggest article improvements

Please use this form to suggest improvements and report missing or outdated content. Support requests will most likely not be answered and it's best to use the Support Request Form instead. Thanks!