-2

I use WordPress and WooCommerce in my site to sell some rent service.

After user's successful payment, I need to generate access pass code for the user. For pass code generation I use php script, but I don't understand where I should place it. I've tried to place it in /plugins/woocommerce/templates/checkout/thankyou.php file, it works, but when user refresh this page, scripts starts again and user get new pass code - this is not good.

So, first question is: where and how I should start my PHP-code to generate pass code after successful payment?

Second question is: how I should store this pass code in user's personal cabinet? I have idea to store this pass code in order's meta data, but I can't find out how I should set this data and get this data from DB.

Ruban S
  • 33
  • 1
  • 11
Viktor
  • 11
  • 1
  • You should not hack this into any template files to begin with, but use an appropriate _hook_. https://stackoverflow.com/questions/33816480/woocommerce-payment-complete-hook – misorude Feb 10 '20 at 09:27
  • And where I should place code with this hook? Anyway, I need some file to place it. Can you help me with that? – Viktor Feb 10 '20 at 10:33

1 Answers1

0

You can use the woocommerce_payment_complete hook to execute your function.

function execute_post_payment_functions( $order_id ){
    // Write your code to generate password here
}
add_action( 'woocommerce_payment_complete', 'execute_post_payment_functions' );

You can add the above code in functions.php of active theme/child theme

melvin
  • 2,571
  • 1
  • 14
  • 37
  • You can add this code in your active theme or child theme's functions.php file. Location of the file is theme -> functions.php – melvin Feb 10 '20 at 10:49