支付成功后,Woocommerce中触发What hook

After a successful payment, What hook is triggered in Woocommerce(支付成功后,Woocommerce中触发What hook)
本文介绍了支付成功后,Woocommerce中触发What hook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Woocommerce 中,要向客户发送短信付款信息,我需要在成功付款后激活触发器.

但是我没有找到任何钩子来做

这是我的插件代码:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {add_action( '####Action 在这里使用#######', array( &$this, 'successful_payment_notification_client' ) );}/* WooCommerce 支付成功通知客户端** @param $order_id*/公共函数 success_payment_notification_client ( $order_id ) {//检查移动字段是否为空如果(空($_REQUEST['mobile'])){返回;}$order = new WC_Order( $order_id );$this->sms->to = array( $_REQUEST['mobile'] );$template_vars = 数组('%order_id%' =>$order_id,'%order_number%' =>$order->get_order_number(),'%status%' =>$order->get_status(),'%billing_first_name%' =>$_REQUEST['billing_first_name'],'%billing_last_name%' =>$_REQUEST['billing_last_name'],'%transaction_id%' =>get_post_meta( $order_id,'_payment_method_title', true ),);$message = str_replace( array_keys( $template_vars ), array_values( $template_vars ), $this->options['wc_notify_customer_payment_successful_message'] );$this->sms->msg = $message;$this->sms->SendSMS();}

所需的钩子应该出现在我的代码的第二行.

任何帮助将不胜感激.

解决方案

你应该尝试使用 woocommerce_payment_complete 动作钩子,它是专门为此制作的,位于 woocommerce_payment_completea href="https://docs.woocommerce.com/wc-apidocs/source-class-WC_Order.html#122" rel="nofollow noreferrer">WC_Order payment_completed() 方法.它在成功付款后立即触发.所以试试:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {add_action( 'woocommerce_payment_complete', array( &$this, 'successful_payment_notification_client' ) );}

<块引用>

您还应该尝试将 array( &$this, 替换为 array( $this, .

或者使用woocommerce_payment_complete_order_status_processing钩子:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {add_action('woocommerce_payment_complete_order_status_processing', array( &$this, 'successful_payment_notification_client' ) );}

<块引用>

您还应该尝试将 array( &$this, 替换为 array( $this, .

或使用 woocommerce_order_status_processing 钩子 (但有 2 个参数:$order_id$order):>

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {add_action( 'woocommerce_order_status_processing', array( &$this, 'successful_payment_notification_client' ) );}

<块引用>

您还应该尝试将 array( &$this, 替换为 array( $this, .

代码进入你的插件文件......


<块引用>

如果没有构造函数(比如一个类)或没有实例化对象,你应该这样使用 add() 动作函数:

 add_action( 'the_hook', 'the_hooked_function', $priority, $nb_of_args );

In Woocommerce, to send sms payment information to the customer, I need to activate a trigger after a successful payment.

But I didn't find any hook do it

This is my plugin code:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {
    add_action( '####Action to be used here#######', array( &$this, 'successful_payment_notification_client' ) );
}

/* WooCommerce Successful payment notification client 
 *
 * @param $order_id
 */
public function successful_payment_notification_client ( $order_id ) {
    // Check the mobile field is empty
    if ( empty( $_REQUEST['mobile'] ) ) {
        return;
    }
    $order          = new WC_Order( $order_id );
    $this->sms->to  = array( $_REQUEST['mobile'] );
    $template_vars  = array(
        '%order_id%'           => $order_id,
        '%order_number%'       => $order->get_order_number(),
        '%status%'             => $order->get_status(),
        '%billing_first_name%' => $_REQUEST['billing_first_name'],
        '%billing_last_name%'  => $_REQUEST['billing_last_name'],
        '%transaction_id%'     => get_post_meta( $order_id,'_payment_method_title', true ),
    );
    $message        = str_replace( array_keys( $template_vars ), array_values( $template_vars ), $this->options['wc_notify_customer_payment_successful_message'] );
    $this->sms->msg = $message;
    $this->sms->SendSMS();
}

The desired hook should come in line two of my code.

Any help will be appreciated.

解决方案

You should try to use woocommerce_payment_complete action hook that is just made specifically for that and located in WC_Order payment_completed() method. It's triggered jus after a successful payment. So try:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {
    add_action( 'woocommerce_payment_complete', array( &$this, 'successful_payment_notification_client' ) );
}

You should try also to replace array( &$this, by array( $this, instead.

Or using woocommerce_payment_complete_order_status_processing hook:

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {
    add_action( 'woocommerce_payment_complete_order_status_processing', array( &$this, 'successful_payment_notification_client' ) );
}

You should try also to replace array( &$this, by array( $this, instead.

or using woocommerce_order_status_processing hook (but with 2 arguments: $order_id and $order):

if ( isset( $this->options['wc_notify_customer_payment_successful_enable'] ) ) {
    add_action( 'woocommerce_order_status_processing', array( &$this, 'successful_payment_notification_client' ) );
}

You should try also to replace array( &$this, by array( $this, instead.

Code goes in your plugin file…


If there is no constructor (like for a class) or no instantiated object, you should use add() action function this way:

 add_action( 'the_hook', 'the_hooked_function', $priority, $nb_of_args );

这篇关于支付成功后,Woocommerce中触发What hook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)