In Woocommerce, I am trying to get the customers additional order message:
I am trying to display that customer message on the email notifications.
But i don't know how to get this information in the php code.
Here is my code in the functions.php file:
add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $item_id, $item, $order, $plain_text){
$notes=$order->customer_message; //did not work
echo '<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 1px solid #e5e5e5;" border="0"><tbody><tr><td><p>Test: ' . $notes . '</p></td></tr></tbody></table>';
}
I really don't know how to access that information.
Any help is appreciated.
There are some errors in your code. Using the WC_Order method get_customer_note(), try the following instead, that will display for some successful email notifications, the customer order note:
add_action( 'woocommerce_email_after_order_table', 'customer_note_email_after_order_table', 10, 4 );
function customer_note_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ){
// Only on some email notifications
if ( in_array( $email->id, array('new_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order') ) ) :
// Get customer Order note
$customer_note = $order->get_customer_note();
// Display the Customer order notes section
echo '<h2>' . __("Order notes", "woocommerce") . '</h2>
<div style="margin-bottom: 40px;">
<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
<tr><td><p>' . $customer_note . '</p></td></tr>
</table></div>';
endif;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Related
Hello This Below Code is working fine but this is not inline to "Add to Cart" button. I Want This Custom Button To Inline Add To Cart.
add_action( 'woocommerce_single_product_summary', 'custom_button_by_categories', 36 ,0 );
function custom_button_by_categories(){
global $product;
// Define your categories in this array (can be Ids, slugs or names)
$product_cats = array('mobile-touch', 'music', 'furnitures');
if( has_term( $product_cats, 'product_cat', $product->get_id() ) ){
$demo_url = get_post_meta( $product->get_id(), 'demo_url', true );
echo '<a class="fancybox iframe" data-width="1280" data-height="820" a href="https://lab.dmarket.pk/" target="_blank" rel="noopener"><button style="background: #007bc4; color: #ffffff; border-radius: 5px;margin-top: -120px !important;margin-left: 220px !important; padding-left: 19px; padding-right: 19px;"> Fix My Device</button></a>';
}
}
Replacing woocommerce_single_product_summary with woocommerce_after_add_to_cart_button should do the job
i've been searching around for a long time now and for some reason i cant get it to work.
i need to hide from admin email only, but for some reason it also apply to customer email. i want to hide, shipping from the other, and in client details, phone and email adress.
<?php
add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' );
function custom_woocommerce_get_order_item_totals( $totals ) {
unset( $totals['shipping'] );
return $totals;
}
///// so far is what i got to hide everything and not just the specific fields i wanted
function removing_customer_details_in_emails( $order, $sent_to_admin, $plain_text, $email ){
$wmail = WC()->mailer();
if($sent_to_admin)
remove_action( 'woocommerce_email_customer_details', array( $wmail, 'email_addresses' ), 20, 3 );
}
add_action( 'woocommerce_email_customer_details', 'removing_customer_details_in_emails', 5, 4 );
as mentioned above it hides the fields from the customer email as well and i need just for the admin email, and for some reason all i found did not work,
To hide billing phone, billing email and shipping address on email notifications sent to the admin, use the following instead:
add_action( 'woocommerce_email_customer_details', 'sent_to_admin_custom_email_addresses', 5, 4 );
function sent_to_admin_custom_email_addresses( $order, $sent_to_admin, $plain_text, $email ){
if( in_array( $email->id, array( 'new_order', 'cancelled_order', 'failed_order' ) ) ) {
$mailer = WC()->mailer();
remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20 );
add_action( 'woocommerce_email_customer_details', 'custom_email_addresses', 20, 1 );
}
}
function custom_email_addresses( $order ) {
if ( is_a( $order, 'WC_Order' ) ) :
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
<tr>
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
<address class="address">
<?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
</address>
</td>
</tr>
</table>
<?php
endif;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
Related: Removing customer details and addresses from email notification templates
In WooCommerce, "Get a custom field array values within WooCommerce email order meta" answer code to one of my previous questions, has given me the way to pull fields out of an array from the order post meta data.
But how would I take this code and adjust it, to add a heading above the new data and a table around the data, within the email?
I would like to add a <h2>Attendee Info</h2> above the output of the fields. In addition I'd like to wrap the field output in a <table>.
I would like to add a few of the fields as new columns within the admin Order Table screen, via some sort of function.
In addition, I would also like to be able to display some of these fields within the Admin Order Table, as new columns.
This can be done using instead woocommerce_email_order_details action hook this way:
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email )
{
$event = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );
if( ! is_array($event) ) return;
$event = isset($event[1][1]) ? $event[1][1] : '';
if( sizeof($event) == 0 ) return;
$custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';
// Set our array of needed data
$fields_array = [
__('First name') => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
__('Last name') => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
__('Title') => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
__('Organization') => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
__('Address') => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
__('City') => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
__('Postcode') => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
__('State') => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
];
if( ! $event ) return;
// The HTML Structure
$html_output = '<h2>' . __('Attendee Info') . '</h2>
<div class="discount-info">
<table cellspacing="0" cellpadding="6"><tbody>';
// Loop though the data array to set the fields
foreach( $fields_array as $label => $value ):
if( ! empty($value) ):
$html_output .= '<tr>
<th>' . $label . '</th>
<td>' . $value . '</td>
</tr>';
endif;
endforeach;
$html_output .= '</tbody></table>
</div><br>'; // HTML (end)
// The CSS styling
$styles = '<style>
.discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.discount-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
.discount-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>';
// The Output CSS + HTML
echo $styles . $html_output;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
or also using woocommerce_email_order_meta action hook, replacing the first line:
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
by this:
add_action('woocommerce_email_order_meta', 'woocommerce_email_order_meta', 25, 4 );
You will get something more clean and formatted with your title like:
To get this data displayed in admin order table is something much more complicate and too broad to be answer in this answer or on stackOverFlow.
You should better display that data in a custom meta box in order edit pages, which is much more easier and practical.
I'm using WooCommerce 3.1.1 and I am trying to replace the "price amount" with some text for specific product categories in New order noification for customers and admin.
I have almost tried everything but I am unable to locate the order item detail table for email notifications.
This email looks like this for now:
Any help would be really appreciated.
You will need first to read this official documentation, to learn about Overriding WooCommerce Templates via your active Theme
The templates that you need to change and override is emails/email-order-items.php
At line 58 for your WC version (Or line 55 in WC version 3.2+), you will replace:
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
By this (where you should set your own category and replacement text string):
<?php
## ---- Variables to define (below)---- ##
$categories = array( 'clothing' ); // The Product categories coma separated (IDs slugs or names)
$replacement_text = __( 'Replacement text (here)' ); // The replacement text
// Getting the email ID global variable (From our function below)
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
// When matching product categories, "New Order", "Processing" and "On Hold" email notifications
if( has_term( $categories, 'product_cat', $product->get_id() )
&& ( $email_id == 'new_order' || $email_id == 'customer_processing_order' || $email_id == 'customer_on_hold_order' ) )
$formated_line_subtotal = $replacement_text;
else
$formated_line_subtotal = $order->get_formatted_line_subtotal( $item );
?>
<td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $formated_line_subtotal; ?></td>
To Get the email ID you will need to add this in function.php file of your active child theme (or active theme):
// Setting the email_id as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['email_id_str'] = $email->id;
}
Now you will get this when product category matches and for "New Order" (admin), "Customer On Hold Order" and "Customer Processing Order" email notifications only:
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;
}