WooCommerce add order note to orderCompleted email - php

I am at my wits end here and have tried every solution I could find online for days and nothing works. The worst part is the problem should be solvable in about 4 lines of code but it just isn't working.
What I need:
When the order completed email goes out I want the order notes (not the customer notes, the actual Order Notes) added into the email. I can filter through them after but I cannot seem to get the notes to appear in the email at all. This is an example of an order note...on an order:
so far I have tried this code:
::PHP::
<?php
$comments = $order->get_customer_order_notes();
if($comments){
echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>';
foreach($comments as $comment) {
echo $comment->comment_content . '<br />';
}
}
?>
which is basically what I need except its targeting the customer_order_notes, which are comments users add into the order when they place it. like: "my dog will pick up my package, his name is lucky"
I have also written a lugin to get the notes based off other peoples, the base is this:
::PHP::
add_action( 'woocommerce_email_order_meta', 'bl_add_order_notes_to_completed_email', 10 );
function bl_add_order_notes_to_completed_email() {
global $woocommerce, $post;
// If the order is not completed then don't continue.
// if ( get_post_status( $post->ID ) != 'wc-completed' ){
// return false;
// }
$args = array(
'post_id' => $post->ID,
'status' => 'approve',
'type' => 'order_note'
);
// Fetch comments
$notes = get_comments( $args );
echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>';
echo '<ul class="order_notes" style="list-style:none; padding-left:0px;">';
// Check that there are order notes
if ( $notes ) {
// Display each order note
foreach( $notes as $note ) {
?>
<li style="padding:0px -10px;">
<div class="note_content" style="background:#d7cad2; padding:10px;">
<?php echo wpautop( wptexturize( $note->comment_content ) ); ?>
</div>
<p class="meta">
<abbr class="exact-date" title="<?php echo $note->comment_date; ?>"><?php printf( __( 'added on %1$s at %2$s', 'woocommerce-customer-order-notes-completed-order-emails' ), date_i18n( wc_date_format(), strtotime( $note->comment_date ) ), date_i18n( wc_time_format(), strtotime( $note->comment_date ) ) ); ?></abbr>
<?php if ( $note->comment_author !== __( 'WooCommerce', 'woocommerce-customer-order-notes-completed-order-emails' ) ) printf( ' ' . __( 'by %s', 'woocommerce-customer-order-notes-completed-order-emails' ), $note->comment_author ); ?>
</p>
</li>
<?php
}
}
echo '</ul>';
}
I don't really know why that isnt working. It looks like it should but it does nothing.
If anyne has a solution that will print those notes into my email...like seen in this image...I will love you forever.

Create a folder called woocommerce in your theme folder. In this folder, create a new folder called emails, and in this folder duplicate the customer-completed-order.php from wp-content/plugins/woocommerce/templates/emails. And add this snippet at line 51: Refer this article
<h2><?php _e( 'Order Notes', 'woocommerce' ); ?></h2>
<?php
$args = array(
'status' => 'approve',
'post_id' => $order->id
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo $comment->comment_content . '<br />';
endforeach;
?>

Try adding this code to your functions.php:
add_action( 'woocommerce_email_before_order_table', 'wc_add_order_notes_to_completed_emails', 10, 1 );
function wc_add_order_notes_to_completed_emails($order) {
if ( $email->id == 'customer_completed_order' ) {
echo '<h2>' . __( 'Order Notes', 'woocommerce' ) . '</h2>';
$order_notes = $order->get_customer_order_notes();
foreach ( $order_notes as $order_note ) {
echo '<p>' . $order_note->comment_content . '<p>';
}
}
}

I have been trying to achieve the same thing as Ecolis and came across a solution which may help other users. I simply wanted to echo an Order Note into an email. In my email template in my child theme folder I wrote the following code:
<?php echo wpautop( wptexturize( make_clickable( $customer_note ) ) ); ?>
Source of code - https://github.com/woocommerce/woocommerce/blob/master/templates/emails/customer-note.php

Answering this much later; but better than never. The final code I ended up using that worked and is still functioning as of today is the following:
?>
<h2><?php _e( 'Tracking ID', 'woocommerce' ); ?></h2>
<?php
$comments = $order->get_customer_order_notes();
$customer_comments = $order->get_order_notes();
foreach( $comments as $comment ){
if ( strpos( $comment -> comment_content, "MyTracking" ) !== false ){
echo $comment -> comment_content . '<br />';
}
}
foreach( $customer_comments as $comment_2 ){
if ( strpos( $comment_2 -> comment_content, "filterText" ) !== false ){
echo $comment_2 -> comment_content . '<br />';
}
}
This was put inside the email template customer-completed-order.php, taken from the woocommerce plugin's templates/emails/ directory. I placed the code in at the desired location and put the customer-completed-order.php file in my child theme in it's root directory within the following folders: woocommerce/emails/customer-completed-order.php
It then added both the customer and admin notes. I also added a filter, because my main goal was to get the tracking data posted to the admin order notes sent to the customer in their completed order email.

I stumbled across this older thread looking for a way to include the note that the customer places in "Order notes" on the checkout page in the Woocommerce New Order email received by the admin.
The code that #Martyn Gray posted above worked perfectly:
echo wpautop( wptexturize( make_clickable( $customer_note ) ) );
I copied the Woocommerce admin-new-order.php email template into my child theme and added it inside the existing code "user-defined additional content".
This is the original code:
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}
This is the updated code:
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
echo wpautop( wptexturize( make_clickable( $customer_note ) ) );
}
The customer's note now appears on all Woocommerce emails including new order, invoice, etc.
WP v5.6.2
WC v5.0.0
Hope that helps anyone that follows.

Related

How do I show the avatar image when hovering over an text/link? (html, CSS)

I create a member list and all members are listed by a php request.
That works fine so far, but now I want that the avatar image just shows up, when hovering over the member name.
Right now, the image shows up at the beginning of the line, as in the image1.
How it is
It would be nice that the image just shows up, when hovering over the name of a member, like image2.
how it should be
The Code for the related php request is this. In the figure-tag is the avatar and in the first and second "printf" line is the name.
<div class="wp-team-member wp-team-list-item author-<?php echo esc_attr( $user->ID ); ?> <?php echo esc_attr( $role_class ); ?>">
<figure class="wp-team-member-avatar author-image">
<?php echo wp_team_list()->get_avatar( $user ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
</figure>
<?php
if ( '' !== $last_name ) {
printf( '<p class="wp-team-member-description"><a href="#"><strong>%s, ', wp_kses_post( $last_name ) );
printf( '%s</strong> </a>- ', wp_kses_post( $first_name ) );
printf( 'LK Jahrgang %s - ', wp_kses_post( $lk_jahrgang ) );
printf( '%s, ', wp_kses_post( $ehem_schule ) );
printf( '%s, ', wp_kses_post( $position ) );
printf( '<span style="color:red;">%s</span>, ', wp_kses_post( $farbe ) );
printf( '%s, ', wp_kses_post( $billing_address_1 ) );
printf( '%s ', wp_kses_post( $billing_postcode ) );
printf( '%s</p>', wp_kses_post( $billing_city ) );
}
If anyone could help me with this, I'd appreciate this a lot.
As you can see in the printf-lines, I'm still a php rookie ;-)
You can make this as simple or complicated as you like. At it's most basic, you could do something like this:
.wp-team-member figure { display: none; }
.wp-team-member:hover figure { display:block; }
However, depending on the structure of your containing element you may want to toggle the opacity from to 0 to 1 rather switching the display mode.
Thanks mate,
that was really simple.
Regards

Show Product's Thumbnail Image as a Clickable link in Woocommerce Account Orders Page [duplicate]

Does somebody know how to insert the first 3 images of an order into the WooCommerce my-orders table?
Additionally I would like to add an "and more"-button, linked to the specific order (same link as order-number) with an if condition.
Hint: To stay more flexible it would be nice to get a solution without hooks, if possible.
I'd like to edit even more and overwrite the my-orders.php via child-theme later.
Here is the solution I use at the moment.
<div class="order-status">
<span>Order Status</span>
<?php
elseif ( 'order-status' === $column_id ) :
$order_color_check = $order->get_status();
if($order_color_check=="completed") :
echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
else :
echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
endif; ?>
</div>
Know that myaccount/my-orders.php is #deprecated since WC 2.6.0
My answer is via hooks, but editing via the template file should be done via myaccount/orders.php
The output of the html will need some CSS for styling (theme dependent)
// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
// Add a new column
$new_column['order-products'] = __( 'Products', 'woocommerce' );
// Return new column as first
return $new_column + $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
// Adds data to the custom "order-products" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
$count = 0;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Count + 1
$count++;
// First 3
if ( $count <= 3 ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// Instanceof
if ( $product instanceof WC_Product ) {
// Get image - thumbnail
$thumbnail = $product->get_image( array(50, 50) );
// Output
echo '<div class="product-thumbnail" style="display:inline-block;padding:2px;">' . $thumbnail . '</div>';
}
} elseif ( $count == 4 ) {
// Output "read more" button
echo '<span>'. __( 'Read more', 'woocommerce') . '</span>';
break;
}
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );

How to insert the first 3 product images of an order in WooCommerce "My account" orders table

Does somebody know how to insert the first 3 images of an order into the WooCommerce my-orders table?
Additionally I would like to add an "and more"-button, linked to the specific order (same link as order-number) with an if condition.
Hint: To stay more flexible it would be nice to get a solution without hooks, if possible.
I'd like to edit even more and overwrite the my-orders.php via child-theme later.
Here is the solution I use at the moment.
<div class="order-status">
<span>Order Status</span>
<?php
elseif ( 'order-status' === $column_id ) :
$order_color_check = $order->get_status();
if($order_color_check=="completed") :
echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
else :
echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
endif; ?>
</div>
Know that myaccount/my-orders.php is #deprecated since WC 2.6.0
My answer is via hooks, but editing via the template file should be done via myaccount/orders.php
The output of the html will need some CSS for styling (theme dependent)
// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
// Add a new column
$new_column['order-products'] = __( 'Products', 'woocommerce' );
// Return new column as first
return $new_column + $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
// Adds data to the custom "order-products" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
$count = 0;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Count + 1
$count++;
// First 3
if ( $count <= 3 ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// Instanceof
if ( $product instanceof WC_Product ) {
// Get image - thumbnail
$thumbnail = $product->get_image( array(50, 50) );
// Output
echo '<div class="product-thumbnail" style="display:inline-block;padding:2px;">' . $thumbnail . '</div>';
}
} elseif ( $count == 4 ) {
// Output "read more" button
echo '<span>'. __( 'Read more', 'woocommerce') . '</span>';
break;
}
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );

Echo category name in woocommerce

I would like to change the Text of the related products, which is at the end of the product detail page. At the moment I'm displaying This could be interesting by using this code
<h2><?php esc_html_e( 'This could be interesting', 'woocommerce' ); ?></h2>
What I would like to display is Our favorite category name
I tried to expand the code with this snippet, without any success
<?php echo wc_get_product_category_list($product->get_id()) ?>
How is it possible to get this function done?
Here is a little helper function that you could put in your functions.php
function get_favorite_category_title_for( $product_id ) {
$title = __('This could be interesting', 'woocommerce');
$cats = wp_get_post_terms( $product_id, 'product_cat' );
if( count($cats) > 0 ) {
$title = __( 'Our favorite ', 'woocommerce' ) . $cats[0]->name;
}
return $title;
}
and then replace the h2 tag with:
<h2><?php echo get_favorite_category_title_for( get_queried_object_id() ); ?></h2>
you can change get_queried_object_id with the $product->get_id() if you have access to $product object.

WooCommerce - unset "<product> removed noticeā€¦" on cart page

actions and filters. On my WooCommerce site I get the following message when I remove a product from the shopping cart:
"<product name>" removed. Undo?
Looking over WooCommerce source code I have found a conditional statement in class-wc-form-header.php as part of the function update_cart_action():
$removed_notice .= ' ' . __( 'Undo?', 'woocommerce' ) . '';
But I can't find the way to use it for eliminate this notice. I have try css solutions but it didn't work:
PS: that may not be the code snippet that is bothering me, but its the only one I have found that seems to make sense.
How can I remove this bothering notice?
Thanks.
You can do that in different ways:
1. Overriding the notices.php template:
You have first (if not done yet) to copy woocommerce templates folder inside your active child theme or theme, then rename it woocommerce. Then open/edit notices/notices.php and try to replace the code:
<?php
/**
* Show messages
* ... Blabla ... / ... blabla ...
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! $messages ){
return;
}
?>
<?php foreach ( $messages as $message ) : // Change your template code from here
if ( strpos( $message, 'removed' ) === false ) : ?>
<div class="woocommerce-info"><?php echo wp_kses_post( $message ); ?></div>
<?php endif;
endforeach; ?>
2. Using hooks:
function remove_added_to_cart_notice()
{
$notices = WC()->session->get('wc_notices', array());
foreach( $notices['notices'] as $key => &$notice){
if( strpos( $notice, 'removed' ) !== false){
$added_to_cart_key = $key;
break;
}
}
unset( $notices['notices'][$added_to_cart_key] );
WC()->session->set('wc_notices', $notices);
}
add_action('woocommerce_before_single_product','remove_added_to_cart_notice',1);
add_action('woocommerce_shortcode_before_product_cat_loop','remove_added_to_cart_notice',1);
add_action('woocommerce_before_shop_loop','remove_added_to_cart_notice',1);
3. Using CSS (with something like):
.woocommerce-cart .woocommerce-message {
display: none !important;
}
References:
Wordpress - Woocommerece remove "Added to Cart" message
New wc-notice-functions.php on github
I had to change this to get it to work. Specifically, the array field is singular notice or at least it is now.
$notices = WC()->session->get('wc_notices', array());
foreach( $notices['notice'] as $key => &$notice){
if( strpos( $notice, 'whilst' ) !== false){
$BadNotice_key = $key;
unset( $notices['notice'][$BadNotice_key] );
WC()->session->set('wc_notices', $notices);
break;
}
}
Just an update regarding Overriding the notices.php template:
<?php foreach ( $notices as $notice ) :
if ( strpos( $notice['notice'], 'removed' ) === false ) : ?>
<div class="woocommerce-message"<?php echo wc_get_notice_data_attr( $notice ); ?> role="alert">
<?php echo wc_kses_notice( $notice['notice'] ); ?>
</div>
<?php endif;
endforeach; ?>
Had to have add the 'notice' array key to the strpos method or it wasn't finding the "removed" string within the notice message. Hope this helps others who were having issues attempting to use this template override method.
There is a very simple answer to this problem as there is a hook that you can plug onto.
// This line is to be added in the functions.php
add_filter('woocommerce_cart_item_removed_notice_type', '__return_null');
1. Easy way: in wp-content/plugins/woocommerce/includes/class-wc-form-handler.php
2. remove/disable this line: wc_add_notice( $removed_notice ); (line 523) like this
if ( $product && $product->is_in_stock() && $product->has_enough_stock( $cart_item['quantity'] ) ) {
$removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
$removed_notice .= ' ' . __( 'Undo?', 'woocommerce' ) . '';
} else {
$removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
}
// wc_add_notice( $removed_notice );
}

Categories