If you would like to trigger an action, e.g. sending a custom email, depending on the value of a specific field, you can do something like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Trigger an action when an add-on field is present in an order | |
* Note that you will need to change the values for $field_id and $value below | |
*/ | |
function prefix_pewc_after_create_product_extra( $add_on_id, $order, $field ) { | |
$field_id = 1234; | |
$value = '__checked__'; | |
// Check specific field ID is checked | |
if( $field['field_id'] == $field_id && $field['value'] == $value ) { | |
// Do our action here | |
prefix_send_custom_email(); | |
} | |
} | |
add_action( 'pewc_after_create_product_extra', 'prefix_pewc_after_create_product_extra', 10, 3 ); | |
/** | |
* Send an email when an add-on field is present in an order | |
* You'll need to change the values for $to, $subject and $content below | |
*/ | |
function prefix_send_custom_email() { | |
$to = 'you@youremail.com'; | |
$subject = 'Your Subject Line'; | |
$content = 'Add your content here.'; | |
ob_start(); | |
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $subject ) ); | |
$email = ob_get_clean(); | |
$email .= $content; | |
wc_get_template( 'emails/email-footer.php' ); | |
$email .= ob_get_clean(); | |
wc_mail( $to, $subject, $email ); | |
} |
The code above checks each field in an order for a specific ID and a specific value. In this example, the field ID is 1234, the field type is a checkbox, so we’re checking for ‘__checked__’ as the field value. You can modify this as you wish.
In the second function, you’ll need to update the values for $to
, $subject
and $content
depending on who you want to email and what you want to say.
Here’s how to add the snippet.