5.6.Send email on (admin) status update
Sending an email whenever you change the status of an order in WPPizza -> Orderhistory
send an email (or whatever else it is you would like to do) when you change the status of an order in the order history
wppizza_on_orderstatus_change
@param: array (order details)
@param: str (the status the order has been changed to – something like ‘REFUNDED’, ‘NEW’, ‘ACKNOWLEDGED’ etc)
@param: array (wppizza options set of the blog/site where the order was made. In a multisite setup – if you are displaying all orders from all sites in the “master” site – this might be different to the current site you are on. For the current sites blogoptions use global $wppizza_options; and use those)
@param: str (plaintext template of a selected template id if using filter below)
@param: str (html template of a selected template id if using filter below and template set to be html – @since wppizza 3.5.1)
@return: str (js alert after change was completed – empty to omit alert)
example:
add_filter('wppizza_on_orderstatus_change', 'myprefix_on_orderstatus_change', 10, 5); function myprefix_on_orderstatus_change($order_details, $order_status, $blog_options, $plaintext_template, $html_template){ /* if in a multiste environment use global $wppizza_options; to get wppizza options of the site you are in. else $blog_options will do if you need them */ /* get the customers address from order_details array var_dump($order_details) to get all details */ $to_address = $order_details['customer']['cemail']['value']; /**if only dealing with latin character domains/emails do a quick validation, else comment out**/ if(!filter_var($to_address , FILTER_VALIDATE_EMAIL)){ $js_alert = __('invalid email');/*alert if invalid email | EDIT AS REQUIRED */ $js_alert .= PHP_EOL . $to_address; return $js_alert; } /* subject line */ $subject = 'The Subject';/* subject line | EDIT AS REQUIRED */ /* email body / message */ $message = __('hello');/*message | EDIT AS REQUIRED*/ /* add plaintext email template of order underneath if exists - see filter below how to get this | EDIT AS REQUIRED */ $message.= !empty($plaintext_template) ? PHP_EOL . $plaintext_template .PHP_EOL : '' ; /* headers */ $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-type: text/plain; charset=utf-8"; $headers[] = "From: Sender Name <[email protected]>";/**who it's from | EDIT AS REQUIRED**/ $headers[] = "X-Mailer: PHP/".phpversion(); /* send the email, wrap message at 74 chars for maximum email client compatibility */ $mailSent = mail($to_address, $subject, wordwrap($message, 74, "\r\n"), implode("\r\n", $headers)); /* set js alert after sending */ if($mailSent){ $js_alert = __('Email sent to '.$to_address.''.PHP_EOL.'Status: '.$order_status.'');/*alert on successful sending | EDIT AS REQUIRED*/ }else{ $js_alert = __('Error sending email to '.$to_address.''); /*alert on error sending | EDIT AS REQUIRED*/ } return $js_alert; }
wppizza_on_orderstatus_change_add_template
if you would like the markup of an email template as set in WPPizza->Templates->Emails
available in your above filter as the 4th parameter, use the below filter example to select from one of the the id’s . If the template is set to be HTML the 5th parameter will contain the html markup.
@param: int (emails template id’s . default = false)
@return: int|false (selected email template id or false)
example:
add_filter('wppizza_on_orderstatus_change_add_template', 'myprefix_on_orderstatus_change_add_template'); function myprefix_on_orderstatus_change_add_template($selected_email_template_id){ /* get plaintext email from template with id 1 for this order */ $selected_email_template_id = 1; return $selected_email_template_id; }
Add some javascript to order history to execute if order status changes
wppizza_filter_orderhistory_markup
@param: array (array of markup parts)
@return: array
add_filter('wppizza_filter_orderhistory_markup', 'my_custom_orderhistory_markup'); function my_custom_orderhistory_markup($markup){ /* add javascript to the end of the markup */ $markup['my_js_markup']= "<script type='text/javascript'> /* <![CDATA[ */ jQuery(document).ready(function($){ $(document).on('change', '.wppizza-orderhistory-order-status', function(e){ var self=$(this); var selId=self.attr('id').split('-').pop(-1); var selfVal=self.val(); /* do things when you change the status to PROCESSED, in this case trigger the print button **/ if(selfVal=='PROCESSED'){ var triggerTarget=$('#wppizza-orderhistory-print-order-'+selId+''); triggerTarget.trigger('click'); } }); }); /* ]]> */ </script>"; return $markup; }
Add some javascript to order history to execute if order status changes to “PROCESSED” and execute an ajax call passing on some js prompt
example:
add_filter('wppizza_filter_orderhistory_markup', 'my_custom_orderhistory_markup'); function my_custom_orderhistory_markup($markup){ /* add javascript to the end of the markup */ $markup['my_js_markup']= "<script type='text/javascript'> /* <![CDATA[ */ jQuery(document).ready(function($){ $(document).on('change', '.wppizza-orderhistory-order-status', function(e){ var self=$(this); var selId=self.attr('id').split('-').pop(-1); var selfVal=self.val(); var order=selId.split('_'); var blog_id=order[0]; var order_id=order[1]; /* do things when you change the status to PROCESSED - in this case adding a prompt and passing this on to an ajax call **/ if(selfVal=='PROCESSED'){ var message = prompt('your message', 'default message'); jQuery.post(ajaxurl , {action :'myprefix_admin_ajax',vars:{'type':'statusupdate', 'blog_id':blog_id, 'order_id':order_id, 'message':message}}, function(response) { alert('ok'); },'text').error(function(jqXHR, textStatus, errorThrown) {alert('error : ' + errorThrown);}); } }); }); /* ]]> */ </script>"; return $markup; } /** admin ajax **/ add_action('wp_ajax_myprefix_admin_ajax', 'myprefix_set_admin_ajax' ); function myprefix_set_admin_ajax(){ // listen to type statusupdate being posted if($_POST['vars']['type'] == 'statusupdate'){ /* blogid (if required) */ $blog_id = (int)$_POST['vars']['blog_id'];// sanitise blog_id , just for good measure /* order */ $order_id = (int)$_POST['vars']['order_id'];// sanitise order id , just for good measure $order = wppizza_get_completed_order($order_id);// get order details /* prompt message set - you probably want to validate/sanitize this somewhat*/ $message = $_POST['vars']['message']; /* send email for example (as outlined above) or whetever you need to do utisiling the variables you have available if needs be (like $order, $message) such as to get the customers address from order array ( var_dump($order) to get all order details) */ $to_address = $order['customer']['cemail']['value']; } die();//important }
documentor id 5