[英]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).我需要在结帐页面检查付款是否成功并显示消息:“您的付款已成功”,然后重定向到感谢页面(由插件 Woo Product Tools 为每个产品定制)。 I've been trying to find hooks on the Woo documentation, but no luck so far.
我一直试图在 Woo 文档中找到钩子,但到目前为止没有运气。 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…同样在您的代码中,
$order_id
未定义...
So the right hook is woocommerce_before_thankyou
using wc_print_notice()
function instead:所以正确的钩子是
woocommerce_before_thankyou
使用wc_print_notice()
函数代替:
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).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
Addition: Display a custom html message instead of a Woocommerce notice添加:显示自定义 html 消息而不是 Woocommerce 通知
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.您可以根据需要在文本消息周围添加自己的 html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.