0

I have used a hook of woocommerce to complete my order status

add_action( 'woocommerce_thankyou','custom_woocommerce_auto_complete_order' ); 
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
 return; 
} 
$order = wc_get_order( $order_id ); 
$order->update_status( 'completed' ); }

Which is working fine.

But when a user tries to cancel the payment from the payment page it and redirect to the site then still the order is getting completed.

Vel
  • 9,027
  • 6
  • 34
  • 66
  • Can you please explain how user tries to cancel the payment? Are you using some addon? If yes, than there must some Post/Get variable setting. So you need to check it. – WildProgrammers Jun 15 '17 at 05:39
  • Okay, I am using Learnpress plugin and doing payment from woocommerce, but still I dont think the learnpress has anything to do with the payment and order completion. Let me know if I am right – Monit Marolia Jun 15 '17 at 07:19
  • You didn't get me. i am not asking anything from LearnPress plugin. I am just asking that you are cancelling the order from payment page? Do your user redirect to payment screen and than closes the window in order to cancel it? – WildProgrammers Jun 15 '17 at 07:58
  • Or can you see any payment id getting to be inserted after successful payment? – WildProgrammers Jun 15 '17 at 08:00
  • No I am cancelling my order from the Payumoney Payment page, and on cancellation it gets redirected to payment screen with order details. – Monit Marolia Jun 15 '17 at 08:09
  • Payment id appears for a second and I get this url on cancellation : /checkout/order-received/8551?key=wc_order_594241241696d&payu_in_callback=1&payu_in_status=failed – Monit Marolia Jun 15 '17 at 08:11
  • OK. So you can use this URL to achieve your goal... I am adding answer in answer section. – WildProgrammers Jun 15 '17 at 08:12
  • Have you checked? – WildProgrammers Jun 15 '17 at 09:12

1 Answers1

0

Use below code :-

    add_action( 'woocommerce_thankyou','custom_woocommerce_auto_complete_order' ); 
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id || $_GET['payu‌​_in_status'] == 'failed' ) {
 return; 
} 
$order = wc_get_order( $order_id ); 
$order->update_status( 'completed' ); }

Check this and let me know whether it works...

WildProgrammers
  • 358
  • 1
  • 7
  • Hello, I am able to execute the above code properly but I am facing an issue with logout on the same page where the url is this checkout/order-received/8551?key=wc_order_594241241696d&pay‌​u_in_callback=1&payu‌​_in_status=failed When I click on logout it throughs an error to me saying "No transaction data passed" Please help me out – Monit Marolia Jun 19 '17 at 11:09
  • I don't think that the problem is from code. Can you please check once by removing the code and whether the error persists or not? – WildProgrammers Jun 19 '17 at 11:27
  • yes, There is no problem with it but can you help me out where if the user is on checkout page and when he clicks on logout he should be redirected to home page – Monit Marolia Jun 19 '17 at 11:45
  • Yes, I am happy to help. This seem to be error from plugin. Can you please try by removing the item in cart and than check whether error still occurs or not? – WildProgrammers Jun 19 '17 at 12:08
  • Ok. This seems to be plugin issue and I need to check the issue. Than only I can say further.(Ofcourse, as this requires some time so it will be paid.) – WildProgrammers Jun 19 '17 at 13:57