简体   繁体   English

在 WooCommerce 中下订单后显示成功自定义通知

[英]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.

相关问题 Woocommerce:如何在注销时显示自定义成功通知消息? - Woocommerce : how to display custom success notice message on Logout? 在woocommerce成功或付款失败后更新订单 - Update order after success or failed payment in woocommerce 在帐单地址后显示 WooCommerce 管理订单中的自定义元数据 - Display custom meta data in WooCommerce admin order after billing address WooCommerce -Thankyou 和“我的帐户”查看订单页面上的自定义通知 - WooCommerce - Custom notice on Thankyou and "My account" view order pages 在 WooCommerce 订单和 email 通知上显示自定义交货通知 - Display a custom delivery notice on WooCommerce orders and email notifications 在 Woocommerce 结帐页面中的所有默认通知之前显示自定义通知 - Display a custom notice before all default notices in Woocommerce checkout page 在WooCommerce结帐页面上下订单后出现错误页面 - Error page appears after placing the order on WooCommerce checkout page Woocommerce - 使用 ajax 表格显示通知 - Woocommerce - Display notice with ajax form 结帐时自定义 WooCommerce 消息/通知 - Custom WooCommerce message/notice at checkout 在Woocommerce 3中将产品自定义字段显示为订单商品 - Display product custom fields as order items in Woocommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM