Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if order is paid/processing or completed

Im trying to set a value "paid" in my custom table when the payment was successful / the customer gets redirectd to the thankyou page.

Im not sure if its secure enough to insert "paid => 1" just when the buyer reached the thankyou page. Thats why i wanted to check if the order was really paid, completed or processing.

Now i figured some payment methods are slow? When i checkout with Klarna my method works and it inserts paid = 1, but when I choose Paypal my Method will die and return: "WASNT PAID". But when I refresh the thankyou-page like 30 secs later it works. So i came to the conclusion that the order_status isnt set fast enough? Is there a cleaner way than mine?

add_action('woocommerce_thankyou', 'ceb_order_complete', 10, 1);
function ceb_order_complete( $order_id ) {

    if ( ! $order_id )
        return;

    // Getting an instance of the order object
    $order = wc_get_order( $order_id );

    if($order->is_paid() || $order->has_status('processing') || $order->has_status('completed')) {
        global $wpdb;
        $payID = WC()->session->get( 'payID' );
        if(!empty($payID)) {
            if(!$wpdb->update($wpdb->prefix."ceb_registrations", array("paid"=>1), array("payID"=>$payID))) {
                die("ERROR IN PAYMENT COMPLETE");
            }
        }
    } else {
        die("WASNT PAID");
    }
}
like image 245
xDrago Avatar asked Jan 27 '26 09:01

xDrago


1 Answers

"woocommerce_thankyou" hook will run each time you see/refresh thankyou page. The most suitable way is to process data after payment received. For that you can use "woocommerce_payment_complete".

<?php
add_action( 'woocommerce_payment_complete', 'payment_complete_callback' );
function payment_complete_callback( $order_id ) {
    $order = wc_get_order( $order_id );
    $user  = $order->get_user();
    // do what ever you want to do using $order object of the WC_Order class providing many functions
}
like image 121
Nabeel Perwaiz Avatar answered Jan 29 '26 12:01

Nabeel Perwaiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!