send email on (admin) status update

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5193
    Olly
    Admin & Mod

      as of v2.11 (2.10 might work too, but not tested)

      assuming you would like to send an email to the customer whenever you change the status of an order, you could do something like the below
      by copying these functions to your themes function.php

      
      /****************************************************************
      allows you to send an email to the customer
      when status has changed
      set labels, from, subject, message and header variables as appropriate in
      wpp4711_orderhistory_hidden_e_mail AND function wpp7811_pizza_status_change_email
      editable things - i.e parameters you probably want or even should edit - are marked with: | EDITABLE |
      ****************************************************************/
      /**************************************************************************************************
      *
      *	[admin order history: add hidden email input field - if exist - after notes to be used in js action]
      *
      **************************************************************************************************/
      add_filter('wppizza_filter_orderhistory_actions', 'wpp4711_orderhistory_hidden_email',10,3);
      function wpp4711_orderhistory_hidden_email($actions, $orderId, $customerDetails) {
      if(isset($customerDetails['cemail']) && $customerDetails['cemail']!=''){
      $actions['notes'].="<input type='hidden' id='wppizza-delivering-order-email-".$orderId."' value='".trim($customerDetails['cemail'])."' />";
      }else{
      $actions['notes'].="".__('no email supplied')."";/* | EDITABLE |*/
      }
      return $actions;
      }
      /**************************************************************************************************
      *
      *	[admin order history: call javascript/ajax on change of status]
      *
      **************************************************************************************************/
      add_action('admin_footer', 'wpp4711_status_update_change');
      function wpp4711_status_update_change(){
      global $current_screen;
      /**only add script to relevant page**/
      if(isset($current_screen) && $current_screen->id=='wppizza_page_wppizza-order-history'){
      echo"<script type='text/javascript'>
      /* <![CDATA[ */
      jQuery(document).ready(function($){
      $(document).on('change', '.wppizza_order_status', function(e){
      var self=$(this);
      self.attr('disabled',true);
      var selId=self.attr('id').split('-').pop(-1);
      var email=$('#wppizza-delivering-order-email-'+selId+'').val();
      jQuery.post(ajaxurl , {action :'wppizza_admin_json',vars:{'type':'statusupdate','email':email,'id':selId, 'status':self.val()}}, function(response) {
      alert(response);
      self.attr('disabled',false);
      },'text').error(function(jqXHR, textStatus, errorThrown) {alert('error : ' + errorThrown);});
      });
      });
      /* ]]> */
      </script>";
      }
      }
      /**************************************************************************************************
      *
      *	[admin order history: send email to customer on status change via ajax call]
      *
      **************************************************************************************************/
      add_action( 'wppizza_ajax_action_admin', 'wpp7811_pizza_status_change_email');
      function wpp7811_pizza_status_change_email($postdata) {
      if($postdata['vars']['type']=='statusupdate'){// could also add a " && in_array($postdata['vars']['status'],array('PROCESSED','REJECTED','REFUNDED'))" to only send when status changed to those
      $status=$postdata['vars']['status'];
      $to      = $postdata['vars']['email'];/*who to send to*/
      /**if only dealing with latin character domains/emails,  else comment out**/
      if(!filter_var($to , FILTER_VALIDATE_EMAIL)){
      $txt=__("invalid email");/*alert if invalid email ->edit as required | EDITABLE| */
      $txt.=PHP_EOL.$to;
      print"".$to;
      exit();
      }
      /***get order details***/
      global $wpdb;
      $getOrderDetails = $wpdb->get_row("SELECT order_details FROM " .$wpdb->prefix . "wppizza_orders WHERE id=".$postdata['vars']['id']." ");
      $subject = 'the subject';/*subject line ->edit as required*/
      $message = 'hello';/*message->edit as required*/
      $message.= PHP_EOL.$getOrderDetails->order_details;/*attach plaintext order underneath - > edit (omit, add linebreaks before and after) as required  | EDITABLE| * */
      /*headers*/
      $headers   = array();
      $headers[] = "MIME-Version: 1.0";
      $headers[] = "Content-type: text/plain; charset=utf-8";
      $headers[] = "From: Sender Name <[email protected]>";/**who its from->edit as required  | EDITABLE|**/
      $headers[] = "Subject: {$subject}";
      $headers[] = "X-Mailer: PHP/".phpversion();
      /*send the email*/
      $mailSent=mail($to, $subject, wordwrap($message, 74, "\r\n"), implode("\r\n", $headers));
      if($mailSent){
      print"email sent to ".$to." status:".$status."";/*alert on successful sending->edit as required  | EDITABLE|*/
      }else{
      print"error sending email ";/*alert on error sending->edit as required  | EDITABLE|*/
      }
      }
      }
      
      #19103
      Olly
      Admin & Mod

        PS: my *guess* would be that the email headers (especially the “from”) should be set to something that corresponds to your server so as to not get blocked by spamfilters etc.
        you’d have to work with your email provider to find out what kind of headers should be set to not get blocked. I really have no idea about your particular setup or what things/headers are needed in particular in your case/setup

      Viewing 2 posts - 1 through 2 (of 2 total)
      • The topic ‘send email on (admin) status update’ is closed to new replies.