How to get order->price in woocommerce

Hamed mayahian picture Hamed mayahian · Sep 17, 2015 · Viewed 11.1k times · Source

I'm trying to get 'order price' and other detail.

but the $order->get_order_total return nothing and no error appear.

How to get order meta and detail?

add_action( 'woocommerce_thankyou', function($order_id){

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

  if ( $order->status != 'failed' ) {
    $wpdb->show_errors = TRUE;
    $wpdb->suppress_errors = FALSE;

    $table_name = $wpdb->prefix . "Arvand_Marketing";
    $amount =  $order->get_order_total;
    $user_market = get_post_meta( $order->id, 'extra-field', true );
    $wpdb->insert( $table_name, array( 'email' => '$user_market', 'amount'=>'$amount') );

    if ($wpdb->last_error) {
      die('error=' . var_dump($wpdb->last_query) . ',' . var_dump($wpdb->error));
    }
  }

});

Answer

rnevius picture rnevius · Sep 17, 2015

get_order_total() isn't a thing. You'll need to use the get_formatted_order_total() method or the get_total() method.

// Get the formatted order total
$order->get_formatted_order_total();

// Get the stored order total (makes use of $order->order_total)
$order->get_total();