PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 2, 2022

[FIXED] Woocommerce hook triggered when the order is received and thanks the user

 February 02, 2022     hook-woocommerce, wordpress     No comments   

Issue

In my plugin I'd like to know how to "intercept" when the user completes an order paying by wire transfer because I need to know the amount of the order to calculate something else and write into another table. I thought that would be:

add_filter('woocommerce_thankyou_order_received_text', 'fn_payment_complete');

would that be the right way or is there a different way? Thanks! Cheers


Solution

woocommerce_thankyou_order_received_text is for returning msg. You can still use it to access order but i would recommend using woocommerce_thankyou instead.

Using woocommerce_thankyou_order_received_text hook

function wired_order_thank_you_msg($msg,$order) {
    //$order is object
    $payment_method = $order->get_payment_method(); //returns payment method bacs,cheque,cod etc
    if($payment_method === 'bacs'):
        //Do stuff
        $status = $order->get_status();
        $order_total = $order->get_formatted_order_total();

        //Return custom msg if you want
        $msg = array();
        $msg[] .= $status;
        $msg[] .= __('Custom msg','text');
        $msg[] .= $order_total;

        return sprintf('%s',implode($msg));
    else:
        return $msg;
    endif;

   
}
add_action('woocommerce_thankyou_order_received_text','wired_order_thank_you_msg',10,2);

Using woocommerce_thankyou hook

function wired_order_thank_you($order_id) {
    //$order is object
    $order = wc_get_order( $order_id );
    $payment_method = $order->get_payment_method(); //returns payment method bacs,cheque,cod etc

    if($payment_method === 'bacs'):
        //Do stuff
        $order_total = $order->get_formatted_order_total();
    endif;
}
add_action('woocommerce_thankyou','wired_order_thank_you',10,1);


Answered By - Martin Mirchev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing