Custom formatted adresses section in WooCommerce email notifications sent to admin - php

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

Related

Set a min unit displayed price for variable products in Woocommerce and show it on new order email

I'm building a webshop in WooCommerce for selling Cream Soaps and Antiseptic Gels. They are being sold by unit, by box, or by pallet (3 different attributes). I am using a code snippet to insert the unit price from the product edit page:
https://paste.pics/a2f918d2b3e233dce1e445f6b843561f (sorry about these links i cant post image so far.)
this is the code to get this new field to add the unit price:
// Backend: Add and display a custom field for variable products
add_action('woocommerce_product_options_general_product_data', 'add_custom_product_general_field');
function add_custom_product_general_field()
{
global $post;
echo '<div class="options_group hide_if_simple hide_if_external">';
woocommerce_wp_text_input(array(
'id' => '_min_unit_price',
'label' => __('Min Unit price', 'woocommerce') ,
'placeholder' => '',
'description' => __('Enter the minimum unit price here.', 'woocommerce'),
'desc_tip' => 'true',
));
echo '</div>';
}
// Backend: Save the custom field value for variable products
add_action('woocommerce_process_product_meta', 'save_custom_product_general_field');
function save_custom_product_general_field($post_id)
{
if (isset($_POST['_min_unit_price'])){
$min_unit_price = sanitize_text_field($_POST['_min_unit_price']);
// Cleaning the min unit price for float numbers in PHP
$min_unit_price = str_replace(array(',', ' '), array('.',''), $min_unit_price);
// Save
update_post_meta($post_id, '_min_unit_price', $min_unit_price);
}
}
I want to get this unit price and print to the new order email (image below)
https://paste.pics/30b2584762396a558f3591461ae87be1
I added the new field (unit price) through email-order-details.php file
And now i need to print this unit price that i put on the product (first screenshot)
on the email-order-items.php file.
The code from email-order-items.php
<?php
/**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce/Templates/Emails
* #version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
$text_align = is_rtl() ? 'right' : 'left';
foreach ( $items as $item_id => $item ) :
$product = $item->get_product();
$sku = '';
$purchase_note = '';
$image = '';
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
continue;
}
if ( is_object( $product ) ) {
$sku = $product->get_sku();
$purchase_note = $product->get_purchase_note();
$image = $product->get_image( $image_size );
}
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
<?php
// Show title/image etc.
if ( $show_image ) {
echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
}
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// SKU.
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta( $item );
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?>
</td>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( wc_price( $order->get_item_subtotal( $item ), array( 'currency' => $order->get_currency() ) ) ) ?>
</td>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ) ); ?>
</td>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( $order->get_formatted_line_subtotal( $item ) ); ?>
</td>
</tr>
<?php
if ( $show_purchase_note && $purchase_note ) {
?>
<tr>
<td colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php
echo wp_kses_post( wpautop( do_shortcode( $purchase_note ) ) );
?>
</td>
</tr>
<?php
}
?>
<?php endforeach; ?>
More specifically on this line here:
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( wc_price( $order->get_item_subtotal( $item ), array( 'currency' => $order->get_currency() ) ) ) ?>
</td>
How can i hook the unit price and echo it on the "unit price" field?
Anyone has experience and a possible solution with this issue? Any help would be highly appreciated. Thanks!

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

Display order customer note in Woocommerce email notifications

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.

Hide price items for specific product categories in Woocommerce email notification

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:

WordPress: WooCommerce email_order_items_table remove price

I’m new to PHP and also new to WooCommerce.
I want to edit the woo-custom-emails plugin.
As a user I have the possibility to set {email_order_items_table} in my text which outputs the product name, followed by its quantity, followed by the price. I want to remove the price!
Here is what it does in code:
// file: my_plugin/admin/class-wcemails-instance.php
$this->find[] = '{email_order_items_table}';
$this->replace[] = $this->object->email_order_items_table();
here is what the that function does:
// file: woocommerce/includes/abstracts/abstract-wc-order.php
/**
* Output items for display in html emails.
*
* #param array $args Items args.
* #param null $deprecated1 Deprecated arg.
* #param null $deprecated2 Deprecated arg.
* #param null $deprecated3 Deprecated arg.
* #param null $deprecated4 Deprecated arg.
* #param null $deprecated5 Deprecated arg.
* #return string
*/
public function email_order_items_table( $args = array(), $deprecated1 = null, $deprecated2 = null, $deprecated3 = null, $deprecated4 = null, $deprecated5 = null ) {
ob_start();
if ( ! is_null( $deprecated1 ) || ! is_null( $deprecated2 ) || ! is_null( $deprecated3 ) || ! is_null( $deprecated4 ) || ! is_null( $deprecated5 ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' );
}
$defaults = array(
'show_sku' => false,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => false,
'sent_to_admin' => false
);
$args = wp_parse_args( $args, $defaults );
$template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, apply_filters( 'woocommerce_email_order_items_args', array(
'order' => $this,
'items' => $this->get_items(),
'show_download_links' => $this->is_download_permitted() && ! $args['sent_to_admin'],
'show_sku' => $args['show_sku'],
'show_purchase_note' => $this->is_paid() && ! $args['sent_to_admin'],
'show_image' => $args['show_image'],
'image_size' => $args['image_size'],
'plain_text' => $args['plain_text'],
'sent_to_admin' => $args['sent_to_admin']
) ) );
return apply_filters( 'woocommerce_email_order_items_table', ob_get_clean(), $this );
}
and that is the template:
// file: woocommerce/templates/emails/email-order-items.php
<?php
/**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.1.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
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 ) ) {
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php
// Show title/image etc
if ( $show_image ) {
echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
}
// Product name
echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
// SKU
if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
echo ' (#' . $_product->get_sku() . ')';
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
// Variation
if ( ! empty( $item_meta->meta ) ) {
echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
}
// File URLs
if ( $show_download_links ) {
$order->display_item_downloads( $item );
}
// allow other plugins to add additional product information here
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?></td>
<td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ); ?></td>
<td class="td" style="text-align:left; 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>
</tr>
<?php
}
if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
<tr>
<td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
</tr>
<?php endif; ?>
I could replace the template as described in the comments BUT that would overwrite all emails to hide the price and I actually want to keep the price in all other emails that are not send through the plugin. I have no idea on how to make this conditional.
So here is my question: How can I hook that email_order_items_table() function to not output the price?
PS: How can I see what is inside $this->object or how can I output in a human readable form everything inside the $thisobject?
You can use either of this to see the object printed as an array in human readable form.
echo '<pre>';print_r($this->object);exit;
echo '<pre>';print_r($this);exit;
Or you may need to log the object into error log file like below
error_log(print_r($this->object, 1), true);
In woocommerce all templates can be override if you put it on your theme,
so:
woocommerce/templates/emails/email-order-items.php
can be override if you put it on:
wp-content/themes/{your theme}/woocomerce/{the template}
in this case
content/themes/{your theme}/woocommerce/emails/email-order-items.php
Then you can change the template, so it dont draw the price.
take out this line on the file that is on your themes directory. Be carefull some time you change somthing on the plugin directory and you will lose it on the next update.
<td class="td" style="text-align:left; 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>
br

Categories