5.7.Unconfirmed Orders
Mark orders as unconfirmed initially until some script/user interaction accepts the order.
Requires wppizza 3.3 (but might also work with slightly earlier versions) Note: the basics of this are tested, but further modifications will be required. Read the notes for each filter/action below. This might not work with pre-paid – i.e paid by credit card – orders (not tested). If it does not but you need this you will have to figure out a way to do this yourself as it’s really bad form to tell people you might not accept an order when it was already paid for (in my book anyway).
wppizza_before_order_execute
set an order to be unconfirmed first
example:
/*****************************************************
* set an order to be unconfirmed
******************************************************/
add_action('wppizza_before_order_execute', 'myprefix_execute_as_unconfirmed');
/******************************************************
* intercepting "normal" execution of cod style orders
* to set an order to "UNCONFIRMED"
******************************************************/
function myprefix_execute_as_unconfirmed($order){
/************************************************************
a made up $transaction_id as there is none yet
but can be overridden when finally executing/confirming
************************************************************/
$order_initiator = $order['ordervars']['payment_gateway']['value'];
$order_id = $order['ordervars']['order_id']['value'];
$transaction_id = $order_initiator . WPPIZZA_WP_TIME . $order_id ;
/************************************************************
$custom_update_columns - none
************************************************************/
$custom_update_columns = false;
/************************************************************
$transaction_details - set some details if you want or need to
refer to later or simply false
************************************************************/
//$transaction_details['my_prefix']['key'] = 'somekey';
$transaction_details = false;
/************************************************************
$transaction_errors - set to false (as there cannot be any yet)
************************************************************/
$transaction_errors = false;
/************************************************************
perhaps us __CLASS__ if script used in class -
used as an identifier for logging purposes, or an arbitrary string if used outside class
(no spaces or special chars other than "-" or "_" please though)
************************************************************/
$class_name = 'some_identifier';
/************************************************************
$check_user_id - should we check for user id too
to make sure wp_user_id of order we are dealing with matches
the current users wp_user_id ?
in same cases this can be set, but if we are receiving
notifications from a third party , this should be null
- in fact, one should quite safely be able to keep it at null
************************************************************/
$check_user_id = null;
/************************************************************
$unconfirmed - set to true here (that's the point of the plugin after all)
************************************************************/
$unconfirmed = true;
/*
if you want to send some other, customised emails instead, uncomment and create the function
(here myprefix_send_confirmation_message) that sends your custom emails somehow
*/
//add_filter('wppizza_on_order_execute_send_email', array($this, 'myprefix_send_confirmation_message'), 10, 5);
/************************************************************
$send_emails - do not send the standard confirmation emails
************************************************************/
$send_emails = false;
/*
execute (capture, omit sending standard emails, and set to UNCONFIRMED instead of complete)
*/
$result = wppizza_order_execute($order, $transaction_id, $transaction_details, $transaction_errors, $class_name, $check_user_id, $custom_update_columns, $unconfirmed, $send_emails);
/* return errors if any, else redirect */
if(isset($result['error'])){
print"".json_encode($result)."";
exit();
}
/*
$result will now be a redirection link and redirect to thank you page with
UNCONFIRMED details (set in localization)
*/
print"".json_encode($result)."";
exit();//we must exit here
}
wppizza_filter_pages_unconfirmed_markup
add a page reloader on unconfirmed results page
example:
add_filter('wppizza_filter_pages_unconfirmed_markup', 'myprefix_thankyou_page_unconfirmed_reload', 10, 2);
function myprefix_thankyou_page_unconfirmed_reload($markup, $order_formatted){
if($order_formatted['ordervars']['payment_status']['value'] == 'UNCONFIRMED'){
$markup['myprefix_refresh_page']='<script>setInterval(function(){window.location = window.location.href;return;},5000);</script>';//reload page every 5 seconds
}
return $markup;
}
init
accept an order programatically
example:
add_action('init', 'myprefix_accept_order');
/*******************************************************
*
* accept an order
* typically done by some script in the backend sending
* some post or get vars
* YOU SHOULD ADD SOME MORE VERIFICATION HERE
* THE BELOW IS JUST A SKELETON
******************************************************/
function myprefix_accept_order(){
/*
a simple check that something was received
AGAIN, YOU REALLY SHOULD ADD MORE VERIFICATION HERE
*/
if(empty($_REQUEST)){return;}
/* parameters received, sanitised */
$parameters = wppizza_sanitize_post_vars($_REQUEST);
/* we assume $parameters['id'] is the order id */
$order_id = $parameters['id'];
/*
check that received id matches an unconfirmed order
by trying to get an unconfirmed order by order id and UNCONFIRMED status
*/
$columns = array();
$columns['id'] = array('data' => $order_id, 'operator' => '=');
$columns['payment_status'] = array('data' => 'UNCONFIRMED', 'operator' => '=');
$order = wppizza_get_order_by_columns($columns);
/*
bail if order not found
*/
if(empty($order)){return;}
/*******
assuming all is good -
now execute the previously unconfirmed but now confirmed order
sending emails , setting to completed etc etc
********/
$transaction_id = $order['sections']['ordervars']['transaction_id']['value'] ;//use tx id we have set previously
$transaction_details = $parameters;// capture tx detals
$transaction_errors = false;// there are no errors that can have happened (or script should have bailed earlier already)
$class_name = 'some_identifier';// for logging, use __CLASS__ if used in calss, else some arbitrary string
$check_user_id = false;// do not try and match user id as this script is called from somewhere else , not related to the user at this time)
$custom_update_columns = false;// no additional column update required (unless you want to, check wppizza_order_execute_ipn as to how )
$unconfirmed = false;// false as we want to complete an order that's already set to unconfirmed
/*
now send normal emails etc etc
*/
$complete_order = wppizza_order_execute_ipn($order, $transaction_id, $transaction_details, $transaction_errors, $class_name, $custom_update_columns, $unconfirmed );
return;
}
documentor id 5