WooCommerce is a WordPress plugin that allows you to sell products and services online. WooCommerce emails are the only emails that are automatically sent by the platform, but they can be customized as well. In this blog you will learn how to add custom a field in WooCommerce > Settings > Emails tab.
In the WooCommerce Email Settings tab there is a table with all of the available email templates such as ‘New Order’, ‘Canceled Order’, ‘Failed Order’, etc. These are usually defined by extending the WC_Email class. Take a look in /plugins/woocommerce/includes/emails for examples.
‘woocommerce_settings_api_form_fields_{email-id}’ can be easily used to add a setting to a specific email class. It provides us with the array of form fields for the email class.
For this example lets add an additional option to the ‘New Order’ email template. We can look in the WC_Email_New_Order class constructor (found in class-wc-email-new-order.php) to find/verify the id to replace {email-id} in our hook. In this case the WC_Email_New_Order class id is ‘new_order’ so to add a setting or form field to the New Order options our hook will look like,
add_action( 'woocommerce_settings_api_form_fields_new_order', 'add_my_custom_email_setting',10,1);
Now in the ‘add_my_custom_email_setting’ function all we need to do is add our new setting to the form_fields array and return it:
function add_my_custom_email_setting($form_fields){
$form_fields['labotrees_custom_id']=[
'title'=>'Custom Option',
'description'=>'This is a text area we added to the new order email settings page.',
'type'=>'textarea', // various fields are accepted for this
'default'=>''
];
return $form_fields;
}
Now, look at how to add an option to all of the available templates. To do this we need simply define a hook for every email class,
add_action('woocommerce_settings_api_form_fields_new_order','add_my_custom_email_setting');
This will add new custom field fin new order email.
But if you want to add similar field or any other field you have to field the ending slug for type of email where you want to add a new field. Look at picture below to how to find respective url for email.
If you have any issues do contact me for more details.
- How to change text on addition notes in woo-commerce ?
- Custom redirect on clicking add to cart in WooCommerce
- How to add a required checkbox field in WooCommerce checkout page ?
- What is the difference between add_filter and apply_filters?
- How to change Address Format For a Specific Country in Woocommerce?
- How to run jQuery after WooCommerce AJAX cart updated?