Use Woocommerce functions in custom php files - php

I am a total beginner in programming with PHP. I wanted to create a PHP file in which the order_status from a predefined order (in my case 108) gets changed to completed.
Therefore I need the woocommerce functions get_order($ID) and update_status but I do not know how to use them in my PHP. I hope you understand my problem. From Java I could imagine that I need to get an instance from a class or something like that?
Here is the code I have so far:
<?php $ord = new WC_Order(108); $ord->update_status('completed'); ?>
When I open the page I receive the following error:
Fatal error: Uncaught Error: Class 'WC_Order' not found (...)

In general on Wordpress/WooCommerce you will include your functions code:
In your active child theme (or active theme) function.php file
In a plugin…
You can also enable some code in:
Your theme templates
WooCommerce templates that you will override through your active child theme (or active theme).
Now to execute that function, you will need an event that will execute your function.
In (Wordpress) Woocommerce there is a lot of action hooks that are triggered on some specific events that you can use to execute your function. In this case your function will be hooked (ready to be executed on a specific event).
If you want to change the status of a specific order is better to do it in the related order edit page in backend.
An example:
For example you can change the order status when a customer has submit his order after checkout on order-received end point (thankyou page):
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) return;
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Change order status to "completed"
$order->update_status( 'completed' );
}
This code is an official code snippet: Automatically Complete Orders.
It is a good example that shows you how things can work… So in your case you are using here WC_Order class methods like update_status().
Now with this code base, you can refine the behaviors like in this answer:
WooCommerce: Auto complete paid Orders (depending on Payment methods)
Related to orders: How to get WooCommerce order details

Related

Woocommerce payment_complete hook alternative [duplicate]

I am implementing an integration using woocommerce. I would like to send the purchase made by the user to other system after the payment of the billet or credit card is confirmed. Does anyone know where I can get this return of the payment and how do I get the purchase's transaction ID?
I tried the code below, but the function doesn't seem to have been called;
add_action( 'woocommerce_payment_complete','send_payed_order_to_omie');
function send_payed_order_to_omie($order_id)
{
/*Código que envia a venda para o ERP*/
}
This is the correct hook for payed orders (excluding "bacs" (bank wire) and "cheque" payments that requires to be manually complete when payment is received).
You can see that in the source code of WC_Order payment_complete() method (used by all payment methods), where woocommerce_payment_complete hook is located, that set the transaction ID (when it's returned by the payment gateway).
To get the transaction ID you can use the WC_Order get_transaction_id() method.
So your code will be:
add_action( 'woocommerce_payment_complete','send_payed_order_to_omie');
function send_payed_order_to_omie( $order_id ) {
$order = wc_get_order( $order_id );
$transaction_id = $order->get_transaction_id();
// Your other code
}
Code goes in functions.php file of the active child theme (or active theme). It should work.

Unhook remove_non_recurring_fees() WooCommerce Subscriptions function

I'm desperately trying to remove an action while the cart calculates the total.
Here is my code:
remove_action('woocommerce_cart_calculate_fees', array('WCS_Cart_Renewal', 'remove_non_recurring_fees'), 1000);
While the original action hook is taking place on the WooCommerce Subscriptions plugin:
// Remove non-recurring fees from renewal carts. Hooked in late (priority 1000), to ensure we handle all fees added by third-parties.
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'remove_non_recurring_fees' ), 1000 );
Unfortunately I could not remove the remove_non_recurring_fees hooked function.
Any idea why?
When you Look at WCS_Cart_Renewal Class and remove_non_recurring_fees() function, you will see that this function removes all fees first and re-add only recurring fees, when a subscription is involved. This function is hooked with a priority of 1000.
Instead of trying to remove the action hook that trigger this function, you have 2 other choices:
1). For custom fees added by you via your theme's functions.php file:
You will have just to use a greater priority like in this following example:
add_action( 'woocommerce_cart_calculate_fees', 'my_custom_fee', 2000 );
function my_custom_fee( $cart ) {
// Your code
}
2). Or better using woocommerce_subscriptions_is_recurring_fee available filter hook:
This filter hook that allows to re-add all desired fees that are not recurring with this simple code line:
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

woocommerce hook when order is placed through REST API

I need to run some custom PHP code when an order is placed on a WooCommerce store. Currently, I am using woocommerce_order_status_changed hook which is working perfectly for web front.
add_action('woocommerce_order_status_changed', 'order_confirmation',10, 3);
function order_confirmation($order_id,$oldstatus,$newstatus){
//my custom code...
}
But when an order is placed through API, this hook is not called.
Is there any hook that we can use to execute some php code when an order is placed through WooCommerce's Rest Api V2?
i think you are sending the set_paid property to true. It sets the status to processing and reduce stock items. if you need to to perform action when order payment is completed, you can use the woocommerce_payment_complete action hook.
function on_woocommerce_payment_complete($order_id){
}
add_action( 'woocommerce_payment_complete', 'on_woocommerce_payment_complete'
);`
However the above hook only fire when order status was from the following array
on-hold', 'pending', 'failed', 'cancelled
before marking the payment completed.
For other order statues the following hook is fired.
do_action( 'woocommerce_payment_complete_order_status_' . $this->get_status(), $this->get_id() );
For more detail you can check the
public function payment_complete( $transaction_id = '' ) {
define in
woocommerce\includes\class-wc-order.php

WooCommerce hook after order is updated?

Is there a hook I can use when I make a change to someone's order via the admin (such as their address, or a custom meta field)? I read this question but unfortunately woocommerce_process_shop_order_meta is fired before the order is saved, meaning I have no access to the newly updated data. What I need is to be able to use the new data that is saved to the order.
UPDATE: An issue with using save_post_shop_order is that the meta is updated before this is hit, so I can't compare the previously saved meta value, for example:
$metaArray = $_POST['meta'];
foreach($metaArray as $meta => $key) {
$metaArr[$key["key"]] = $key["value"];
}
$meta = get_post_meta($order->ID);
if($meta['coverstart'][0] != $metaArr['coverstart']) {
die("COVER START DATE HAS CHANGED");
}
The die() is never hit, because the script always gets the newly saved value.
Sorry but woocommerce_checkout_update_order_meta is fired after the order is saved… See this extract source code located in WC_Checkout create_order() method:
// Save the order.
$order_id = $order->save(); // <== Order is saved here before
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data ); <== // The hook
return $order_id;
So in woocommerce_checkout_update_order_meta you can get the saved order data:
by retrieving the WC_Order object from the $order_id argument and using all methods on it.
or using get_post_meta() on with the $order_id argument to get the data saved in wp_postmeta database table.
Then you can update the data with update_post_meta() function…
You can even use woocommerce_checkout_create_order before the data is saved…
You will be able to get the data from the $order argument using all available methods for the WC_Order class (CRUD getters methods).
You will be able to alter this data and saving it using the CRUD setters methods…
Some examples in stackOverFlow
If you need to do that after the order process the hooks to be used can be:
woocommerce_new_order (on newly created order event)
woocommerce_thankyou (on order received page)
woocommerce_order_status_changed (on order status changing event)
And may be some others…
To alter the data when order is saved in backend, you will use save_post_shop_order that has 3 arguments: $post_id, $post and $update…

Adding data in a custom database table for paid orders in WooCommerce

When the user buy a product, I want to save a series of data in custom tables in database. This data will be the product id, custom fields I have, and some other data.
My idea is that this should be done when the payment of the product has been made correctly, that is to say, at the time of payment.
I wanted you to give me advice, I have created a way but I don't know if it is the right one or if you would recommend any other way.
I've edited the thankyou page, and I have inserted this code:
$order = new WC_Order ($order->get_id ();
$check_payment = $order->payment_complete ();
if ($check_payment) {
global $wpdb;
wpdb->insert (/* CODE DATABASE*/);
}
As woocommerce Order-received (thankyou) page can be reloaded, it's not really the good way.
The correct hook to be used that you can find inside WC_Order payment_complete() method is woocommerce_payment_complete. So your code should be for most payment gateways:
add_action('woocommerce_payment_complete', 'action_payment_complete', 30, 1 );
function action_payment_complete( $order_id ){
global $wpdb;
// Get an instance of the WC_Order object (if needed)
$order = wc_get_order( $order_id );
// Your database actions code
wpdb->insert (/* CODE DATABASE*/);
}
Code goes in function.php file of the active child theme (or active theme).
For payment methods (CHEQUE HERE) as 'cheque', 'bacs' and 'cod' that needs to be "completed" by shop manager, you will use instead:
add_action( 'woocommerce_order_status_completed', 'action_order_status_completed', 20, 2 );
function action_payment_complete( $order_id, $order ){
// The specific payment methods to be target
$payment_methods = array('bacs','cheque','cod');
// Only for specific payment methods
if( ! in_array( $order->get_payment_method(), $payment_methods ) return;
global $wpdb;
// Your database actions code
wpdb->insert (/* CODE DATABASE*/);
}
So when order status will change to completed for this specific payment methods, this hook will be triggered…
You can also use instead woocommerce_order_status_processing if you target Processing order status or woocommerce_order_status_on-hold if you target On Hold order status
Code goes in function.php file of the active child theme (or active theme).
This should works.

Categories