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.
Related
I have woocommerce_shipping_package_name hook that makes API call after the customer fill his billing data to get the address and caculate shipping
`add_filter('woocommerce_shipping_package_name', 'customer_address_city_with_shipping', 10, 1);`
// this inside the customer_address_city_with_shipping function
$woocommerce->cart->add_fee('Shipping', $fee, true, '');
the shipping method added but without the fee (for sure I tried custom value)
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
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.
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
I am working on a 'Sponsor An Orphan' project using the Woocommerce Subscriptions plugin. Monthly or yearly subscriptions are available.
When a new subscription is created, I am allocating an orphan to the subscription when payment completes like this..
add_action('woocommerce_subscription_payment_complete', 'allocate_orphans');
It correctly allocated the new orphan for a new subscription but it is also allocating an orphan on every renewal.
I think I am using wrong action hook. Which action hook should I use for new subscription for new orphan (that should not allocate the orphan on next payment)?
Any help will be appreciated.
Just had the same problem myself. I think it's very strange WC haven't made this into its own hook, it would be very easy to add an else statement to the if which creates the "non-initial" payment hook that exists...
Anyway, at least we can use their logic in our action:
function assign_orphan($subscription) {
$last_order = $subscription->get_last_order( 'all', 'any' );
if ( false !== $last_order || wcs_order_contains_renewal( $last_order ) ) {
//get out of here
return;
}
//go ahead and allocate on initial sign up
}
add_action('woocommerce_subscription_payment_complete','assign_orphan',10,1);