• Im looking to set up an ‘event listener” in wordpress that makes use of wp_mail. Basically, I have a custom content type I created. it has fields of the following:

    order_status = Order status. Will be one of these values
    Possible Values: Assembly, Shipped, Ready to Ship, Manufacturing, Queued
    customer_email = Customer’s email address (text entry)
    customer = Customer’s name

    Basically, the way i want it set up is that when order_status gets changed to “Ready to Ship”, wp_mail would send out an email like this:

    Subject: Your order is ready to ship!
    Body: Dear $customer,

    To ensure a jump on delivery time, you can send payment for the item to be shipped out as early as tomorrow…. (etc…)

    However, if the order_status is changed to Queued, than they might get an email like this:

    Subject: Your order is queued into our system
    Body: Dear $customer,

    At this time, your order is queued for our lab’s creation process. Please check our page for more details.

    Thanks.

    ************

    So far, the closest I can deduce to make this possible is to put this in my theme’s function.php:

    <?php
    
    $ready_to_ship = get_custom_field('order_status');
    
    $to = get_custom_field('customer_email');
    $customer_name = get_custom_field('customer');
    
    if (update_{$ready_to_ship}_meta === 'SHIPPED') {
    
    $subject = 'Your order is ready to ship!';
    $message = 'Dear ' . $customer_name . ': Your order is ready to ship. ';
    
    wp_mail( $to, $subject, $message);
    
    }
    
    ?>

    Am I on the right track, or should I use
    if (updated_{$ready_to_ship}_meta === 'SHIPPED') {

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter WebmistressM

    (@webmistressm)

    Bumping this topic. Still have no answer and using the above code, I get some parsing errors. So, I can confirm that my code is not correct. Any ideas how to target specific emails to go out (from wp_mail) depending on custom field order_status?

    [No bumping, please.]

    Thread Starter WebmistressM

    (@webmistressm)

    Hopefully this post isnt considered “bumping”, as I wish to show progression for any future people who may want this kind of functionality in their site.

    Using add_action and save_post, this might be closer to finding something that looks for specific values, rather than if the custom field is empty or not. Apologies if the code is not parsed with all brackets and parenthesis closed. Still tweaking the code a bit:

    add_action( 'save_post', 'my_check_function', 999 );
    
    function my_check_function( $post_id ) {
    	$blahvalue = get_post_meta( $post_id, 'blah', true );
    
    if $blahvalue = "San Francisco"
    //need to declare specific values for each of the strings below, for a letter sent when "San Francisco" is written in this custom field
    wp_mail( $to, $subject, $message, $headers, $attachments );
    
    if $blahvalue = "Los Angeles"
    //need to declare specific values for each of the strings below, for a letter sent when "Los Angeles" is written in this custom field
    wp_mail( $to, $subject, $message, $headers, $attachments );
    
    else end;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Event Listener on Custom Fields – WP Mail’ is closed to new replies.