WooCommerce send new order email to customer - php

I have a problem and it sounds stupid, but I'm really stucked.
I need to send "new order" email also to customer. I tried adding function to functions.php file, and it sends email, but doesn't show most of variables. I inserted the same code into sample php file, added as page in wordpress and it works really well. I really don't know what is going on.
Any solutions? Maybe there is more simple way, like adding recipent to "new order"email in woocommerce? I checked class-wc-email-new-order.php file, but I was not able to change anything in there, I don't understand how it works.
screen: code working on page VS what is visible in received email
function create_email_body($order_id){
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$body = '';
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$body .= $product['name'];
}
$order = new WC_Order( $order_id );
$body =' <div style = "width: 100%; clear: both; color: #000;">';
$body .=' <div style="width: 50%; float: left;">';
$body .=' <p> Numer zamówienia:<b> '. $order->get_order_number() .'</b> </p>';
$body .=' <p> Imię i nazwisko:<b> '. $order->billing_first_name . " " . $order->billing_last_name . '</b> </p>';
$body .=' <p> Sposób dostawy: <b> '. $order->get_shipping_method() .'</b> </p>';
$body .=' <p> Status zamówienia:<b> przyjęte</b> </p>';
$body .=' <p> Adres dostawy: <b> '. $order->shipping_address_1 . " " . $order->shipping_address_2 . ", " . $order->shipping_postcode . " " . $order->shipping_city . ", " . $order->shipping_state . $order->shipping_country .'</b> </p>';
$body .=' </div>';
$body .=' <div style="width: 50%; float: left;">';
$body .=' <p> Data zamówienia:<b> '. $order->order_date . '</b> </p>';
$body .=' <p> Email: <b><span style = "color: #000 !important"> '. $order->billing_email . '</b> </span></p> ';
$body .=' <p> Metoda płatności: <b> '. $order->payment_method_title .'</p>';
$body .=' <p> Numer telefonu:<b> '. $order->billing_phone .'</b> </p> ';
$body .=' </div>';
$body .=' </div>';
$body .=' <div style = "width: 100%; clear: both; color: #000; margin-top: 30px;">';
$body .=' <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: "Myriad", sans-serif;" border="1">';
$body .=' <thead>';
$body .=' <tr>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;"></th>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Produkt</th>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Ilość</th>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Cena</th>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Wartość</th>';
$body .=' </tr>';
$body .=' </thead>';
$body .=' <tbody>';
$items = $order->get_items();
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
$body .=' <tr class=' . esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ) .'">';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; word-wrap:break-word; color: #000;">';
$body .=' </td><td style = "border: none; color: #000">';
// Product name
$body .=$item['name'];///' <p style = "display: inline-block; float: left">' . apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
if ( ! empty( $item_meta->meta ) ) {
$body .= nl2br( $item_meta->display( true, true, '_', "\n" ) );
}
$body .= "</p>";
$body .= '</td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ) .'</td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $_product->get_price();
$body .=' </td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $order->get_formatted_line_subtotal( $item );
$body .=' </td></tr>';
}
endforeach;
$body .=' </tbody>';
$body .=' <tfoot><td style = "border: none;"></td><td style = "border: none;"></td><td style = "border: none; color: #000;"></td>';
$body .=' <td style = "border: none; color: #000;">';
$body .=' <p> Wartość produktów </p> ';
$body .=' <p> Cena wysyłki </p>';
$body .=' <p> Do zapłaty </p>';
$body .=' </td>';
$body .=' <td style = "border: none; color: #000;">';
$body .=' <p>' . number_format((float)$order->get_subtotal(), 2, '.', '') . " zł" . '</p>';
$body .=' <p>' . number_format((float)$order->get_total_shipping(), 2, '.', '') . " zł" . '</p>';
$body .=' <p>' . $order->get_formatted_order_total(). '</p>';
$body .=' </td>';
$body .=' </tfoot>';
$body .=' </table>';
$body .=' </div>';
$body .= file_get_contents(get_stylesheet_directory_uri() . '/woocommerce/emails/customer-on-hold.order.php');
return $body;
}
function send_email_also_to_customer($order_id){
$order = new WC_Order( $order_id );
$to_email = $order->billing_email;
$headers = 'Od: AB.com <a#b.com>' . "\r\n";
$mailer = WC()->mailer();
$subject = 'Potwierdzenie przyjęcia zamówienia numer # ' . $order_id;
$body = create_email_body($order_id);
$mailer->send( $to_email, $subject, $mailer->wrap_message( $subject, $body), '', '' );
}
add_action( 'woocommerce_new_order', 'send_email_also_to_customer');

You do not need to do all these stuff again for sending multiple emails. You can simply add recipient to "New Order" email(with customer's email). Here is the code you can try:
<?php
/**
* Add another email recipient for admin New Order emails if a shippable product is ordered
*
* #param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!)
* #param \WC_Order $order the order object for which the email is sent
* #return string $recipient the updated list of email recipients
*/
function sv_conditional_email_recipient( $recipient, $order ) {
// Bail on WC settings pages since the order object isn't yet set yet
// Not sure why this is even a thing, but shikata ga nai
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
// just in case
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
$items = $order->get_items();
// check if a shipped product is in the order
foreach ( $items as $item ) {
$product = $order->get_product_from_item( $item );
// add our extra recipient if there's a shipped product - commas needed!
// we can bail if we've found one, no need to add the recipient more than once
if ( $product && $product->needs_shipping() ) {
$recipient .= ', warehouse-manager#example.com';
return $recipient;
}
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );

WRONG :
$recipient .= ', ' . $order->billing_email
CORRECT:
$recipient .= "," . $order->get_billing_email();
I tested and debug this for a whole day.

Update 2018-2019 - For Woocommerce 3+
The accepted answer code is outdated since Woocommerce 3, with some errors in the code.
get_product_from_item() method is deprecated and replaced by $item->get_product().
The Customer email is missing
There is unnecessary code (removed and replaced).
Here is a similar up to date and clean version:
add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 10, 2 );
function custom_new_order_email_recipient( $recipient, $order ) {
// Avoiding backend displayed error in Woocommerce email settings for undefined $order
if ( ! is_a( $order, 'WC_Order' ) )
return $recipient;
// Check order items for a shipped product is in the order
foreach ( $order->get_items() as $item ) {
$product = $item->get_product(); // Get WC_Product instance Object
// When a product needs shipping we add the customer email to email recipients
if ( $product->needs_shipping() ) {
return $recipient . ',' . $order->get_billing_email();
}
}
return $recipient;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

How to send own custom email to user for purchasing product in woocommerce?

I'm trying to send custom email to the user who's purchasing product from our site, I have implemented this code, its working fine but I'm not getting product name and quantity of ordered products. Can anyone help me out and check if Iam missing something in a code or any issues from my end? Thanks
P.s: I'm looking forward to the suggestions from my fellow developers. Thanks
function create_email_body($order_id){
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$body = '';
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$body .= $product['name'];
}
$order = new WC_Order( $order_id );
$body =' <div style = "width: 100%; clear: both; color: #000;">';
$body .=' </div>';
$body .=' <h4> Your order details</h4>';
$body .=' <div style="width: 100%;" id="custom_order_details">';
$body .=' <p> Order Number:<b> '. $order->get_order_number() .'</b> </p>';
$body .=' </div>';
$body .=' <div style="width: 100%;">';
$body .=' <p> Order Date:<b> '. $order->order_date . '</b> </p>';
$body .=' <p> Total Amount:<b>' . $order->get_formatted_order_total(). '</p>';
$body .=' <p> Email: <b><span style = "color: #000 !important"> '. $order->billing_email . '</b> </span></p> ';
$body .=' <p> Payment method: <b> '. $order->payment_method_title .'</p>';
$body .=' <p> Your Phone Number:<b> '. $order->billing_phone .'</b> </p> ';
$body .=' </div>';
$body .=' </div>';
$body .=' <div style = "width: 100%; clear: both; color: #000; margin-top: 30px;">';
$body .=' <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: "Myriad", sans-serif;" border="1">';
$body .=' <thead>';
$body .=' <tr>';
$body .=' <th class="td" scope="col" style="text-align:left;" style = "; color: #000; background-red;">Produkt</th>';
$body .=' </tr>';
$body .=' </thead>';
$body .=' <tbody>';
$items = $order->get_items();
foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
$body .=' <tr class=' . esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ) .'">';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; word-wrap:break-word; color: #000;">';
$body .=' </td><td style = "border: none; color: #000">';
Product name
$body .=$item['name'];' <p style = "display: inline-block; float: left">' . apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
if ( ! empty( $item_meta->meta ) ) {
$body .= nl2br( $item_meta->display( true, true, '_', " " ) );
}
$body .= "</p>";
$body .= '</td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ) .'</td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $_product->get_price();
$body .=' </td>';
$body .=' <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $order->get_formatted_line_subtotal( $item );
$body .=' </td></tr>';
}
endforeach;
$body .=' </tbody>';
$body .=' <tfoot><td style = "border: none;"></td><td style = "border: none;"></td><td style = "border: none; color: #000;"></td>';
$body .=' </tfoot>';
$body .=' </table>';
$body .=' </div>';
$body .= file_get_contents(get_stylesheet_directory_uri() . '/woocommerce/emails/customer-on-hold.order.php');
return $body;
}
function send_email_also_to_customer($order_id){
$order = new WC_Order( $order_id );
$to_email = $order->billing_email;
$headers = 'Od: djohn#dispostable.com <djohn#dispostable.com>' . "
";
$mailer = WC()->mailer();
$subject = 'Confirmation of order receipt number # ' . $order_id;
$body = create_email_body($order_id);
$mailer->send( $to_email, $subject, $mailer->wrap_message( $subject, $body), '', '' );
}
add_action( 'woocommerce_new_order', 'send_email_also_to_customer');

Send email after receipt upload

I'm using this plugin: https://es.wordpress.org/plugins/pepro-bacs-receipt-upload-for-woocommerce/ and I want to send a email to the admin after a buyer upload a receipt. I have this code, but it's not sending emails, what am I doing wrong?
add_action( "woocommerce_customer_uploaded_receipt", "send_mail_after_bacs_receipt_uploaded", 10, 2);
function send_mail_after_bacs_receipt_uploaded($OrderID, $UploadedAttachmentID)
{
global $Pepro_Upload_Receipt;
$_image_url = wp_get_attachment_url($UploadedAttachmentID);
$_image_src = wp_get_attachment_image_src($UploadedAttachmentID, 'full');
$_image_src = $_image_src ? $_image_src[0] : $Pepro_Upload_Receipt->defaultImg;
$blog_name = get_bloginfo('name', 'display');
$blog_address = parse_url(get_bloginfo('url'), PHP_URL_HOST);
$blog_mail = "wordpress#$blog_address";
$admin_mail = get_option('admin_email');
$order = new WC_Order($OrderID);
$user_id = $order->get_user_id();
$current_user = get_user_by("ID", $user_id);
$mail_receiver = $current_user->user_email;
$mail_subject = "[$blog_name] BACS Receipt";
$mail_wrapper_styles = "display:block;
width:450px;
border-radius:0.5rem;
margin: 1rem auto;
text-align: center;
color: #2b2b2b;
padding: 1rem;
box-shadow: 0 2px 5px 1px #0003;
border: 1px solid #ccc;";
$mail_body = "<div style='$mail_wrapper_styles'>";
$mail_body .= "<h2>BACS Payment Proof Received</h2>";
$mail_body .= "<p>Hello <strong>$current_user->display_name</strong>, We've received you BACS Payment Proof.</p>";
$mail_body .= "<p>We are cheking nearly thousand payments daily and yours is in the list too.</p>";
$mail_body .= "<p>Please be patient, thank you.</p><br />";
$mail_body .= "<a href='$_image_url' target='_blank'>";
$mail_body .= " <img title='Click to enlarge' style='border-radius: 0.5rem;' src='$_image_src' width='400px' />";
$mail_body .= "</a><br />";
$mail_body .= "<p><strong><small>Copyright © $blog_name ($blog_address), all rights reserved.</small></strong><br />";
$mail_body .= "<small style='color: #717171;'>THIS MAIL WAS SENT TO <i>$mail_receiver</i></small></p>";
$mail_body .= "</div>";
$headers = array(
"Content-Type: text/html; charset=UTF-8",
"From: $blog_name <$blog_mail>"
);
// https://developer.wordpress.org/reference/functions/wp_mail/
wp_mail( $mail_receiver, $mail_subject, $mail_body, $headers );
}

Php echo is not working in my HTML woocommerce custom email

I have a custom email which is triggered if product has meta in woocommerce order. Everything works great but I can't get the billing_first_name into the text. I use <?php echo $menomeno; ?>which is declared as $order->get_billing_first_name(); and I've also tried <?php echo $order->get_billing_first_name(); ?> Nothing worked, I get everytime empty string in the email. I have also tried to use sprintf method but it did not sent email. Any ideas how may I solve this?
Note: HTML code is shortened, its much bigger but I did not want to post the whole big code because its not neccesary in this situation when we need to get billing name in the part of the code.
function send_a_custom_email( $order_id, $order ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$mailer = $woocommerce->mailer();
$product_ids = array( ); // Initializing
$customer_email = $order->get_billing_email();
$customer_name = $order->get_billing_first_name();
foreach ( $order->get_items() as $item ) {
$meta_data = $item->get_meta('meno'); // Zisti ake je meno
$venovanie = $item->get_meta('venovanie'); // // Zisti ake je venovanie
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if( empty($meta_data) ) {
$product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
}
}
if ( empty($product_ids) && $product_id == 2805 ) {
$recipient = $customer_email; // Set email recipient
$menomeno = $customer_name;
$subject = sprintf( __('Order #%d: Has missing item meta data'), $order_id );
$content = '<table border="0" cellpadding="0" cellspacing="0" class="text_block" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; word-break: break-word;" width="100%">
<tr>
<td style="padding-bottom:30px;padding-left:30px;padding-right:30px;padding-top:15px;">
<div style="font-family: sans-serif">
<div style="font-size: 14px; mso-line-height-alt: 25.2px; color: #ffffff; line-height: 1.8; font-family: Montserrat, Trebuchet MS, Lucida Grande, Lucida Sans Unicode, Lucida Sans, Tahoma, sans-serif;">
<p style="margin: 0; font-size: 16px; text-align: center;"><strong>S láskou <?php echo $menomeno; ?>
</strong></p>
</div>
</div>
</td>
</tr>
</table>';
//wp_mail( $recipient, $subject, $content ); // Send email
$mailer->send( $order->billing_email, sprintf( __( 'DTerapia s láskou' ), $order->get_order_number() ), $content );
}
}
Thanks in advance.
Please see this explanation about concatenation:
https://www.php.net/manual/en/language.operators.string.php

Blank Emails php

some emails are coming up blank in my inbox. I think the reason may be here in the source code.
The left image is the one that presents the error.
Code:
define( 'OWNER_EMAIL', 'leads#consorciomenegalli.com.br' );
define( 'DONOTREPLY_EMAIL', 'do-not-reply#consorciomenegalli.com.br' );
define( 'OWNER_NAME', 'Menegalli' );
case 'quote-form':
# put the email title here
$title = 'Nova simulação via website';
# email headers
$headers = "MIME-Version: 1.0\n".
"Content-type: text/html; charset=utf-8\n".
"Content-Transfer-Encoding: 8bit\n".
"From: ". $_POST['clientName'] ." <". $_POST['clientEmail'] .">\n".
"Reply-to: ". $_POST['clientName'] ." <". $_POST['clientEmail'] .">\n".
"Date: ". date( "r" ). "\n";
# appointment values
$values = $_POST['values'];
# create rows with values from appointment form
$rows = '';
for( $i = 0; $i < count( $values ); $i++ ) {
$rows .= '<tr>
<td style="width: 200px; font-weight: bold; border: 1px solid #eee; padding: 10px;">'. $values[$i]['name'] .'</td>
<td style="border: 1px solid #eee; padding: 10px;">'. $values[$i]['value'] .'</td>
</tr>';
}
# email content
$content = '<table style="width: 600px; font-size: 11px; border-collapse: collapse;">'. $rows .'</table>';
# sending an email
$result = mail(
OWNER_EMAIL,
"=?UTF-8?B?". base64_encode( $title ) ."?=",
$content,
$headers
);
# if the email wasn't send
if( $result == false ) {
# second version of email
mail(
OWNER_EMAIL,
"=?UTF-8?B?". base64_encode( EMAIL_TITLE ) ."?=",
$content
);
}
break;
I think the email is not picking up . $values[$i]['name'] . and . $values[$i]['value'] .

How to remove price from Woocommerce Email

I am trying to develop a plugin that send the customer a "gift receipt" upon completing an order. I have everything working perfectly except I am unable to strip the price from the emails. When I try to edit the email-order-items.php template and remove the price column then the emails come in blank.
Specifically this is for downloadable products so the download links no longer show when I make any edits to the email-order-items file. And I would only want it to strip the price from the gift receipt emails not the other emails.
What I have done:
in my plugin I call on a email template "customer-gift-receipt.php" which is pretty much the same as "customer-processing-order.php" that comes package with Woocommerce.
In the file there is this line that brings in the email-order-items template and shows the links and price
<?php echo $order->email_order_items_table( $order->is_download_permitted(),
true, ($order->status=='processing') ? true : false ); ?>
This is the email-order-items template.
No matter what I do I cannot seem to get those prices stripped from just the customer-gift-receipt.php emails. Specifically it is this line in the above template:
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">
<?php echo $order->get_formatted_line_subtotal( $item ); ?>
</td>
I tried creating a duplicate of the email-order-items template and removing that line then calling it in my plugin and that did not work. I also tried copying the email-order-items template inside of the customer-gift-receipt.php file in the appropriate section and that failed as well. I had to define $items = $order->get_items(); when I tried copying email-order-items directly into the customer-gift-receipt template for it to somewhat work.
So can anyone suggest a way for me to strip the prices out of my customer-gift-receipt templates?
I have checked through these links:
Class WC_Order
Class WC_Email_Customer_Processing_Order
UPDATE:
I just found this link which should help me bring in email_order_items_table outside of the class: https://gist.github.com/mikejolley/1965842
When I try to add the above code in my customer-email-receipt template and place an order I get this error:
Fatal error: Class 'order_item_meta' not found in
.../.../.../.../woocommerce/emails/customer-gift-receipt.php on line 41"
Disable e-mail order items table from e-mail template and copy the function as a custom function into your theme.
<tbody>
<?php //echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false );
echo custom_order_table($order);
?>
</tbody>
I had to remove download and variations which causes *Fatal Error with Class 'order_item_meta'* error. So your custom function will look like:
function custom_order_table($order,$price = false) {
foreach($order->get_items() as $item) :
$_product = $order->get_product_from_item( $item );
$file = $sku = $variation = $image = '';
if ($show_image) :
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail');
$image = apply_filters('woocommerce_order_product_image', '<img src="'.$src[0].'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />', $_product);
endif;
if ($show_sku && $_product->get_sku()) :
$sku = ' (#' . $_product->get_sku() . ')';
endif;
$return .= '<tr>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'. $image . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td>
<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'.$item['qty'].'</td>';
if ($price):
$return .= '<td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">';
if ( $order->display_cart_ex_tax || !$order->prices_include_tax ) :
$ex_tax_label = ( $order->prices_include_tax ) ? 1 : 0;
$return .= woocommerce_price( $order->get_line_subtotal( $item ), array('ex_tax_label' => $ex_tax_label ));
else :
$return .= woocommerce_price( $order->get_line_subtotal( $item, true ) );
endif;
$return .= '</td>';
endif;
$return .= '</tr>';
// Show any purchase notes
if ($show_purchase_note) :
if ($purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) :
$return .= '<tr><td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . apply_filters('the_content', $purchase_note) . '</td></tr>';
endif;
endif;
endforeach;
echo $return;
}

Categories