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
Related
I have a woocommerce website & using PayU payment system. As of now when customer order fails, then the redirection is happening to order-pay endpoint & when order is success, page is redirecting to order-received endpoint. I need customer to redirect to a specific custom url when order fails and for success order, instead of redirecting to order-received endpoint, I would like to show the order summary details & prevent user from redirecting to home page.
I tried the below in functions.php
add_action( 'woocommerce_thankyou', 'test_func');
function test_func( $order_id ) {
$order = wc_get_order( $order_id );
$url1 = 'https://yoursite.com/custom-url-1';
$url2 = 'https://yoursite.com/custom-url-2';
if ( ! $order->has_status( 'failed' ) ) {
wp_safe_redirect( $url1 );
exit;
} else {
wp_safe_redirect( $url2 );
exit;
}
}
But still it is redirecting to the checkout end points mentioned.
I know that it is taking from the woocommerce checkout endpoints mentioned in the Advance Section, but can somebody please help me to find a workaround for this?
Any help would be really appreciated.
Thanks in Advance.
I have prepared a solution for your problem. I have tested your code and it looks like woocommerce has changed something or I don't know and this kind of code is not working anymore. But no worries. I have searched over the internet and found a good solution for you which I have prepared and tested on my staging website and it works great.
You need to use template_redirect and set it for checkout endpoint url (your case - order-received). Then you need to do the rest with the right functionality to make it work as you expect. Put my code bellow into the functions.php file of your theme and your problem should be solved. Do not forget to change endpoint url with the endpoint url of your website (order-received) and then change the url for failed order status (google.com) and other statuses (yahoo.com)
add_action( 'template_redirect', 'wc_thank_you_redirect' );
function wc_thank_you_redirect(){
if( isset( $_GET['key'] ) && is_wc_endpoint_url( 'order-received' ) ) { //change order-received to your endpoint if you will change it in the future
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
if ( ! empty( $order_id ) ) {
$order = wc_get_order( $order_id );
$order_status = $order->get_status();
if ('failed' == $order_status ) { //failed order status
wp_redirect( 'https://google.com' ); //change url here
exit;
} else {
wp_redirect( 'https://yahoo.com' ); //change url here for other statuses redirect
exit;
}
}
}
}
To do this with order-pay you could easily modify my function and change your endpoint url to order-pay and remove else from the function. To display order summary you need to set template for your new URL. Maybe it is better to redirect only failed orders and modify existing woocommerce template for your thank you page.
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
}
I have developed a web page and integrated Cc Avenue gateway for payment and it is working fine.
My problem is, after a successful payment from bank customer gets redirected to thank you page which will have details like order no, date, customer details etc. The URL looks something like: https://mysite/checkout/order-received/785/?key=wc_order_5b909f1966e92
If I manually change key=wc_order_5b909f1966e92 to key=wc_order_5b909f1966e81 it should show error on 'thank you' page like "invalid order". Instead it is showing "Thank you. Your order has been received." without any order details on the page.
Before changing key:
After changing key:
The following function will check order key validity. If the order key doesn't match, it will display a custom error notice (and optionally redirect to shop page if needed):
add_action( 'template_redirect', 'check_thankyou_order_key' );
function check_thankyou_order_key() {
if( is_wc_endpoint_url('order-received') && isset($_GET['key']) ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order = wc_get_order( $order_id );
if( $order->get_order_key() != wc_clean($_GET['key']) ){
// Display a custom error notice
wc_add_notice( __('Oups! The order key is invalid…', 'woocommerce'), 'error');
// Optionally redirect to shop page (uncomment code below)
// wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
// exit();
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
With the optional redirection to shop page:
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
}
I am in the middle of creating an additional plugin which is a custom payment gateway using the Woocommerce plugin and a child theme to style it.
This is working and I see the payment form correctly and items which is great.
The issue I have is with the payment process option.
The checkout page has an i-frame to the payment solution and is supposed to show only when an order is created and an ID is present. And to make sure we have all the persons details etc.
However the process payment take you to the thanks you page instead.
It also dumps the cart session.
even if I redirect the URL back to the cart page by playing around with it, it still kills the cart.
This would be fine in normal circumstances as I'd expect it to go to a payment page, checkout and be done. But I do not want to do this because this site has to mirror one that is currently live and how that works.
The function in my extended class is this.
public function process_payment( $order_id ) {
global $woocommerce;
#$order = wc_create_order();
///
$order = new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __( 'Awaiting Confirmation of Funds', 'woocommerce' ));
// Reduce stock levels
///$order->reduce_order_stock();
// Remove cart
//$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'refresh' => true,
'reload' => false,
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
///return $order_id;
}
As you can see I commented out the bit where to empty the cart and the also the endpoint brings in the thank you template instead of just staying on the same page.
So far I have tried:
Replicating the code on my checkout to the thank you page and that
results in an undefined checkout, because the process removes it by
then
Changing the end-point reference to the same as checkout
Creating another page and putting the same shortcode on it
Nothing works, and I have been through the plugin and can see a few things I could do.
Copy the process_payment function into my extended class and
effectively re-write it
or
Find a filter similar to below that could do what I need
add_action( 'woocommerce_thankyou', function(){
global $woocommerce;
$order = new WC_Order();
if ( $order->status != 'failed' ) {
wp_redirect( home_url() ); exit; // or whatever url you want
}
});
What I need is it to stay on the same page (refresh) the same way it does when it checks the details to refresh the totals.
Create an order with those current details in and return an order number
Will not kill the cart session at that point, so if I was to refresh the browser for arguments sake it would stay live. (I will work out a way to kill the cart when the person navigates away and a unique session has been created at that point).
Its just this bit I have been fighting with for the past couple of days and not sure the best way.
Any help or pointers would be greatly appreciated.
thanks
Andi
Ok figured this one out.
There is a header action that clears the cart.
We needed to add some more statuses to the array so that it ignores it.
1st remove the action and then add your own method.
function st_wc_clear_cart_after_payment( $methods ) {
global $wp, $woocommerce;
if ( ! empty( $wp->query_vars['order-received'] ) ) {
$order_id = absint( $wp->query_vars['order-received'] );
if ( isset( $_GET['key'] ) )
$order_key = $_GET['key'];
else
$order_key = '';
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->order_key == $order_key ) {
WC()->cart->empty_cart();
}
}
}
if ( WC()->session->order_awaiting_payment > 0 ) {
$order = wc_get_order( WC()->session->order_awaiting_payment );
if ( $order->id > 0 ) {
// If the order has not failed, or is not pending, the order must have gone through
if ( ! $order->has_status( array( 'failed', 'pending','pending-st-cleared-funds','on-hold' ) ) ) { ///// <- add your custom status here....
WC()->cart->empty_cart();
}
}
}
}
function override_wc_clear_cart_after_payment() {
remove_filter('get_header', 'wc_clear_cart_after_payment' );
add_action('get_header', 'st_wc_clear_cart_after_payment' );
}
add_action('init', 'override_wc_clear_cart_after_payment');