1
votes

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 ?

1

1 Answers

1
votes

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