/** * Changes the redirect URL for the Return To Shop button in the cart. */ function wc_empty_cart_redirect_url() { return 'http://example.url/category/specials/'; } add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url', 10 );Adding fields is done in a similar way to overriding fields. For example, let’s add a new field to shipping fields – shipping_phone:
// Hook in add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); // Our hooked in function - $fields is passed via the filter! function custom_override_checkout_fields( $fields ) { $fields['shipping']['shipping_phone'] = array( 'label' => __('Phone', 'woocommerce'), 'placeholder' => _x('Phone', 'placeholder', 'woocommerce'), 'required' => false, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; } /** * Display field value on the order edit page */ add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); function my_custom_checkout_field_display_admin_order_meta($order){ echo 'What do we do with the new field? Nothing. Because we defined the field in the checkout_fields array, the field is automatically processed and saved to the order post meta (in this case, _shipping_phone). If you want to add validation rules, see the checkout class where there are additional hooks you can use. Adding a Custom Special Field To add a custom field is similar. Let’s add a new field to checkout, after the order notes, by hooking into the following:'.__('Phone From Checkout Form').': ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '
'; }
/** * Add the field to the checkout */ add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' ); function my_custom_checkout_field( $checkout ) { echo 'Next we need to validate the field when the checkout form is posted. For this example the field is required and not optional:'; }' . __('My Field') . '
'; woocommerce_form_field( 'my_field_name', array( 'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => __('Fill in this field'), 'placeholder' => __('Enter something'), ), $checkout->get_value( 'my_field_name' )); echo '
/** * Process the checkout */ 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['my_field_name'] ) wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' ); }A checkout error is displayed if the field is blank: Finally, let’s save the new field to order custom fields using the following code:
/** * Update the order meta with 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['my_field_name'] ) ) { update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) ); } }The field is now saved to the order. If you wish to display the custom field value on the admin order edition page, you can add this code:
/** * Display field value on the 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 'Some other Example : Make phone number not required'.__('My Field').': ' . get_post_meta( $order->id, 'My Field', true ) . '
'; }
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 ); function wc_npr_filter_phone( $address_fields ) { $address_fields['billing_phone']['required'] = false; return $address_fields; }Adding Custom Fields To Emails To add a custom field value to WooCommerce emails — a completed order email, for example — use the following snippet:
/* To use: 1. Add this snippet to your theme's functions.php file 2. Change the meta key names in the snippet 3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg 4. When next updating the status, or during any other event which emails the user, they will see this field in their email */ add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys'); function my_custom_order_meta_keys( $keys ) { $keys[] = 'Tracking Code'; // This will look for a custom field called 'Tracking Code' and add it to emails return $keys; }That’s it