Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - get_order() doesn't work and it returns zero

I am creating an online shop with WooCommerce and I'm adding a function which will update the bonus point to my database into absract-wc-payment-gateway.php.

Here is what I am doing:

  1. Firstly, on the checkout page, the users will click the place order button and then the method will get the users bonus points and minus the bonus points with the get-total(), and then update to the database and go to the thank you page.

enter image description here

  1. Then, the thank you page will get the user's bonus points from the database. And I set the bonus points value to 2000. So in this case, the bonus points should be minus by total Points($50.00)

enter image description here

Here is my code. It will be ran when the user clicks the place order button:

global $woocommerce;
$order = new WC_Order($order_id);
$total = $order->get_total();   
$bonusPoint -= (int)$total; //minus total price and calculate the latest bonus point

$updateSql = "UPDATE userdata02 SET bonusPoint ='" .$bonusPoint.  "' WHERE userID = 2147483647";

mysqli_query($link, $updateSql);// update to an int column

if(mysqli_query($link, $updateSql)) {
    echo "Record updated successfully";
} else {
    echo "Error update record: <>" . mysqli_error($link);
}

Call the method when the user clicks place button:

public function get_return_url( $order = null ) {

    if ( $order ) {
        //$message = "wrong answer";
        //echo "<script type='text/javascript'>alert('$message');</script>";
        $return_url = $order->get_checkout_order_received_url();
    } else {
        $return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) );
    }

    if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' ) {
        $return_url = str_replace( 'http:', 'https:', $return_url );
    }

    self::reducePoints();  //Call reducePoints();
    return apply_filters( 'woocommerce_get_return_url', $return_url, $order );
}

The source code: reducePoints() lines 89 from abstract-WC-Payment-Gateway.php

The get_total() doesn't work and it returns zero.

What I am doing wrong?

like image 394
Capslock10 Avatar asked Jul 11 '16 10:07

Capslock10


People also ask

How do I get total sum of orders by user in WooCommerce?

function so_27969258_track_orders_per_customer($order_id){ $order = new WC_Order( $order_id ); $myuser_id = (int)$order->user_id; $user_info = get_userdata($myuser_id); $items = $order->get_items(); foreach ($items as $item) { } return $order_id; } add_action( 'woocommerce_payment_complete', ' ...

Why are my WooCommerce orders failing?

What steps should you take to resolve the error? Start by double-checking the payment gateway settings in WooCommerce. Confirm that your API key is working and that your username and password are correct. If this doesn't fix the issue, try disconnecting from your payment gateway completely, then reconnecting.

How do I find the current order ID in WooCommerce?

The current way of accomplishing this is by using this function: $order->get_id(); That should return the order id without "#".

How do I get the meta order in WooCommerce?

Add order metaInto the popup that appears, key in custom Field Name and its corresponding Meta Key. Note: The order meta key, if configured, can be found in the custom field section in the WooCommerce Order page.


1 Answers

You need to create an object for $order to use it with get_total(). Try this:

global $woocommerce;
$order = new WC_Order($order_id);
$total = $order->get_total(); //Get the total price of the order.
$bonusPoints -= (int)$total; //calculate the new bonusPoints

Update1: This is just solving the internal data error. We need to get the $order_id to get it work…

Note: You can remove global $woocommerce;before $order = new WC_Order($order_id); because is already included in public function reducePoints( ){


Update2 - The good track:

Remove my code:

global $woocommerce;
$order = new WC_Order($order_id); 

Then at line 89 of your code just add $order in:

public function reducePoints( $order ){
    global $woocommerce;

    // ...

Really happy that this works… It was a long search...

like image 142
LoicTheAztec Avatar answered Oct 19 '22 11:10

LoicTheAztec