Woocommerce change order recieved page title - php

I am trying to change/add in the title of the "Order Recieved" Woocommerce page.
The below snippet works - I am able to change the pre-existing TEXT with the following code:
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = $str . ' We have emailed the purchase receipt to you.';
return $new_str;
}
The below snippet does not work. - I am unable to change/add the TITLE and also pass in the username to personalise it. Here is the code and also an image of the output I am trying to achieve....The "You are awesome FIRSTNAME" added in.
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
This should be possible as there are examples on how to do it, such as here...I just can't figure out why the main title wont even appear?

As a workaround I inspected the CSS and changed the text below the header to be a larger size and the font family required.
Then through the below PHP I created the custom text with the customer name in the header.
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = sprintf( "You are awesome, %s :) - We've recieved your order.", esc_html( $order->get_billing_first_name() ) );
return $new_str;
}

2022 update:
function ps_title_order_received( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
unset( $order );
}
}
if ( isset ( $order ) ) {
$title = sprintf( "Thank you, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
add_filter( 'the_title', 'ps_title_order_received', 10, 2 );

Related

Target specific WooCommerce email notifications when using the "woocommerce_order_item_meta_start" hook

I have this function which adds custom meta field to the product detail in all WooCommerce emails. But I need to show only after the order is paid (this can be also just the "completed" email).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
I hoped that I can nest it inside if ( $email->id == 'customer_completed_order' ) {}, so the final code will look like this:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
if ( $email->id == 'customer_completed_order' ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
}
But it stops working after that change. Any advice?
As you can see in your code attempt, $email is not part of the woocommerce_order_item_meta_start hook. So to target certain WooCommerce email notifications, you will need a workaround.
Step 1) creating and adding a global variable, via another hook that only applies to WooCommerce email notifications.
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
Step 2) In the hook woocommerce_order_item_meta_start, use the global variable so we can target certain WooCommerce email notifications
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the email ID global variable
$ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
$email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
// NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma
if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) {
// Get meta
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
// OR use to get meta
// $ot_address = $item->get_meta( 'ot_address' );
if ( ! empty( $ot_address ) ) {
printf( '<div>' . __( 'Terms: %s', 'woocommerce' ) . '</div>', $ot_address );
}
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );

WooCommerce customized email only for processing and completed orders

I am using a plugin to send custom referral codes in the email orders but now I found that every order email has the code inserted.
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
$code = get_user_meta($user_id, "gens_referral_id", true);
} else {
$code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}
if( $plain_text ){
_e('Your referral code is: ','gens-raf') . $code;
} else {
echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 3 );
I was messing around with
if ( $email->id == 'customer_completed_order' )
But gave me error from the php code builder. The main issue is to send that email only to completed and/or processing orders instead of refunded, cancelled and so on.
Thank you!
You're getting the error because you didn't add the fourth $email argument available to the woocommerce_email_customer_details hook.
The content will only be displayed in the Completed order and
Processing order templates.
Try this:
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 4 );
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' || $email->id == 'customer_processing_order' ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
$code = get_user_meta($user_id, "gens_referral_id", true);
} else {
$code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}
if( $plain_text ){
_e('Your referral code is: ','gens-raf') . $code;
} else {
echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}
}
}
The code has been tested and works.
The variable $email is the 4th missing argument from your hooked function, that you need to target specific email notifications.
Use instead (for woocommerce 3 and above):
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 100, 4 );
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific notifications: Customer processing and completed orders
if ( in_array( $email->id, ['customer_processing_order', 'customer_completed_order'] ) ) {
$gens_raf = get_user_meta( $order->get_user_id(), "gens_referral_id", true );
$code = empty( $gens_raf ) ? $order->get_billing_email() : $gens_raf;
if( $plain_text ){
printf( __('Your referral code is: %s', 'gens-raf'), $code );
} else {
$output = sprintf( __("Your referral code is: %s", "gens-raf"), get_home_url() .'?raf='. $code );
echo '<p style="text-align:center;margin-top:10px;">' . $output . '</p>';
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Change WooCommerce thankyou page title based on order status

Attempting to change the title of the thank you page based on specific order statuses, by combining a filter and action as follows:
add_action( 'woocommerce_thankyou', 'order_thank_you_status' );
function order_thank_you_status( $order_id ){
// Get an instance of the `WC_Order` Object
$order = wc_get_order( $order_id );
// Get the order number
$order_number = $order->get_order_number();
// Get the order status name
$status_name = wc_get_order_status_name( $order->get_status() );
// Get the order key
$test_order_key = $order->get_order_key();
if ( $order->has_status('on-hold') || $order->has_status('mockup-requested') || $order->has_status('mockup-sent')|| $order->has_status('mockup-approved')) {
echo 'helllo world';
add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
function woo_title_order_received( $title, $id ) {
if ( function_exists( 'is_order_received_page' ) &&
is_order_received_page() && get_the_ID() === $id ) {
$title = "Mockup request received";
}
return $title;
}
}
}
but I have tried multiple ways to combine the two and without any success.
You can use the following to change "Order received" page title based on order statuses:
add_action( 'the_title', 'change_order_received_page_title_based_on_order_status' );
function change_order_received_page_title_based_on_order_status( $title ){
if ( is_wc_endpoint_url('order-received') && $title === 'Order received' ) {
global $wp;
$targeted_statuses = array('on-hold', 'mockup-requested', 'mockup-sent', 'mockup-approved' );
// Get an instance of the `WC_Order` Object
$order = wc_get_order( absint($wp->query_vars['order-received']) );
if ( is_a( $order, 'WC_Order' ) && in_array( $order->get_status(), $targeted_statuses ) ) {
return __("Mockup request received", "woocommerce");
}
}
return $title;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Save WooCommerce custom Session Variables as order meta data

I'm trying to store custom session variables in database. Then, show them inside new order email and order details in WooCommerce Admin.
I have custom variables inside session:
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
Firstly, I added custom variables to database with this code;
// Storing session variables for using them in order notifications
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order, $data ) {
if ( $_POST['konumu'] ) update_meta_data( $order_id, '_konum', esc_attr( $_POST['konumu'] ) );
if ( $_POST['masa_no'] ) update_meta_data( $order_id, '_masa', esc_attr( $_POST['masa_no'] ) );
}
Second, I added this variable's data to a new order email for admin.
// Show this session variables in new order email for admin
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_ekle', 20, 4 );
function konumu_emaile_ekle( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order->get_id(), '_konum', true ) . '</p>';
if ( get_post_meta( $order->get_id(), '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order->get_id(), '_masa', true ) . '</p>';
}
Last part of the code is shown session variables data in WooCommerce order page;
// Show session variable in woocommerce order page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_admine_ekle', 10, 1 );
function konumu_admine_ekle( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order_id, '_konum', true ) . '</p>';
if ( get_post_meta( $order_id, '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order_id, '_masa', true ) . '</p>';
}
But, it does not work. When customer made an order, it gave an error "We were unable to process your order, please try again."
Updated: There are some mistakes in your code when you are trying to save your custom data from session as order meta data and display it on emails and admin order pages…
Your first function is correct (oturum_degiskeni_olustur)…
Assuming that data is passed through URL like: website.com/?konumu=newyork&masa_no=12
Here is the revisited code:
// Unchanged
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Save custom session data as order meta data
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order ) {
$data = WC()->session->get( 'custom_data' ); // Get custom data from session
if ( isset($data['konum']) ) {
$order->update_meta_data( '_konum', $data['konum'] );
}
if ( isset($data['masa']) ) {
$order->update_meta_data( '_masa', $data['masa'] );
}
WC()->session->__unset( 'custom_data' ); // Remove session variable
}
// Show this session variables in new order email for admin and in woocommerce order page
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_admine_ekle', 20 );
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_emaile_admine_ekle' );
function konumu_emaile_admine_ekle( $order ) {
if ( $konum = $order->get_meta( '_konum' ) )
echo '<p><strong>Masa Numarası :</strong> ' . $konum . '</p>';
if ( $masa = $order->get_meta( '_masa' ) )
echo '<p><strong>Konum :</strong> ' . $masa . '</p>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and Works.
On the email notification before customer details:
On admin order edit page (under billing phone):
Tested on WooCommerce 4.2+ under storefront theme

Customize Order received page based on shipping method in WooCommerce

How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
$chosen_titles = array();
$available_methods = $wp->shipping->get_packages();
$chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'Delivery price on request', $chosen_titles ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
}
return $title;
}
is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )…
Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".
Try this instead:
add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
if ( empty($order_id) || $order_id == 0 )
return $title; // Exit
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key )
return $title; // Exit
$method_title_names = array();
// Loop through Order shipping items data and get the method title
foreach ($order->get_items('shipping') as $shipping_method )
$method_title_names[] = $shipping_method->get_name();
if( in_array( 'Delivery price on request', $method_title_names ) ) {
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Similar: Adding custom message on Thank You page by shipping method in Woocommerce

Categories