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 );
Related
In the functions.php file I have this shortcode which shows the download button of the last order placed by a woocommerce customer. Everything works fine for customers who have bought something, however if a customer has no downloads or no order, the layout breaks. I suppose this happens because there is nothing to be displayed.
So I would like to display a message for those who have no download or have not placed an order, I tried with else but it didn't work.
Sorry but I'm relatively new to php, can anyone help me figure out how to introduce a warning message?
add_shortcode( 'order_download' , 'last_order_info_08' );
function last_order_info_08( $downloads ){
$customer = new WC_Customer( get_current_user_id() );
$last_order = $customer->get_last_order();
$downloads = $last_order->get_downloadable_items();
if ($downloads) {
wc_get_template(
'button-downloads.php',
array(
'downloads' => $downloads,
'show_title' => true,
)
);
}
}
I modified the code a bit and should suit your need. You should be able to just replace the current code with this. Tested and confirmed locally on any page the shortcode is added it displayed. If you have some odd CSS setup it may or may not display correct on your site.
add_shortcode( 'order_download' , 'last_order_info_08' );
function last_order_info_08( $downloads ){
$customer = new WC_Customer( get_current_user_id() );
$last_order = $customer->get_last_order();
$shop_url = get_permalink( wc_get_page_id( 'shop' ) );
// Check to see if they have an order
if ($last_order) {
// Get download items from last order.
$downloads = $last_order->get_downloadable_items();
// Check to see if it contains downloadable items
if ($downloads) {
wc_get_template(
'button-downloads.php',
array(
'downloads' => $downloads,
'show_title' => true,
)
);
} else {
wc_add_notice( '<a class="woocommerce-Button button" href="'. $shop_url .'"> Browse products</a> No downloads available yet.', 'notice' );
}
} else {
wc_add_notice( '<a class="woocommerce-Button button" href="'. $shop_url .'"> Browse products</a> No downloads available yet.', 'notice' );
}
// HERE we print the notices
wc_print_notices();
}
This solution is based on Add an informative custom message in Woocommerce Checkout page
I've created a custom message but not sure if the syntax is correct. It displays fine on the front end, but need help to check it.
add_action( 'woocommerce_before_checkout_form', 'print_webcache_notice', 10 );
function print_webcache_notice() {
wc_print_notice( sprintf(
__("Having trouble checking out? Please clear your web browser cache!", "woocommerce"),
'<strong>' . __("Information:", "woocommerce") . '</strong>',), 'success' );
}
There was a little missing thing in your sprintf() content (the placeholder):
add_action( 'woocommerce_before_checkout_form', 'print_webcache_notice', 10 );
function print_webcache_notice() {
wc_print_notice( sprintf(
__("%sHaving trouble checking out? Please clear your web browser cache!", "woocommerce"),
'<strong>' . __("Information:", "woocommerce") . '</strong> '
), 'success' );
}
or without using sprintf() function:
add_action( 'woocommerce_before_checkout_form', 'print_webcache_notice', 10 );
function print_webcache_notice() {
$message = '<strong>' . __("Information:", "woocommerce") . '</strong> ';
$message .= __("Having trouble checking out? Please clear your web browser cache!", "woocommerce");
wc_print_notice( $message, 'success' );
}
Both work.
Now if you don't need "Information:" string at the beginning, simply use:
add_action( 'woocommerce_before_checkout_form', 'print_webcache_notice', 10 );
function print_webcache_notice() {
wc_print_notice( __("Having trouble checking out? Please clear your web browser cache!", "woocommerce"), 'success' );
}
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 );
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
I am trying to create a function that when a user updates her/his profile, the admin would get a mail notification. Not the data stored in wp_users, I would like to know changes stored in wp_usermeta. There are actually quite a lot of metakeys created with Ultimate Member.
The E-Mail should only contain the changed value, and best would be if the old value would be shown too.
As I am using the UltimateMember plugin.
According to this site, I would need this to get started:
function action_um_after_user_account_updated( $get_current_user_id ) {
// make action magic happen here...
};
add_action( 'um_after_user_account_updated', 'action_um_after_user_account_updated', 10, 1 );
After a lot of searching and mostly based on this I came up with this:
function action_um_after_user_account_updated( $get_current_user_id, $prev_value) {
$key = 'name';
$user = get_user_meta( $user_id, $key, $single);
$single = true;
if($prev_value->$key != $user->$key) {
$admin_email = "admin#site.com";
$message .= sprintf( __( 'New Name is: %s' ), $user ). "\r\n\r\n";
$message .= sprintf( __( 'Old name was: %s' ), $prev_value ). "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( '[DB] Name changed' ) ),$message );
}
};
// add the action
add_action( 'um_after_user_account_updated', 'action_um_after_user_account_updated', 10, 1 );
Well, it doesn't work at all. I don't know if I have a php code problem, or if the code is maybe out of date, but I can't get it to work.
I also included the pluggable.php which I need to use the wp_mail, as far as I know. (include ABSPATH . WPINC . '/pluggable.php';) in the headerfile of my theme (smartpress).
Wordpress-Version: 4.8.2
Ultimate Member Version: 1.3.88
PHP Version: 5.6
UPDATE:
I now made a plugin instead, which is kind of working. I receive a mail, and I get the values from the provided meta_keys. Now, I don't want to show every meta_value in the mail, only the ones that changed. Is there any way to store the previous values, just before the profile gets updated and compare against it or something?
Here is my current code:
function profile_update_name() {
$user_id = get_current_user_id();
$single = true;
$user_fnm = get_user_meta( $user_id, 'firstnamemother', $single);
$user_lnm = get_user_meta( $user_id, 'nachnamemother', $single);
$admin_email = "admin#site.com";
$message .= sprintf( __( $user_fnm .' '. $user_lnm . ' has updated the profile.')). "\r\n\r\n";
$message .= sprintf( __( 'New Name is: %s' ), $user_fnm .' '. $user_lnm ). "\r\n\r\n";
$message .= sprintf( __( 'Old name was: %s' ), $user_lnm ). "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( '[DB] Name changed' ) ),$message );
};
// add the action
add_action( 'um_user_after_updating_profile', 'profile_update_name', 1, 10 );
I believe that you problem is located here: $user = get_user_meta( $user_id, $key, $single);
Some variables you are passing in are empty. The following should get you the proper user meta:
$user = get_user_meta( $get_current_user_id, $key, true);
Here is an example of how you can get the last name of a user from the Codex:
<?php
$user_id = 9;
$key = 'last_name';
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>';
?>
You should var_dump() the $user variable to see how it returns the values.
EDIT:
As your question got updated, the second paremeter of the function get_user_meta() is the meta key. A meta key in this case is the part of a user that you want to retrieve. For example the first name or last name. Change the following in your code:
<?php
$user_fnm = get_user_meta( $user_id, 'name'/*I am 90% sure this one is right*/, $single);
$user_lnm = get_user_meta( $user_id, 'last_name', $single);
?>
This should retrieve the result you want. All you need to do now, is echo or use __($yourvar) to print it on the screen.