Get the order id on WooCommerce "Order received" Thankyou page - php

I will customize the thankyou page from my WoocCommerce shop.
For this I added a blanc thankyou.php into the WooCommerce checkout directory.
I tried this code
function get_order($order_id) {
echo $order_id;
}
add_action('woocommerce_thankyou', 'get_order');
But the variable $order_id is empty.
Is there somebody who knows how I get the order id on the thankyou page?

If Url is like www.example.com/checkout/order-received/1234/?key=wc_order_s5ou6md6nTZDds you can use the following to get the order id:
global $wp;
if ( isset($wp->query_vars['order-received']) ) {
$order_id = absint($wp->query_vars['order-received']); // The order ID
$order = wc_get_order( $order_id ); // The WC_Order object
}

Related

Show product order details in custom thankyou page - Woocomerce

I'm redirecting woocomerce order received page when click on purchase buttom, but i need to show the product order details in my custom thankyou page, do you have any idea how to do this?
this is the code that I use to redirect to my custom thankyou page
add_action( 'woocommerce_thankyou', 'redirectcustom');
function redirectcustom( $order_id ){
$order = wc_get_order( $order_id );
$url = 'my_url_thankyoupage';
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url );
exit;
}
}
you need to pass encrypted order id with url so you will have access to order detail from order id.
this is simple way to complete task otherwise there are many plugins available to change default thank you page

Woocommerce: change order id after payment is completed

I have made a simple webshop with woocommerce, with three payment methods. iDeal and by direct bank transfer and on account. The order ID is created based on the payment method. for example, if payment is made with iDEAL, the order id becomes ID190100; if payment is made on account, the order id becomes RK190100. I get this working with the plugin
"Sequential Order Numbers for WooCommerce" from BeRocket but these are already created before the payment is complete. The order ID must only be finalized once the payment has been made. Now orders that have not yet paid, and may not be paying, will receive a fixed order ID. So is it possible to create a temporary order id and when the order is completed change the order id based on payment method?
Woocommerce by default will use the post ID of the order for the order ID. This is evident when viewing the WC_Order::get_order_number() method. If you want to use a custom order number to display, you'll need to add a filter on woocommerce_order_number to load in a different value.
An example script would be:
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->update_meta_data('_new_order_number', $number );
}
}
add_filter('woocommerce_order_number', function($default_order_number, \WC_Order $order) {
//Load in our meta value. Return it, if it's not empty.
$order_number = $order->get_meta('_new_order_number');
if(!empty($order_number)) {
return $order_number;
}
// use whatever the previous value was, if a plugin modified it already.
return $default_order_number;
},10,2);
Try this. It's very quick example. Hope help.
add_action( 'woocommerce_order_status_completed', 'wc_change_order_id' );
function wc_change_order_id( $order_id ) {
$order = wc_get_order( $order_id );
$method = $order->get_payment_method(); // need check this
if ( $method === 'account' ) {
$number = 'ID' . $order->get_id();
$order->set_id( $number );
$order->save();
}
}

Update order status on form submission

I am trying to create a form that automatically updates order status once form has been submitted. The form is located at the order details page, so I would assume that current page ID is equal to orderID. When I try to do a form submission it's simply just stuck and nothing happens. I am asumming it's a problem with getting the orderID and therefore which order to update status on.
I've found the gform_after_submission hook and linked it to the form that is placed on the order details page (form ID 7). I've been trying to use global $wpdb; but not quite sure if thats the right thing to do.
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $order_id ) {
global $wpdb;
//getting orderID
$order = wc_get_order( $order_id );
//changing order status
$order = array();
$order['ID'] = $order->ID;
$order['post_status'] = 'wc-completed';
//updating order
wp_update_post( $order );
}
I am expecting that once a form has been submitted then the order status of the current order ID (the page the form was submitted from) is going to be updated as orderstatus completed.
Replace your code with follows -
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function update_order_submission( $entry, $form ) {
global $post;
$order_id = get_the_ID(); // getting orderID
$order = wc_get_order( $order_id );
if( $order ) {
//changing order status
$order->update_status( 'completed' );
}
}
I managed to solve it by getting the orderID from the URL by using $_GET["id"] to getting the url parameter. The code below in functions.php solves the task.
add_action( 'gform_after_submission_7', 'update_order_submission', 10, 2 );
function update_order_submission( $entry, $form ) {
global $post;
$order_id = $_GET["id"]; // getting orderID
$order = wc_get_order( $order_id );
if( $order ) {
//changing order status
$order->update_status( 'completed' );
}
}

Add custom functionality on WooCommerce complete button

I've a situation to add some data to the database tables on order status completed button.
I can see the url in class-wc-admin-post-types.php
Can someone help me for any hook? Or how the admin-ajax.php works? I have to add status to some of mine custom database tables.
this code will fire a customer's order is set to completed..
add_action( 'woocommerce_order_status_completed', 'custom_task' );
function custom_task( $order_id ) {
// Only continue if have $order_id
if ( ! $order_id ) {
return;
}
// Get order
$order = wc_get_order( $order_id );
// Do your thing
}

Woocommerce - Call custom function after payment is completed

I am using Woocommerce for some project and i need to send the order id to some remote site when the payment is made. I am not finding the accurate hook to do this. Can anyone help me to find what's the correct hook to perform certain action after order is completed.
Here is what i have tried
add_action( 'woocommerce_thankyou', 'woo_remote_order' );
function woo_remote_order( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
//Some action to make sure its working.
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
Not sure which is the proper hook to perform this action. I am using paypal payment gateway for payment and orders successfully passes through.
looks like you need add accepted_args on last parameters,
Try this :
add_action( 'woocommerce_thankyou', 'your_func', 10, 1 );
function your_func($order_id) {
$order = new WC_Order( $order_id );
/* Do Something with order ID */
}
Maybe try one of the following.
woocommerce_checkout_order_processed
woocommerce_new_order
add_action( 'woocommerce_subscription_payment_complete', 'YourFunction', 1, 2);
function YourFunction ($order_id)
{
$order = new WC_Order( $order_id );
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
The add_action call must be placed at the very beginning of your plugin, if using wordpress, or if a theme, in functions.php.

Categories