I'm trying to add a message to the order-received (Thank You) page, only if the order is using Free Shipping. The message can either replace the standard "Thank you..." message, or can be in addition to.
Here is the code I'm working with. It's based off of the answer here: Customize Order received page based on shipping method in WooCommerce
//add message to order received if outside delivery area
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $thankyou_text, $order ) {
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
$method_title_names = array();
if( in_array( 'Free shipping', $method_title_names ) ) {
return sprintf( __("%s <div class=\"outside-delivery-checkout\"><b>PLEASE NOTE:</b><br />Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.</div>", "woocommerce"),
$thankyou_text
);
}
}
return $thankyou_text;
}
I can't get it to work correctly, and not sure what's wrong. Any help is greatly appreciated.
You need simply the following (where "Free shipping" is the name of your free shipping method):
add_filter( 'woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
function woo_change_order_received_text( $text, $order ) {
if( $order->get_shipping_method() == 'Free shipping' ) {
$text .= ' <div class=\"outside-delivery-checkout\"><strong>'. __("PLEASE NOTE", "woocommerce") . ':</strong><br />'.__("Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.", "woocommerce") . '</div>';
}
return $text;
}
Similar: Code goes in function.php file of your active child theme (or active theme). Tested and works.
Customize Order received page based on shipping method in WooCommerce
Related
I need to check on the checkout page whether payment has been successful or not and display the message: 'Your payment has been successful', and then redirect to the thank you page (which is customized per product by the plugin Woo Product Tools). I've been trying to find hooks on the Woo documentation, but no luck so far. The latest code I have is:
add_action( 'woocommerce_after_checkout_validation', 'message_after_payment' );
function message_after_payment(){
global $woocommerce;
$order = wc_get_order( $order_id );
if ( $order->has_status('processing') ){
wc_add_notice( __("Your payment has been successful", "test"), "success" );
}
}
How can this be achieved?
You can't display a "success" notice on checkout page once you submit an order (place an order)… You can do that in Order Received (thankyou) page. Also in your code, $order_id is not defined…
So the right hook is woocommerce_before_thankyou using wc_print_notice() function instead:
add_action( 'woocommerce_before_thankyou', 'success_message_after_payment' );
function success_message_after_payment( $order_id ){
// Get the WC_Order Object
$order = wc_get_order( $order_id );
if ( $order->has_status('processing') ){
wc_print_notice( __("Your payment has been successful", "woocommerce"), "success" );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Addition: Display a custom html message instead of a Woocommerce notice
Just replace the code line:
wc_print_notice( __("Your payment has been successful", "woocommerce"), "success" );
with for example this:
echo '<p class='cudtom-message"> . __("Your payment has been successful", "woocommerce"), "success" ) . '</p>';
You can add your own html as you like around the text message.
I am trying to add additional email recipient based on payment method Id on WooCommerce "New order" email notification.
Here is my code:
function at_conditional_admin_email_recipient($recipient, $order){
// if( ! is_a($order, 'WC_Order') ) return $recipient;
if ( get_post_meta($order->id, '_payment_method', true) == 'my_custom_gateway_id' ) {
$recipient .= ', xx1#xx.com';
} else {
$recipient .= ', xx2#xx.com';
}
return $recipient;
};
add_filter( 'woocommerce_email_recipient_new_order', 'at_conditional_admin_email_recipient', 10, 2 );
But the hook doesn't seem firing my function. What could be the reason?
Your code is outdated since Woocommerce 3, try the following instead:
add_filter( 'woocommerce_email_recipient_new_order', 'payment_id_based_new_order_email_recipient', 10, 2 );
function payment_id_based_new_order_email_recipient( $recipient, $order ){
// Avoiding backend displayed error in Woocommerce email settings (mandatory)
if( ! is_a($order, 'WC_Order') )
return $recipient;
// Here below set in the array the desired payment Ids
$targeted_payment_ids = array('bacs');
if ( in_array( $order->get_payment_method(), $targeted_payment_ids ) ) {
$recipient .= ', manager1#gmail.com';
} else {
$recipient .= ', manager2#gmail.com';
}
return $recipient;
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
Also sometimes the problem can be related to a wrong payment method Id string in your code (so try first for example with WooCommerce "cod" or "bacs" payment methods ids, to see if the code works).
I'm attempting to display a custom thank you message on the Woocommerce order received page if one of three specific coupon codes are used during checkout.
Our Woocommerce version is 2.6.11.
I've tried a few variations of the below code but cannot get it working, am I doing something incorrectly?
//show custom coupon thankyou
function coupon_thankyou($order_id) {
$coupon_id = '1635';
$order = wc_get_order($order_id);
foreach( $order->get_items('coupon') as $coupon_item ){
if( $coupon_item->get_code() = $coupon_id ){
echo '<p>This is an custom thank you.</p>';
}
}
}
add_action('woocommerce_thankyou','coupon_thankyou');
There is a mistake in your IF statement condition where = has to be replaced with == or ===. Also with coupons, you need to use the coupon code slug (but not the post ID).
To display a message on Order received page better use woocommerce_thankyou_order_received_text filter hook, this way (for Woocommerce 3+):
// On "Order received" page (add a message)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_applied_coupon_message', 10, 2 );
function thankyou_applied_coupon_message( $text, $order ) {
$coupon_code = '1635'; // coupon code name
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon->get_code() === $coupon_code ){
$text .= '<p>'.__("This is an custom thank you.").'</p>';
}
}
return $text;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
Updated
For the versions of Woocommerce before 3.0, you should use use the following instead:
// On "Order received" page (add a message)
add_action( 'woocommerce_thankyou', 'thankyou_applied_coupon_message', 10, 1 );
function thankyou_applied_coupon_message( $order_id ) {
$coupon_code = '1635'; // coupon code name
$order = wc_get_order( $order_id );
foreach( $order->get_items('coupon') as $coupon ){
if( $coupon['name'] === $coupon_code ){
echo '<p>'.__("This is an custom thank you.").'</p>';
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I want to be able to change who receives the Woocommerce email notifications based on what role the user is when ordering.
For example, If the user is logged in as a Wholesale Customer then a different email will be notified.
I've found how to change it when a new order is complete using the woocommerce_email_recipient_new_order hook but i can't find any hooks related to the failed or cancelled notifications.
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
function sv_conditional_email_recipient( $recipient, $order ) {
// Bail on WC settings pages since the order object isn't yet set yet
// Not sure why this is even a thing, but shikata ga nai
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
// just in case
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
if ( in_array( 'wholesale_customer', (array) $user->roles ) ) {
$recipient .= ', shaun#example.com';
return $recipient;
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
Can anyone help please?
The hook you are already using is a composite hook: woocommerce_email_recipient_{$this->id}, where {$this->id} is the WC_Email ID like new_order. So you can set any email ID instead to make it work for the desired email notification.
Below You have the 3 hooks for "New Order", "Cancelled Order" and "Failed Order" that you can use for the same hooked function.
In your function, I have removed some unnecessary code and completed the code to get the customer data (the user roles) related to the order:
add_filter( 'woocommerce_email_recipient_new_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_cancelled_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'user_role_conditional_email_recipient', 10, 2 );
function user_role_conditional_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Get the customer ID
$user_id = $order->get_user_id();
// Get the user data
$user_data = get_userdata( $user_id );
// Adding an additional recipient for a custom user role
if ( in_array( 'wholesale_customer', $user_data->roles ) )
$recipient .= ', shaun#example.com';
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
In WooCommerce I need to apply a custom handling fee for a specific payment gateway. I have this piece of code from here: How to Add Handling Fee to WooCommerce Checkout.
This is my code:
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = 5.00;
$woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}
This function add a fee to all transactions.
Is it possible to tweak this function and make it apply for specific payment method only ?
The other problem is that I want this fee to be applied on cart. Is it possible?
I will welcome any alternative method as well. I know about the similar "Payment Gateway Based Fees" woo plugin, but I can't afford it.
2021 UPDATE
Note: All payment methods are only available on Checkout page.
The following code will add conditionally a specific fee based on the chosen payment method:
// Add a custom fee (fixed or based cart subtotal percentage) by payment
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_payment_id = WC()->session->get('chosen_payment_method');
if ( empty( $chosen_payment_id ) )
return;
$subtotal = $cart->subtotal;
// SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
$targeted_payment_ids = array(
'cod' => 8, // Fixed fee
'paypal' => 5 * $subtotal / 100, // Percentage fee
);
// Loop through defined payment Ids array
foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
if ( $chosen_payment_id === $payment_id ) {
$cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
}
}
}
You will need the following to refresh checkout on payment method change, to get it work:
// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
wc_enqueue_js( "jQuery( function($){
$('form.checkout').on('change', 'input[name=payment_method]', function(){
$(document.body).trigger('update_checkout');
});
});");
}
Code goes in functions.php file of your active child theme (or active theme). tested and works.
How to find a specific payment method ID in WooCommerce Checkout page?
The following will display on checkout payment methods the payment Id just for admins:
add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
$title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
}
return $title;
}
Code goes in functions.php file of your active child theme (or active theme). Once used, remove it.
Similar answer:
Add a custom fee for a specific payment gateway in Woocommerce
Add a fee based on shipping method and payment method in Woocommerce
Percentage discount based on user role and payment method in Woocommerce
First of all there are some key things we need to understand:
We are going to use the only filter hook which is woocommerce_cart_calculate_fees
In order to get user selected payment method we must retrieve it from user sessions using this method WC()->session->get( 'chosen_payment_method' )
calculate_fees() and calculate_totals() methods are not necessary.
We are going to add the fee using cart method WC()->cart->add_fee() which accepts two parameters – the first one, is the fee description, the second one – fee amount.
And one more thing – we will need a payment method slug, the easiest way to get it described in this tutorial https://rudrastyh.com/woocommerce/add-id-column-to-payment-methods-table.html
Let's go now:
add_action( 'woocommerce_cart_calculate_fees', 'rudr_paypal_fee', 25 );
function rudr_paypal_fee() {
if( 'paypal' == WC()->session->get( 'chosen_payment_method' ) ) {
WC()->cart->add_fee( 'PayPal fee', 2 ); // let's add a fee for paypal
}
}
It would be also great to refresh the checkout every time payment gateway is changed, it is easy to do using this code:
jQuery( function( $ ) {
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$( 'body' ).trigger( 'update_checkout' );
});
});
That's it. Everything is described in details here as well: https://rudrastyh.com/woocommerce/charge-additional-fees-based-on-payment-gateway.html
For anyone else looking to do this, I wanted to add a fee for Bank Transfers (BACS), here is the method I used:
//Hook the order creation since it is called during the checkout process:
add_filter('woocommerce_create_order', 'my_handle_bacs', 10, 2);
function my_handle_bacs($order_id, $checkout){
//Get the payment method from the $_POST
$payment_method = isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : '';
//Make sure it's the right payment method
if($payment_method == "bacs"){
//Use the cart API to add recalculate fees and totals, and hook the action to add our fee
add_action('woocommerce_cart_calculate_fees', 'my_add_bacs_fee');
WC()->cart->calculate_fees();
WC()->cart->calculate_totals();
}
//This filter is for creating your own orders, we don't want to do that so return the $order_id untouched
return $order_id;
}
function my_add_bacs_fee($cart){
//Add the appropriate fee to the cart
$cart->add_fee("Bank Transfer Fee", 40);
}
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// get your payment method
$chosen_gateway = WC()->session->chosen_payment_method;
//echo $chosen_gateway;
$fee = 5;
if ( $chosen_gateway == 'cod' ) { //test with cash on delivery method
WC()->cart->add_fee( 'delivery fee', $fee, false, '' );
}
}