How to save WooCommerce checkout custom fields to user meta - wordpress

I've created the following custom fields for the user meta using the PODS.io Wordpress plugin:
date_of_birth
emergency_contact_name
relation
emergency_phone
I've added these fields to the WooCommerce checkout, under extra information, by using the following code in my theme's functions.php:
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
woocommerce_form_field( 'date_of_birth', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date of Birth'),
'placeholder' => __('dd/mm/yyyy'),
), $checkout->get_value( 'date_of_birth' ));
woocommerce_form_field( 'emergency_contact_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Emergency Contact Name'),
'placeholder' => __('contact name'),
), $checkout->get_value( 'emergency_contact_name' ));
woocommerce_form_field( 'relation', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Emergency Relation'),
'placeholder' => __('wife/husband'),
), $checkout->get_value( 'relation' ));
woocommerce_form_field( 'emergency_phone', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Emergency Phone'),
'placeholder' => __('xxxx xxx xxx / xxxx xxxx'),
), $checkout->get_value( 'emergency_phone' ));
}
Error checking:
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['date_of_birth'] )
wc_add_notice( __( 'Please enter your date of birth' ), 'error' );
if ( ! $_POST['emergency_contact_name'] )
wc_add_notice( __( 'Please enter your Emergency Contact Name' ), 'error' );
if ( ! $_POST['relation'] )
wc_add_notice( __( 'Please enter how your Emergency Contact is related to you' ), 'error' );
if ( ! $_POST['emergency_phone'] )
wc_add_notice( __( 'Please enter the phone number of your Emergency Contact' ), 'error' );
}
(Hopefully) update the user meta upon checkout:
add_action( 'woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( !empty( $_POST['date_of_birth'] ) ) {
$dob = sanitize_text_field( $_POST['date_of_birth'] );
update_user_meta( $current_user->ID, 'date_of_birth', $dob);
}
if ( ! empty( $_POST['emergency_contact_name'] ) ) {
update_user_meta( $user_id, 'emergency_contact_name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
}
if ( ! empty( $_POST['relation'] ) ) {
update_user_meta( $user_id, 'relation', sanitize_text_field( $_POST['relation'] ) );
}
if ( ! empty( $_POST['emergency_phone'] ) ) {
update_user_meta( $user_id, 'emergency_phone', sanitize_text_field( $_POST['emergency_phone'] ) );
}
}
Unfortunately, the user meta custom fields are not updated when I checkout.
I can update the order meta custom fields with the following code:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['date_of_birth'] ) ) {
update_post_meta( $order_id, 'Date Of Birth', sanitize_text_field( $_POST['date_of_birth'] ) );
}
if ( ! empty( $_POST['emergency_contact_name'] ) ) {
update_post_meta( $order_id, 'Emergency Contact Name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
}
if ( ! empty( $_POST['relation'] ) ) {
update_post_meta( $order_id, 'Emergency Relation', sanitize_text_field( $_POST['relation'] ) );
}
if ( ! empty( $_POST['emergency_phone'] ) ) {
update_post_meta( $order_id, 'Emergency Phone', sanitize_text_field( $_POST['emergency_phone'] ) );
}
}
However, we need the custom fields in the user meta, not the order meta.
Can you see what is wrong with the code that saves custom fields upon checkout to the user meta?
Thanks.

first, you should add your custom fields like this: (use woocommerce_checkout_fields filter)
function reigel_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['date_of_birth'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date of Birth'),
'placeholder' => __('dd/mm/yyyy'),
'required' => true,
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'reigel_woocommerce_checkout_fields' );
adding 'required' and setting it to true will have the same effect with how you check if this field is set or not. (your "Error checking")
then in your woocommerce_checkout_update_user_meta, the first parameter is not $order_id but the $customer_id. You should know too that the second parameter is $posted. $posted contains the $_POST[] data. Including your custom fields if you did the code above.
function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
if (isset($posted['date_of_birth'])) {
$dob = sanitize_text_field( $posted['date_of_birth'] );
update_user_meta( $customer_id, 'date_of_birth', $dob);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );

Related

Multiple SKU's for 1 item [duplicate]

We needed another field in our products for Prod ref/Cat numbers and I found the bit of code below which works perfectly.
I now need to add a second field for Nominal Codes used by the accountants software.
I tried using the below code again, adjusted for the new field, but it didn't work.
function jk_add_custom_sku() {
$args = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
function jk_save_custom_meta( $post_id ) {
// grab the SKU value
$sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the custom SKU meta field
$product->update_meta_data( 'jk_sku', $sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_meta' );
Adding extra fields can be done in a very simple way:
function jk_add_custom_sku() {
$args_1 = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args_1 );
// Extra field
$args_2 = array(
'label' => __( 'Nominal codes', 'woocommerce' ),
'placeholder' => __( 'Enter nominal codes here', 'woocommerce' ),
'id' => '_nominal_codes',
'desc_tip' => true,
'description' => __( 'This is for nominal codes.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args_2 );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
// Save
function jk_save_custom_meta( $product ){
if( isset($_POST['jk_sku']) ) {
$product->update_meta_data( 'jk_sku', sanitize_text_field( $_POST['jk_sku'] ) );
}
// Extra field
if( isset($_POST['_nominal_codes']) ) {
$product->update_meta_data( '_nominal_codes', sanitize_text_field( $_POST['_nominal_codes'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'jk_save_custom_meta', 10, 1 );

Woocommerce custom_fields(Shipping and Billing) in email_order_meta_fields

I'm trying to store custom shipping and billing fields in order and then to be print in emails ( admin & customer ) but with no success , is there anyone with big heart to help me ?
So as you can see i removed some fields , added other fields in Shipping & Billing then i try to store data of order and at the last i try to send my extra fields in order_email .
Here is my code in functions.php
// Replace billing_email with shipping_email " This because custome don recieve email notification if don't ask invoice "
add_action('woocommerce_checkout_update_order_meta', 'set_billing_email_from_shipping_email', 50, 2 );
function set_billing_email_from_shipping_email( $order_id, $data ) {
// Get customer shipping email
$email = get_post_meta( $order_id, '_shipping_email', true );
// Set billing email from shipping email
update_post_meta( $order_id, '_billing_email', $email );
}
// Remove fields
add_filter( 'woocommerce_checkout_fields', 'wc_remove_checkout_fields' );
function wc_remove_checkout_fields( $fields ) {
// Billing fields
unset( $fields['billing']['billing_first_name'] );
unset( $fields['billing']['billing_last_name'] );
unset( $fields['billing']['billing_address_2'] );
// Shipping fields
unset( $fields['shipping']['shipping_address_2'] );
unset( $fields['shipping']['shipping_company'] );
// Order fields
unset( $fields['order']['order_comments'] );
return $fields;
}
//Add custom fields
add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_fields' );
function custom_shipping_fields( $fields ) {
$fields['shipping_email'] = array(
'label' => __( 'Email', 'woocommerce' ),
'required' =>true,
'class' => array( 'form-row-last' ),
'validate' => array( 'email' ),
);
$fields['shipping_phone'] = array(
'label' => __( 'Telefono', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first' ),
'clear' => true,
'validate' => array( 'phone' ),
);
$fields['shipping_invoice'] =array(
'type' => 'checkbox',
'class' => array('form-row-four'),
'label' => __(' Fattura ?'),
'label_class' => array('woocommerce-form__label l_checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox i_checkbox'),
);
return $fields;
}
add_filter( 'woocommerce_billing_fields' , 'custom_billing_fields' );
function custom_billing_fields( $fields ) {
$fields['billing_vat'] = array(
'label' => __( 'P.Iva/CF', 'woocommerce' ),
'required' =>false,
'class' => array( 'form-row-last' ),
);
return $fields;
};
function custom_shipping_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['custom_shipping_fields'] ) ) {
update_post_meta( $order_id, '_custom_shipping_fields', sanitize_text_field( $posted['_custom_shipping_fields'] ) );
}
}
function custom_billing_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['custom_shipping_fields'] ) ) {
update_post_meta( $order_id, '_custom_shipping_fields', sanitize_text_field( $posted['_custom_shipping_fields'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'custom_save_extra_checkout_fields', 10, 2 );
function custom_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['custom_shipping_fields'] = array(
'label' => __( 'Custom Siping Fields' ),
'value' => get_post_meta( $order->id, '_custom_shipping_fields', true ),
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'custom_email_order_meta_fields', 10, 3 );
Thank you!

Display custom DOB fields in WooCommerce admin order edit pages

I've added this code to functions.php
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
add_action( 'woocommerce_before_order_notes', 'competitor_details' );
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
function competitor_details( $checkout ) {
echo '<div id="competitor_details"><h3>' . __('Competitor\'s Details') . '</h3>';
woocommerce_form_field( 'competitor_first_name',array('label' => 'First name', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_last_name',array('label' => 'Last name', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '<div id="competitor_details"><h4>' . __('Date of birth') . '</h4>';
woocommerce_form_field( 'competitor_dob_day',array('label' => 'Day', 'type' => 'select', 'required' => 'true', 'options' => array(''=>'','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15','16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20','21'=>'21','22'=>'22','23'=>'23','24'=>'24','25'=>'25','26'=>'26','27'=>'27','28'=>'28','29'=>'29','30'=>'30','31'=>'31'))
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_month',array('label' => 'Month', 'type' => 'select', 'required' => 'true', 'options' => array(''=>'','Jan'=>'Jan','Feb'=>'Feb','Mar'=>'Mar','Apr'=>'Apr','May'=>'May','Jun'=>'Jun','Jul'=>'Jul','Aug'=>'Aug','Sep'=>'Sep','Oct'=>'Oct','Nov'=>'Nov','Dec'=>'Dec'))
, $checkout->get_value( 'my_field_name' ));
woocommerce_form_field( 'competitor_dob_year',array('label' => 'Year', 'type' => 'text', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '<div id="consent_to_rules"><h4>' . __('I have read the Rules and agree to abide by them') . '</h4>';
woocommerce_form_field( 'consent_to_rules',array('label' => 'YES, I agree', 'type' => 'checkbox', 'required' => 'true')
, $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['competitor_first_name'] )
wc_add_notice( __( 'Please enter Competitors First Name' ), 'error' );
if ( ! $_POST['competitor_last_name'] )
wc_add_notice( __( 'Please enter Competitors Last Name' ), 'error' );
if ( ! $_POST['competitor_dob_day'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['competitor_dob_month'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['competitor_dob_year'] )
wc_add_notice( __( 'Please enter Competitors date of birth' ), 'error' );
if ( ! $_POST['consent_to_rules'] )
wc_add_notice( __( 'Please agree that you have read the Rules' ), 'error' );
}
That adds the required fields to the checkout page. I don't want a date picker, because the DOB can be anything really - it just has to be easy to enter.
The checkout works fine, but I don't see the field values when I'm checking the order details in the admin area.
Any help with that?
First of all, I've rewritten your current code, as it had some minor bugs
// Unset order comments field
function filter_woocommerce_checkout_fields( $fields ) {
unset( $fields['order']['order_comments'] );
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'filter_woocommerce_checkout_fields', 10, 1 );
// NOT checked by default
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );
// Add custom fields
function action_woocommerce_before_order_notes( $checkout ) {
echo '<h3>' . __( 'Competitor\'s Details', 'woocommerce' ) . '</h3>';
// First name
woocommerce_form_field( 'competitor_first_name', array(
'label' => __( 'First name', 'woocommerce' ),
'type' => 'text',
'required' => 'true'
), $checkout->get_value( 'competitor_first_name' ) );
// Last name
woocommerce_form_field( 'competitor_last_name', array(
'label' => __( 'Last name', 'woocommerce' ),
'type' => 'text',
'required' => 'true'
), $checkout->get_value( 'competitor_last_name' ) );
echo '<h4>' . __( 'Date of birth', 'woocommerce' ) . '</h4>';
// Options day
$options_day = array( '' => __( 'Please select a day', 'woocommerce' ) );
$from = 1;
$to = 31;
$options_day_combine = $options_day + array_combine( range( $from, $to ), range( $from, $to ) );
// Dob day
woocommerce_form_field( 'competitor_dob_day', array(
'label' => __( 'Day', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => $options_day_combine
), $checkout->get_value( 'competitor_dob_day' ) );
// Dob month
woocommerce_form_field( 'competitor_dob_month', array(
'label' => __( 'Month', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => array(
'' => __( 'Please select a month', 'woocommerce' ),
'Jan' => __( 'Jan', 'woocommerce' ),
'Feb' => __( 'Feb', 'woocommerce' ),
'Mar' => __( 'Mar', 'woocommerce' ),
'Apr' => __( 'Apr', 'woocommerce' ),
'May' => __( 'May', 'woocommerce' ),
'Jun' => __( 'Jun', 'woocommerce' ),
'Jul' => __( 'Jul', 'woocommerce' ),
'Aug' => __( 'Aug', 'woocommerce' ),
'Sep' => __( 'Sep', 'woocommerce' ),
'Oct' => __( 'Oct', 'woocommerce' ),
'Nov' => __( 'Nov', 'woocommerce' ),
'Dec' => __( 'Dec', 'woocommerce' ),
)
), $checkout->get_value( 'competitor_dob_month' ) );
// Options year
$options_year = array( '' => __( 'Please select a year', 'woocommerce' ) );
$from = 2021;
$to = 1910;
$options_year_combine = $options_year + array_combine( range( $from, $to ), range( $from, $to ) );
// Dob year
woocommerce_form_field( 'competitor_dob_year', array(
'label' => __( 'Year', 'woocommerce' ),
'type' => 'select',
'required' => 'true',
'options' => $options_year_combine
), $checkout->get_value( 'competitor_dob_year' ) );
echo '<h4>' . __( 'I have read the Rules and agree to abide by them', 'woocommerce' ) . '</h4>';
// Rules
woocommerce_form_field( 'consent_to_rules', array(
'label' => __( 'Yes, I agree', 'woocommerce' ),
'type' => 'checkbox',
'required' => 'true'
), $checkout->get_value( 'consent_to_rules' ) );
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
// Validate custom fields
function action_woocommerce_checkout_process() {
// Isset and empty, add an error
if ( isset( $_POST['competitor_first_name'] ) && empty( $_POST['competitor_first_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors First Name', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_last_name'] ) && empty( $_POST['competitor_last_name'] ) ) {
wc_add_notice( __( 'Please enter Competitors Last Name', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_day'] ) && empty( $_POST['competitor_dob_day'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - day', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_month'] ) && empty( $_POST['competitor_dob_month'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - month', 'woocommerce' ), 'error' );
}
if ( isset( $_POST['competitor_dob_year'] ) && empty( $_POST['competitor_dob_year'] ) ) {
wc_add_notice( __( 'Please enter Competitors date of birth - year', 'woocommerce' ), 'error' );
}
if ( ! isset( $_POST['consent_to_rules'] ) ) {
wc_add_notice( __( 'Please agree that you have read the Rules', 'woocommerce' ), 'error' );
}
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );
To answer your question, save and display
Use the woocommerce_checkout_create_order and woocommerce_admin_order_data_after_billing_address actions hooks.
This parts can be optimized with a foreach loop as opposed to adding an if condition every time, but I have not applied this so that everything stays clear.
So you get:
// Save fields
function action_woocommerce_checkout_create_order( $order, $data ) {
// Isset and NOT empty, save
if ( isset( $_POST['competitor_first_name'] ) && ! empty( $_POST['competitor_first_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_first_name', sanitize_text_field( $_POST['competitor_first_name'] ) );
}
if ( isset( $_POST['competitor_last_name'] ) && ! empty( $_POST['competitor_last_name'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_last_name', sanitize_text_field( $_POST['competitor_last_name'] ) );
}
if ( isset( $_POST['competitor_dob_day'] ) && ! empty( $_POST['competitor_dob_day'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_day', sanitize_text_field( $_POST['competitor_dob_day'] ) );
}
if ( isset( $_POST['competitor_dob_month'] ) && ! empty( $_POST['competitor_dob_month'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_month', sanitize_text_field( $_POST['competitor_dob_month'] ) );
}
if ( isset( $_POST['competitor_dob_year'] ) && ! empty( $_POST['competitor_dob_year'] ) ) {
// Update meta data
$order->update_meta_data( 'competitor_dob_year', sanitize_text_field( $_POST['competitor_dob_year'] ) );
}
if ( isset( $_POST['consent_to_rules'] ) ) {
// Update meta data
$order->update_meta_data( 'consent_to_rules', sanitize_text_field( $_POST['consent_to_rules'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
// Display the custom 'select' field value on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Get meta
$competitor_first_name = $order->get_meta( 'competitor_first_name' );
// NOT empty
if ( ! empty ( $competitor_first_name ) ) {
echo '<p><strong>' . __( 'Competitor first name', 'woocommerce' ) . ':</strong> ' . $competitor_first_name . '</p>';
}
// Get meta
$competitor_last_name = $order->get_meta( 'competitor_last_name' );
// NOT empty
if ( ! empty ( $competitor_last_name ) ) {
echo '<p><strong>' . __( 'Competitor last name', 'woocommerce' ) . ':</strong> ' . $competitor_last_name . '</p>';
}
// Get meta
$competitor_dob_day = $order->get_meta( 'competitor_dob_day' );
// NOT empty
if ( ! empty ( $competitor_dob_day ) ) {
echo '<p><strong>' . __( 'Competitor dob day', 'woocommerce' ) . ':</strong> ' . $competitor_dob_day . '</p>';
}
// Get meta
$competitor_dob_month = $order->get_meta( 'competitor_dob_month' );
// NOT empty
if ( ! empty ( $competitor_dob_month ) ) {
echo '<p><strong>' . __( 'Competitor dob month', 'woocommerce' ) . ':</strong> ' . $competitor_dob_month . '</p>';
}
// Get meta
$competitor_dob_year = $order->get_meta( 'competitor_dob_year' );
// NOT empty
if ( ! empty ( $competitor_dob_year ) ) {
echo '<p><strong>' . __( 'Competitor dob year', 'woocommerce' ) . ':</strong> ' . $competitor_dob_year . '</p>';
}
// Get meta
$consent_to_rules = $order->get_meta( 'consent_to_rules' );
// NOT empty
if ( ! empty ( $consent_to_rules ) ) {
echo '<p><strong>' . __( 'Yes, I agree', 'woocommerce' ) . '</strong></p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

Wordpress Woocommerce custom field

I'm trying to create custom field in woo-commerce (not interested in plugins) in single product and variable product ( custom field price should change according to selected variable option) as that client should be able to enter price as show in 1st picture and customer could be able check the option.
My Requirement
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'Enter your choose', 'woocommerce' ),
'placeholder' => 'Custom text field',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field',
'label' => __( 'Enter your number', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '15'
)
)
);
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox',
'label' => __('Select', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' )
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'option', 'woocommerce' ),
'options' => array(
'1' => __( 'Custom Option 1', 'woocommerce' ),
'2' => __( 'Custom Option 2', 'woocommerce' ),
'3' => __( 'Custom Option 3', 'woocommerce' )
)
)
);
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Description', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
function woocommerce_template_top_category_desc (){
$terms = get_the_terms( $post->ID, 'wc-attibute-class' );
if ( !empty($terms)) {
$term = array_pop($terms);
$text= get_field('txt-field', $term);
if (!empty($text)) {
echo $text;
}
}
}
/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Text Field
$text_field = $_POST['_text_field'];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
// Save Number Field
$number_field = $_POST['_number_field'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
}
// Save Textarea
$textarea = $_POST['_textarea'];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_html( $textarea ) );
}
// Save Select
$select = $_POST['_select'];
if( ! empty( $select ) ) {
update_post_meta( $post_id, '_select', esc_attr( $select ) );
}
// Save Checkbox
$checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $checkbox );
// Save Hidden field
$hidden = $_POST['_hidden_field'];
if( ! empty( $hidden ) ) {
update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
}
}
I have tried some woocommerce custom plugin but it didn't solve my requirement as I have 25 above custom filed to be created and plugin seems to be very lengthy process for my work
My output is in picture 2 which I got from coding for not as my requirement
My output
I know you are not interested in plugins, but I highly recommend ACF plugin for custom fields. Check it out, it will save you a lot of time.

Custom fields in WooCommerce product variations

QUESTION UPDATED AS PEOPLE SEEMED TO MISUNDERSTAND IT:
Using the WooCommerce plugin for WordPress, I'd like to display the product variation names in the Additional Information tab in the same way as weight and dimesions are displayed.
For instance, I have a product that comes in two sizes, 2 litres and 10 litres, therefore it's a variable product with the two product variations '2 litres' and '10 litres'. If I check the box 'display on product page', size is displayed in the Additional Information tab like this: 'Size: 2 litres, 10 litres'.
I want it to work like the weight and dimensions, so when the product variation '2 litres' is selected, the Additional Information tab will display 'Size: 2 litres', and when the product variation '10 litres' is selected, the Additional Information tab will display 'Size: 10 litres'.
Can this be done?
Here is the full code to add all types of custom input fields for Product Variations:
<?php
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes','variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field[' . $variation->ID . ']',
'label' => __( 'My Number Field', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the custom number here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_number_field', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea[' . $variation->ID . ']',
'label' => __( 'My Textarea', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_select[' . $variation->ID . ']',
'label' => __( 'My Select Field', 'woocommerce' ),
'description' => __( 'Choose a value.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_select', true ),
'options' => array(
'one' => __( 'Option 1', 'woocommerce' ),
'two' => __( 'Option 2', 'woocommerce' ),
'three' => __( 'Option 3', 'woocommerce' )
)
)
);
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox[' . $variation->ID . ']',
'label' => __('My Checkbox Field', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_checkbox', true ),
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field[' . $variation->ID . ']',
'value' => 'hidden_value'
)
);
}
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
// Number Field
$number_field = $_POST['_number_field'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
}
// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}
// Select
$select = $_POST['_select'][ $post_id ];
if( ! empty( $select ) ) {
update_post_meta( $post_id, '_select', esc_attr( $select ) );
}
// Checkbox
$checkbox = isset( $_POST['_checkbox'][ $post_id ] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $checkbox );
// Hidden field
$hidden = $_POST['_hidden_field'][ $post_id ];
if( ! empty( $hidden ) ) {
update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
}
}
?>
To get those values on the frontend we just need to use the popular get_post_meta() function.
Reference article at here:
http://www.remicorson.com/woocommerce-custom-fields-for-variations/
Here's a first attempt. There's not a lot to go on in the attributes table, but when an attribute dropdown is changed, it will find the attribute in the additional information tab (where it is listed in default WooCommerce) and change the text to match the label in the dropdown. A problem occurs when the customer switches back to the empty "choose" option, so you'd need to expand this to somehow account for that.
jQuery( document ).ready(function($) {
$( ".variations_form" ).on( 'change', '.variations select', function() {
id = $(this).attr('id');
attribute = toTitleCase( $(this).attr('id').replace("pa_", "") );
selected = $(this).find('option:selected').text();
$('#tab-additional_information').find('.shop_attributes th:contains('+attribute+')').next('td').text(selected);
});
function toTitleCase(str){
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
});
Save the above as somescript.js in your theme's folder. Then add the following to functions.php to properly enqueue the script.
/**
* Proper way to enqueue scripts and styles
*/
function theme_name_scripts() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/somescript.js', array('jquery'), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Resources