Display Shipping address2 in a text on WooCommerce email notifications - php

In the WooCommerce checkout field shipping_address_2, I have established a custom discount coupon. What I need is to get this order field value in a specific email notification.
Here is my code attempt:
add_action( 'woocommerce_email_before_order_table', 'add_content_to_specific_email', 20, 4 );
function add_content_to_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_processing_order' ) {
echo '<h2 class="email-upsell-title">Get 20% off</h2><p class="email-upsell-p">Thank you for making this purchase! Come back and use the code "<strong> get_post_meta( $order_id, '_shipping_address_2', true ) </strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
}
It doesn't works. What I want to transform this field value into a string between <strong> html tags.
Your help is welcome please.

You were very near to get what you were expecting. The correct was instead:
echo '<h2 class="email-upsell-title">Get 20% off</h2><p class="email-upsell-p">Thank you for making this purchase! Come back and use the code "<strong>' . get_post_meta( $order->ger_id(), '_shipping_address_2', true ) . '</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
But it's better to use WC_Order get_shipping_address_2() method.
Here is your revisited code:
add_action( 'woocommerce_email_before_order_table', 'add_content_to_specific_email', 20, 4 );
function add_content_to_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
// For customer processing and completed orders notifications
if ( in_array($email->id, ['customer_processing_order', 'customer_completed_order']) ) {
if( $coupon_code = $order->get_shipping_address_2() ) {
echo '<h2 class="email-upsell-title">'.__("Get 20% off").'</h2>
<p class="email-upsell-p">';
printf(
__("Thank you for making this purchase! Come back and use the code %s to receive a 20%% discount on your next purchase! %s."),
'"<strong>'.$coupon_code.'</strong>"',
'' . __("Click here to continue shopping") . ''
);
echo '</p>';
}
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Related

Dynamically enter coupon data into WooCommerce Email

When customer order is completed, I have a coupon created automatically by woocommerce which uses customers first name and order number as coupon code. Now im trying to enter it dynamically to woocommerce completed order by concatenating customers first name and order ID from the order class, but it's not showing up in the email. Which part of the code should I change to make it work ?
Thanks !
add_action( 'woocommerce_email_before_order_table', 'iq_add_content_specific_email', 20, 4 );
function iq_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' ) {
echo '<h2 class="email-upsell-title">We are giving you a free coupon for your next purchase !</h2><p class="email-upsell-p">Use the coupon code "<strong>$order->get_billing_first_name() . $order->get_id()</strong>" for 15% discount on your next purchase ! </p>';
}
}
I'm trying to concatenate $order->get_billing_first_name() . $order->get_id() but I'm getting the text output in the email instead of a dynamic result.

Woocommerce - Stop code from running based on product category

I am using a code in my functions.php that displays messages on my store based on weather the user is logged in or not. The code displays a message telling customers to sign up as a member to receive a 10% discount sitewide.
When as user is logged in the message displayed advises that discount is automatically applied at checkout.
The code works great, however, there is one particular product category that the discount is not valid for. I don't want this code to be executed on that particular category.
Can anyone assist with this?
This is a snippet of the code.
if (!is_user_logged_in())
add_filter( 'woocommerce_get_price_html', 'PW_text_after_price');
function PW_text_after_price($price){
$text_to_add_after_price = '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('<p><p>Sign-up for 10% members discount!</p>').'</span>'; //change text in bracket to your preferred text
return $price . $text_to_add_after_price;
}
if ( is_user_logged_in() )
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
// Echo content.
echo '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('Members discount automatically applied at checkout!').'</span>';
}
I think that you just need change first line like this:
if (!is_user_logged_in() && !is_category( '9' );) //change 9 to your category ID
add_filter( 'woocommerce_get_price_html', 'PW_text_after_price');
function PW_text_after_price($price){
$text_to_add_after_price = '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('<p><p>Sign-up for 10% members discount!</p>').'</span>'; //change text in bracket to your preferred text
return $price . $text_to_add_after_price;
}
if ( is_user_logged_in() )
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
// Echo content.
echo '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('Members discount automatically applied at checkout!').'</span>';
}

Custom message on completed order status email notification only for customer user role

I would like to improve this code from this answer, to display a message on completed order status emails only for customers, but not for other user roles (as subscribers, etc...).
Here is that code:
add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
How can I achieve it?
Thanks
To enable this custom message on email notification for complete order status and just for 'customer' user role, you have to get the user data related to the order, to get the user role. Then you will use it in your conditional.
Here is the code:
add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {
// Getting order user data to get the user roles
$user_data = get_userdata($order->customer_user);
if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' && in_array('customer', $user_data->roles) )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.

Displaying a coupon message on emails for completed order status only

I've got that code that displays a coupon message in the email orders if the customer has not used any in this order.
I would like to display that coupon message only on completed order instead on all mails.
add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-processing' ) )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
How can I achieve that?
Thanks
It is possible easily to display a custom message on emails with an if statement and 2 conditions, for "complete" order status only.
add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
This code goes on function.php file of your active child theme or theme
This code is tested and works perfect
BUT to autocomplete paid orders you need to add something:
WooCommerce: Auto complete paid Orders (depending on Payment methods)
Reference:
Add coupon to the processing order email only if the customer have not used one
WooCommerce: Auto complete paid Orders (depending on Payment methods)

Add coupon to the processing order email only if the customer have not used one

I came across this snippet that adds a coupon to the order mail.
I would like to make it appear in the processing order mail only if the customer have not used any coupon.
add_action( 'woocommerce_email_before_order_table', 'add_content', 20 );
function add_content() {
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
Thanks.
#update 2: New orders are most of the time in 'on-hold' status, but not in 'Processing'.
In this case, use this instead:
add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-on-hold' )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
Or to handle both 'on-hold' AND 'processing' statuses use this:
add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-processing' ) )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
This code goes on function.php file of your active child theme or theme
This code is tested and fully functional
TESTING:
To display the status of the emailed order you can add in the function this line:
echo '<p>The status of this order is: ' . $order->post_status . '</p>';
To display the coupons used (if they are some) add this:
echo '<p>Coupons used in this order are: '; print_r( $order->get_used_coupons() ); echo '</p>'
(this is just for testing purpose).
Original answer:
Yes it is possible easily adding an if statement with 2 conditions.
The first one detect if a coupon has not been used in the order used in the order:
empty( $order->get_used_coupons() )
And the second one will detect if the order is in "processing" status:
$order->post_status == 'wc-processing'
If that conditions are matched, your message is displayed using**'woocommerce_email_before_order_table'** hook:
Here is your customized code snippet:
add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-processing' )
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}
This code goes on function.php file of your active child theme or theme
This code is tested and fully functional

Categories