'Display a success custom notice after Placing an Order in WooCommerce
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |