【问题标题】:After a successful payment, What hook is triggered in Woocommerce支付成功后,Woocommerce中触发了什么钩子
【发布时间】:2018-07-05 23:29:13
【问题描述】:

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

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

这是我的插件代码:

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();
}

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

我们将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce payment hook-woocommerce


    【解决方案1】:

    您应该尝试使用专门为此而设计的位于WC_Order payment_completed() methodwoocommerce_payment_complete 动作挂钩。付款成功后触发。所以试试:

    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 );
    

    【讨论】:

    • Hello tnx 我试过这个(woocommerce_payment_complete)但没有回答例如,试试这个(woocommerce_new_order)并在购买前发送一条消息,我确保代码正常工作。我可以将它连接到将订单信息发送到电子邮件的操作吗?因为邮件是在付款完成后发送的。
    • 谢谢。支付成功后,会发生变化(进行中)。
    • :-) 是的,“处理”
    • 以上几项都不行.......有朋友告诉我他有3个办法.... 1- 把Hook 2-cron作业放在最后 3- 把那个钩入 wp 钩子。 (add_action('wp', ...) .......你的意见是什么?
    • 您没有在问题中说明它的真正用途。 1)在插件或类中,有构造函数,你使用add_action( 'the_hook', array( $this, 'the_hooked_function' ) )… 2)如果没有构造函数(或类或实例化对象),你使用add_action( 'the_hook', 'the_hooked_function' )… 现在缺少一些东西,比如优先级和数字挂钩函数具有的参数。所以你应该尝试每一种可能性,因为你并不真正知道自己在做什么。
    【解决方案2】:
     add_action('woocommerce_payment_complete','this_is_custom_function');
     function this_is_custom_function($order_id){
            $order = new WC_Order( $order_id );
     }
    

    使用“woocommerce_payment_complete”挂钩

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2019-05-28
      • 2012-04-07
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多