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.
Related
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'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
I am trying to add an action that will check whether or not a product that is currently only available on back order is being checked out - and in the case that one or more is, I want to display a message before the checkout form.
I've gotten this far:
add_action( 'woocommerce_before_checkout_form', 'wnd_checkout_message', 10 );
function wnd_checkout_message( ) {
echo '<div class="wnd-checkout-message"><h3>The message goes here!</h3></div>';}
But how do I check whether or not a back ordered product is currently being checked out/is in the cart?
Add this code to your functions.php file. It will show a notice if one of the products in your cart is on backorder.
add_action( 'woocommerce_before_checkout_form', 'es_checkout_add_cart_notice' );
function es_checkout_add_cart_notice() {
$message = "You have a backorder product in your cart.";
if ( es_check_cart_has_backorder_product() )
wc_add_notice( $message, 'error' );
}
function es_check_cart_has_backorder_product() {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = wc_get_product( $values['data']->get_id() );
if( $cart_product->is_on_backorder() )
return true;
}
return false;
}
Usually in WooCommerce submitted orders are redirect to /order-received/ once payment is completed.
Is it possible to redirect customer to a custom page for a particular payment method?
For example:
Payment method 1 -> /order-received/
Payment method 2 -> /custom-page/
Payment method 3 -> /order-received/
With a custom function hooked in template_redirect action hook using the conditional function is_wc_endpoint_url() and targeting your desired payment method to redirect customer to a specific page:
add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
// Get the order ID
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Set HERE your Payment Gateway ID
if( $order->get_payment_method() == 'cheque' ){
// Set HERE your custom URL path
wp_redirect( home_url( '/custom-page/' ) );
exit(); // always exit
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
How to get the Payment Gateway ID (WC settings > Checkout tab):
A small correction.
"exit" needs to be within the last condition
add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
// Get the order ID
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Set HERE your Payment Gateway ID
if( $order->get_payment_method() == 'cheque' ){
// Set HERE your custom URL path
wp_redirect( home_url( '/custom-page/' ) );
exit(); // always exit
}
}
}
I have no cart link set in the backend of WooCommerce. Instead I have the cart redirected to the checkout page. This works fine, however now I end up with empty urls. When I add a product to the cart I get the message 'successfully added to the cart, see the cart here'. 'See the cart here' is linked to wc_get_page_permalink( 'cart' ), but this is not set.
Is it possible through a function to set the wc_get_page_permalink( 'cart' ) ONLY when items are in the cart?
I tried something like:
function custom_continue_shopping_redirect_url ( $product_id ) {
$url = "http://www.url.com"; // Add your link here
return $url;
}
add_filter('wc_add_to_cart_message', 'custom_continue_shopping_redirect_url');
But that is obviously replacing the whole add to cart message.
Thanks.
You missed some code. try it this way:
add_filter( 'wc_add_to_cart_message', 'custom_continue_shopping_redirect_url', 10, 2 );
function custom_continue_shopping_redirect_url( $message, $product_id ) {
global $woocommerce;
// $permalink = get_permalink(woocommerce_get_page_id('shop')); // replaced by:
$permalink = get_permalink(woocommerce_get_page_id('cart')); // your link here.
$message = sprintf('%s %s', $permalink, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
return $message;
}
You will have to fine tune your replacement link. You can also change the text…
References:
Hookr: wc_add_to_cart_message
Alternative for the wc_add_to_cart_message hook in Woocommerce for WP
Custom Add To Cart Messages - GitHub