1

In Woocommerce, I'm trying to to get all paid orders by credit card and for that I tried using this code:

// Get Report for orders made by credit card
foreach( $orders as $order ){
    if ( $order->get_payment_method() = 'nmwoo_2co' ) {
        $order_data = $order->get_data(); // The Order data
        $orders_by_credit .= 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>' . $order->get_payment_method(); 
    }
}

But the condition $orders->get_payment_method() = 'nmwoo_2co' doesn't work. it's not valid so how to check if the payment method used in the order is nmwoo_2co ?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
mohamed adel
  • 695
  • 1
  • 15
  • 36

1 Answers1

1

First there is an error in your IF statement that should be instead (with === instead of =):

if ( $order->get_payment_method() === 'nmwoo_2co' ) {

Now in your code there is some other errors in:

$orders_by_credit .= 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>' . $order->get_payment_method(); 

That should be:

$orders_by_credit .= 'Order Number: #' . $order->get_order_number() . '<br>' .'Order Status: '. $order->get_status() . '<br>' . 'Order Creation Date: ' . $order->get_date_created()->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->get_total() . '<br>' . 'Customer Username: ' . $order->get_billing_first_name() . '<br>' . 'Customer E-Mail: '.  $order->get_billing_email() . '<br>' . 'Customer Phone: ' . $order->get_billing_phone() . '<br>' . $order->get_payment_method(); 

How to get the correct payment ID in WooCommerce:

  • Go in backend, on Settings > Payments, when you click on a payment method, you can see in the URL: ?page=wc-settings&tab=checkout&section=paypal, where paypal will change for each payment method to the corresponding payment method ID slug.

  • Or also in checkout page, you can inspect with your browser tools, the payment radio buttons on the value attribute like: value="paypal">

If you set the correct payment ID, your condition will work.

See: How to get WooCommerce order details

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks very much i got the right id but the if statement get me a syntax error `if ( $order->get_payment_method() = 'nmwoo_2co' )` what is wrong with this condition > Can't use return value in write context – mohamed adel Apr 08 '19 at 10:32
  • Thank you very much you are awesome – mohamed adel Apr 08 '19 at 10:47
  • sorry but i added the code but i get `Fatal error: Call to undefined method WC_Order_Refund::get_payment_method()` – mohamed adel Apr 08 '19 at 11:49
  • 1
    @mohamedadel Yes there is no payment methods for refund orders, normal… You need to get only normal orders in your orders query. The problem is on your query (on how you get orders), not in my code. – LoicTheAztec Apr 08 '19 at 11:52
  • 1
    so if i added if ( !$order->get_status() === 'refunded'){ // Do the code here } ? it will work ? – mohamed adel Apr 08 '19 at 12:09
  • 1
    @mohamedadel You should only query paid orders (with status "processing" and "complete") – LoicTheAztec Apr 08 '19 at 12:44