Hot Rock Hosting Managed WordPress

Support: 1-813-433-0922‬

Woocommerce add or update field

To add a field or change a label in woocommerce, you must create a child theme and add the following code to your function.php file. First function is optional, as it just calls the parent theme’s css in to your theme.

<?php

// enqueue parent styles
function example_enqueue_styles() {
 wp_enqueue_style('classicrgb-lite', get_template_directory_uri() .'/style.css');
}
add_action('wp_enqueue_scripts', 'example_enqueue_styles');


// Overide label
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
 $fields['order']['order_comments']['placeholder'] = '';
 $fields['order']['order_comments']['label'] = 'Card Message';
 return $fields;
}


// Add field to checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
 echo '<div id="my_custom_checkout_field">';
 woocommerce_form_field( 'delivery_date', array(
 'type' => 'text',
 'class' => array('my-field-class form-row-wide'),
 'label' => __('Delivery Date'),
 'placeholder' => __(' / / '),
 ), $checkout->get_value( 'delivery_date' ));
 echo '</div>';
}


// Save new field value
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['delivery_date'] ) ) {
 update_post_meta( $order_id, 'Delivery Date', sanitize_text_field( $_POST['delivery_date'] ) );
 }
}


// Display new field value on the Admin order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
 echo '<p><strong>'.__('Delivery Date').':</strong> ' . get_post_meta( $order->id, 'Delivery Date', true ) . '</p>';
}


?>

 

Get Started with a WooCommerce WordPress Website

credentials