WPPizza – A Restaurant Plugin for WordPress › Support › General Support › add order details as attachment (JSON) to email
- AuthorPosts
- 13 January, 2017 at 2:04 pm #24708
Hello,
i want to consume a WPPizza order with my POS software. I’ve tried to parse the plaintext/html email but this is very unreliable to get the field information like customer, items etc.
So now i attach the $orderdetails as JSON to the order email. JSON is very easy to parse and i get the right information. I’ve writen a custom filter to generate the JSON data:
// save orderdetails as JSON to file
add_action( ‘wppizza_on_order_executed’, ‘print_order’);
function print_order($orderId) {require(WPPIZZA_PATH.’classes/wppizza.order.details.inc.php’);
$orderDetails = new WPPIZZA_ORDER_DETAILS();
$orderDetails->setOrderId($orderId);
$order = $orderDetails->getOrder();$txt .= json_encode($order, JSON_PRETTY_PRINT);;
file_put_contents(‘order.json’, $txt);
}I added the “order.json” as attachment in the mail settings. This works but i wonder if there is a better way to do this. Saving the data in “wppizza_on_order_executed” and adding this later in email, does this work in multithreading way? So if two customers orders at same time, do they get the right file?
So is there a better way to add orderdata as attachment to the order mail? Is there a filter to to this at time of attachment adding? Maybe i can avoid to use a temp file?
Thanks in advance
Dirk13 January, 2017 at 3:32 pm #24729that action hook runs after everything is said and done (i.e after mails have been sent)
you could of course just send another simple email with/after that hook, attaching your file
(you probably want to also name that file using the orderId as prefix/suffix )13 January, 2017 at 3:33 pm #24730there is currently no (simple) way to add custom/dynamically generated attachments to an email
13 January, 2017 at 8:07 pm #24742Hi Olly,
thank’s for always good & fast response!
So this my feature request: Can you add a filter while adding attachments to mail to intercept and userdefined data to mail? In my case just the $order data?something like this (pseudo code for “wppizza.send-emails.inc.php”, line 662 ff):
/*attachments*/
if(!$omit_attachments){
if(!empty($options[‘order’][‘order_email_attachments’])){
foreach($options[‘order’][‘order_email_attachments’] as $attachment){
if(!empty($attachment) && is_file($attachment)){
$email_attachments[]=$attachment;
}
}
}}/**allow adding/changing attachments on the fly***/
$options = apply_filters(‘wppizza_filter_order_email_attachments’, $options , $order, $email_attachments);/**send as html ? */
$asHtml=!empty($email_markup[‘html’]) ? true : false;That filter would allow my plugin to add orderdata in a computer-parseable JSON/XML/CSV format..
Best regards
Dirk27 January, 2017 at 12:50 pm #25020Hi Olly,
i managed to attach the order data as json on demand but i’m very unhappy to patch the file “wppizza.send-emails.inc.php” directly.
Is there a better way to do this via filter/plugin?This is my patch:
Index: wppizza.send-emails.inc.php =================================================================== --- wppizza.send-emails.inc.php (revision 9754) +++ wppizza.send-emails.inc.php (working copy) @@ -489,6 +489,14 @@ $order_details=$orderDetails->getOrder();/**all order vars => plaintext**/ /*********************************************************** + Add orderdetails as JSON attachment + ***********************************************************/ + $txt .= json_encode($orderDetails->getOrder(), JSON_PRETTY_PRINT); + $orderfilename = 'order'.$orderId.'.json'; + file_put_contents($orderfilename, $txt); + + + /*********************************************************** [set name and email of the the person that is ordering] ***********************************************************/ /*get order details for html too if required*/ @@ -547,7 +555,7 @@ foreach($tpl_recipients as $template_id=>$recipients){ - $mail_result=$this->send_email_execute($recipients, $email_markup[$template_id], $template_id, $order_details['customer'], $no_attachments[$template_id]); + $mail_result=$this->send_email_execute($orderfilename, $recipients, $email_markup[$template_id], $template_id, $order_details['customer'], $no_attachments[$template_id]); /**only return results for main email sent to shop**/ if(!empty($recipients['email_shop'])){ $send_email_result=$mail_result; @@ -554,6 +562,11 @@ } } + // delete temp order file +// if (file_exists($orderfilename)) { +// unlink($orderfilename); +// } + return $send_email_result; } @@ -566,7 +579,7 @@ * @customer_in_cc true to cc, false to send separate email * *********************************************************************************************************************/ - function send_email_execute($recipients, $email_markup, $template_id, $customer_data, $omit_attachments=false){ + function send_email_execute($orderfilename, $recipients, $email_markup, $template_id, $customer_data, $omit_attachments=false){ //global $phpmailer; static $static=0; $static++; /*get plugin options*/ @@ -689,7 +702,11 @@ } } }} - + if(!empty($orderfilename) && is_file($orderfilename)){ + $email_attachments[]=$orderfilename; + } + + /**send as html ? */ $asHtml=!empty($email_markup['html']) ? true : false;
Thanks
Dirk27 January, 2017 at 2:02 pm #25021i really do not understand why you would want to send this attachment to everybody that might be set up as recipient and not just to one dedicated email that can actually do something with it
i.e on order execute hook create your file as you described above and then just send it along these lines
/*email parameters - tweak as appropriate */ $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "From: Sender Name <[email protected]>";/**who its from->edit as required**/ $headers[] = "X-Mailer: PHP/".phpversion(); /* you might need some more headers like content type etc to do attachments properly */ $to = '[email protected]';/** edit as required **/ $subject = 'my attachment';/** edit as required **/ $message = 'my message';/** edit as required **/ $attachments = array(); $attachments[] = "[absolute/path/to/attachment]"; /** edit as required **/ /*send the email*/ $send_attachments =wp_mail($to, $subject, $message, $attachments);
not tested , but this sort of thing anyway
- AuthorPosts
- The topic ‘add order details as attachment (JSON) to email’ is closed to new replies.