Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

php - After a successful payment, What hook is triggered in Woocommerce

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...