In WooCommerce, on top of my thank you / order-received page, I've added a custom text, with the following code:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 1, 0);
function my_order_received_text(){
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . '</p></div>' ;
}
How can I get the email address of the customer added to the end of the custom text?
To get the customer billing email, you can use one of those:
The Woocommerce WC_Order method get_billing_email()
The WordPress function get_post_meta() with the meta key _billing_email from order ID.
Now you can set the text in 2 different locations:
1) On top of Order received page:
add_filter( 'woocommerce_thankyou_order_received_text', 'my_order_received_text', 10, 2 );
function my_order_received_text( $text, $order ){
if( ! is_a($order, 'WC_Order') ) {
return $text;
}
// Get Customer billing email
$email = $order->get_billing_email();
return $text . '<br>
<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) On bottom of Order received page:
Using the WC_Order method get_billing_email() this way:
add_action( 'woocommerce_thankyou', 'my_order_received_text', 10, 1 );
function my_order_received_text( $order_id ){
if( ! $order_id ){
return;
}
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
echo '<div class="my_thankyou2"><p>' . __('Your download link was sent to: ') . $email . '</p></div>' ;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Alternatively, using WordPress get_post_meta() function, replacing in the function:
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
$email = $order->get_billing_email(); // Get Customer billing email
By the following line:
$email = get_post_meta( $order_id, '_billing_email', true ); // Get Customer billing email
Related
I am setting up a website based on Wordpress and Woocommerce can you tell me how can I change the Shopping Item in woocommerce. I want the title to be just "Cart".
I've tried to change it with add_action, but without success, even got an error.
add_action('cart-title', 'change-cart-title', 10);
$message = __('Cart', 'woocommerce');
echo '<span class="cart-title">' . $message . '</span>';
}, 9);
How can I change the cart-title to just Cart?
You can use this to change cart page title
function wpa_change_my_basket_text( $translated_text, $text, $domain ){
if($translated_text == 'Cart:' )
$translated_text = 'Basket:';
return $translated_text;
}
add_filter( 'gettext', 'wpa_change_my_basket_text', 10, 3 );
Found this code on another thread but can't make it work.
PDF uploaded to wp-content/child-theme/.
Goal is to attach the pdf to the completed order emails that woocommerce will send out.
Not sure if customer_completed_order is correct?
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3 );
function attach_terms_conditions_pdf_to_email ( $attachments , $email_id, $email_object ) {
// Avoiding errors and problems
if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) ) {
return $attachments;
}
if( $email_id === 'customer_completed_order' ){
$your_pdf_path = get_stylesheet_directory() . '/Q-0319B.pdf';
$attachments[] = $your_pdf_path;
}
return $attachments;
}
There are some errors in your code: The $email_object function argument is the wrong variable name and should be instead $order to match with your first if statement.
Now for the attachement path linked to the theme, you will use:
get_stylesheet_directory() for a child theme
get_template_directory() for a parent theme (Website without a child theme)
The email Id customer_completed_order is correct to target Customer "completed" email notification.
As you are not using the $order variable argument in your code, ! is_a( $order, 'WC_Order' ) is not needed, so the working code will be:
add_filter( 'woocommerce_email_attachments', 'attach_pdf_file_to_customer_completed_email', 10, 3);
function attach_pdf_file_to_customer_completed_email( $attachments, $email_id, $order ) {
if( isset( $email_id ) && $email_id === 'customer_completed_order' ){
$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
}
return $attachments;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
For a parent theme replace:
$attachments[] = get_stylesheet_directory() . '/Q-0319B.pdf'; // Child theme
by the following line:
$attachments[] = get_template_directory() . '/Q-0319B.pdf'; // Parent theme
I would like to add "We ship to {country name}" in WooCommerce header based on user geoip country name?
I would like to write html content in the header of my WooCommerce store, such as We ship to "Your Country name".
Any help will be really appreciated?
I have WooCommerce geolocation enabled already.
You can make a custom function based on WC_Geolocation Class this way:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ]; // return the country name
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE
You will add the following code in your child theme's header.php file:
1) in between html code:
<?php printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() ); ?>
2) or in between php code:
printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() );
Converting this to Shortcode:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return sprintf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', WC()->countries->countries[ $country ] );
}
add_shortcode('geoip_country', 'get_user_geo_country');
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Normal shortcode usage (in the backend text editor):
[geoip_country]
or in php code:
echo do_shortcode( "[geoip_country]" );
In woocommerce I am using following code for adding PDF files as email attachment:
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object ) {
$your_pdf_path1 = get_stylesheet_directory() . '/pdf/ano1.pdf';
$your_pdf_path2 = get_stylesheet_directory() . '/pdf/ano2.pdf';
$attachments[] = $your_pdf_path1;
$attachments[] = $your_pdf_path2;
return $attachments;
}
My problem is that attachment is sending always for all email to customers. I would like to send email attachment only in case that Order status is "on-hold".
How is possible to know status of my order and send email attachment only for this case?
Updated
You need to use the $id argument with 'customer_on_hold_order' as email ID in your function as a condition…
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object ) {
// Continue if it's customer_on_hold email notiication
if ( $id != 'customer_on_hold_order' ) return $attachments;
$your_pdf_path1 = get_stylesheet_directory() . '/pdf/ano1.pdf';
$your_pdf_path2 = get_stylesheet_directory() . '/pdf/ano2.pdf';
$attachments[] = $your_pdf_path1;
$attachments[] = $your_pdf_path2;
return $attachments;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
I am overriding the admin email notification template in WooCommerce and when the order transaction is successful I want to add the transaction id in the email.
I tried $order->get_transaction_id() as explained here in the admin email template, but it returns an empty result.
I then tried this in the admin email template:
do_action('getOrderTransactionID', $order->id);
And in my theme's functions.php I added this but this function doesn't return anything either.
add_action('getOrderTransactionID', 'getOrderTransactionIDForEmail');
function getOrderTransactionIDForEmail($orderId){
echo get_metadata('post', $orderId, '_transaction_id', true);
//get_post_data doesn't return anything either
//get_post_meta( $orderId, '_transaction_id', true);
}
In the wp_postmeta table, the _transaction_id meta key is saved after each successful transaction. Why then am I unable to retrieve the _transaction_id which is already in the database?
What gateway are you using? If you don't see _transaction_id then you need to save the returned transaction_id manually in your gateway plugin. Check out this page
Try saving transaction id in your payment gateway file, then try to see if it displays when you do var_dump(get_post_custom($order->id));
add_post_meta( $order->id, '_transaction_id', YOUR_TRANSACTION_ID, true );
The following is an example of how to add data to email templates using existing hooks.
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field;</p>;
}
}
add_action( 'woocommerce_email_order_meta', 'kia_display_email_order_meta', 30, 3 );