Author: super_admin_v3x
Email/Print Templates
On (admin) order 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 }
Customise order id
Gateway filters frontend
Get (logged in) users previous order details
Gettings ome info about a logged in users previous orders programatically
the ‘wp’ action hooks is only for demonstration purposes. you will probably want to use another action or filter hook as required
/* change wp hook to whatever is appropriate */ add_action('wp', 'my_custom_loggedin_customer'); function my_custom_loggedin_customer(){ if( is_user_logged_in()){ $user_id = get_current_user_id(); $user_data = WPPIZZA()->db->get_customers($user_id); /* do something with $user_data['results_set'] */ } }
Change prices of menu item(s) depending on day of week
Prices output in loop
Additional validation function
Adding additional validation functions to fields in WPPizza -> Order form
There are a number of additional javascript validation functions in-built into the plugin. However, aside from the default ones you can already select you have to selectively enable the ones that are of use to you.
The following functions are available (please see the jquery validation documentation for details on each)
- alphanumeric
- maxWords
- minWords
- rangeWords
- accept
- bankaccountNL
- bankorgiroaccountNL
- bic
- cifES
- cpfBR
- creditcard
- creditcardtypes
- currency
- dateFA
- dateITA
- dateNL
- extension
- giroaccountNL
- iban
- integer
- ipv4
- ipv6
- lettersonly
- letterswithbasicpunc
- mobileNL
- mobileUK
- nieES
- nifES
- notEqualTo
- nowhitespace
- parameters
- phoneNL
- phoneUK
- phoneUS
- phonesUK
- postalCodeCA
- postalcodeBR
- postalcodeIT
- postalcodeNL
- postcodeUK
- require_from_group
- skip_or_fill_minimum
- stateUS
- time
- time12h
- url2
- vinUS
- zipcodeUS
- ziprange
- decimal /* wppizza plugin custom added methods – not a jqueryvalidation rule */
wppizza_filter_validation_rules
enabling one of the above rules
@param: array (all validation rules)
@return: array
example:
add_filter('wppizza_filter_validation_rules','my_function'); //to enable an exiting rule above - let's say 'phoneUS' function my_function($validation_rules){ $validation_rules['phoneUS']['enabled'] = true; return $validation_rules; }
creating and adding your own rules
example:
/*-----------------------php (see also https://docs.wp-pizza.com/developers/?section=filters-actions-functions)-----------------------------------------------*/ //to add your own rule - let's say 'my_rule' add_filter('wppizza_filter_validation_rules','my_function'); function my_function($validation_rules){ $validation_rules['my_rule']= array( 'lbl'=> 'My Rule', 'parameters'=>false, 'callback'=>false, 'enabled'=>true); return $validation_rules; } /*-----------------------js-----------------------------------------------*/ // then add the rule like so via wp_footer action hook (or add it to an already existing js file) // adjust the validation pattern as appropriate for your validation rule jQuery(document).ready(function($){ $.validator.methods.my_rule = function (value, element) { return this.optional(element) || /^(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value); } // adding a custom error message if validation fails instead of the default (not actually tested but should work just the same) jQuery.extend(jQuery.validator.messages, { my_rule: 'some error message, }) });