How to check payment method on a WooCommerce order by id? - wordpress

I want to make some changes if the chosen payment method is COD. On existing WC_Order i have used
($order->payment_method_title == 'Cash On Delivery' ? ... : ... );
to retrieve the title.
But i would like to check against the id (cod) because the title string gets translated to different languages which doesn't make it a good solution.
Is there a way to retrieve the id on a WC_Order in woocommerce?

The post meta key for the payment method ID is simply _payment_method
So if $order->payment_method doesn't have the magic methods in place to get that automatically, you could retrieve the post meta using traditional WordPress
get_post_meta( $order->id, '_payment_method', true );
Update for WooCommerce 3.0
$order->get_payment_method();

If you want the title of the payment method you can do:
$order = new WC_Order( $order_id );
$payment_title = $order->get_payment_method_title();
This returns the string set in the Woocommerce > Payment methods, ex: Paypal.
Here are some very helpful references and documentation that will help you do anything you want with WooCommerce.
WooCommerce Order Class
WooCommerce Reference Docs
Cheers.

If you need the payment gateway object itself you can use the wc_get_payment_gateway_by_order function.
$payment_gateway = wc_get_payment_gateway_by_order( $order );

Related

Update ACF custom fields for new WooCommerce order

I'm trying to update 2 ACF custom fields every time an order is created.
We have orders coming from our own website and orders coming from an external marketplace integration. My goal is to differentiate the 2 type of orders using custom fields in the order.
The data for the custom fields is provided by an external marketplace integration. The first field has to be filled with the ordernumber (if it it exists) and the other field is just a simple true/false field.
For website orders we're using "Free shipping". For external orders, the provided shipping details are as follows:
Shipping "Verzending voor bol(xxxxx)"
I'm trying to get the shipping method using the woocomerce_new_order action, and using $order->get_shipping_method()
However, when a new order is created, the custom fields won't update.
This is what I've tried so far:
add_action( 'woocommerce_new_order', 'vliegwerk_bolcom_order_filter', 99, 2);
function vliegwerk_bolcom_order_filter( $order_id, $order ) {
$order = wc_get_order( $order_id );
$verzendmethode = $order->get_shipping_method();
$bol_ordernummer = (int) filter_var($verzendmethode, FILTER_SANITIZE_NUMBER_INT);
if ($bol_ordernummer > 0) {
update_field('field_61fa977c8dff9', $bol_ordernummer, $order_id );
update_field('field_61fa95f594b13', 1, $order_id );
}
}
I already tested the woocommerce_new_order trigger, it works as expected.
I've also tried updating the fields using a foreach loop, and triggering the function with a shortcode. This also works as expected.
However, when combining it all together, the fields won't update. Any help would be appreciated!

Get the order ID on the final checkout page when a customer purchased a product in WooCommerce

I'm developing a plugin that needs to get the order ID on the final checkout page when a customer purchased a product. How do I do that?
I tried
global $order
var_dump($order);
And all I see is "DESC". I need to access the current order of the customer who is looking at the page.
I need to get the order id in the frontend and add that ID to a script, also on the front end, so that when a client orders a product and pays, the page will load, show them the "Thank you, here is your order number: xxx". At that moment I need my script to, for example, execute a console.log("The oder ID is:", order_id);
To get the order ID on the final checkout page when a customer purchased a product you can use the woocommerce_thankyou hook, where the callback function already contains the order ID.
// define the woocommerce_thankyou callback
function action_woocommerce_thankyou( $order_id ) {
echo "Thank you, here is your order ID: " . $order_id;
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Customer billing country
$billing_country = $order->get_billing_country();
// etc..
};
// add the action
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

How to get ACF data from WooCommerce customers for order export

On a Wordpress-Shop I use WooCommerce (WC) with Advenced-Custom-Fields (ACF)
and WP-All-Import (WPAI) + WP-All-Export (WPAE).
I added a ACF field CustomerNumber to the WC-Customer (which enhanced the WP-User).
On the WPAI-XML-Import I set the CustomerNumber with a value from a ERP.
So all customers have a unique CustomerNumber.
I now need to export the WC-Orders (to import them in the ERP again).
The Order-XML must include the CustomerNumber from the Customer belongs to the Order.
As I see, the other standard fields from the customer – like name and address – are copied automatically to the order (by WooCommerce itself).
My question is now: How I have to do this for the ACF’s?
Did I have to do this by code on my own? Adding the same AC-fields to the WC-Order and hook into the order checkout and copy the values from the customer to the order?
Or is there some kind of setup which do that and which I did not recognize?
Thx
I did not really found an answer.
My current solution is now as I described it in my question.
I added a new rule for these acf to also make them available on the orders.
Then I added a hook to new created orders, determine the acf from the user and copied the necessary values into the order acf.
function di_woocommerce_new_order( $order_id ) {
$order = new WC_Order( $order_id );
if ( !$order ) return;
$customer_id = $order->get_customer_id();
if ( !$customer_id ) return;
$customer = new WC_Customer($customer_id);
if ( !$customer ) return;
$customer_number = $customer->get_meta('customernumber');
if ( !$customer_number ) return;
// add customer number
$order->add_meta_data('customernumber', $customer_number);
// update order modified timestamp
$order->add_meta_data('order_last_updated', current_time( 'mysql' ));
$order->save_meta_data();
}
add_action( 'woocommerce_checkout_order_processed', 'di_woocommerce_new_order', 10, 1);

How to get Pay Now URL with custom order status in WooCommerce?

I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callback 
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filter 
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();

How can I get the order date, in WooCommerce?

I can see inside class-wc-admin-cpt-shop_order.php there are some functions that are pulling together the order information for display in WooCommerce. However, I don't see anywhere where the date can be used ...
Because WooCommerce uses wp_posts to store the data, can I assume that the post_date field is the correct one to use?
Also, anyone know whether there is a function in WooCommerce to get this, or whether there is a way of getting the date to come out in class-wc-admin-cpt-shop_order.php.
// Get $order object from order ID
$order = wc_get_order( $order_id );
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
Source: https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Additionally
$order->get_date_created();
Is the "order date", which you can change within WooCommerce ("Edit Order")
You can use the WC_Order object, if you have the order ID:
$order = new WC_Order($order_id);
$order_date = $order->order_date;
Order properties should not be accessed directly. Best way is $order->get_date_completed()

Resources