Step 1: First step is to write a php where you can redirect after popup close with status:
public function process_frontend_response() {
if ( isset( $_GET['status'] ) ) {
$status = $_GET['status'];
// LOGIC FOR YOUR STATUS
//After logic, redirect to success
$thanks_link = $this->get_return_url( $order );
wp_redirect( $thanks_link );
//OR, FAILURE PAGE
wp_redirect( $order->get_cancel_endpoint() );
}
Now register this function in your constructor:
add_action( 'process_response', array( $this, 'process_frontend_response' ) );
Step 2: We register an endpoint to call this function:
add_action( 'init', 'register_custom_redirect' );
function register_custom_redirect() {
if ( isset( $_GET['post-popup-redirect'] ) ) {
do_action( 'process_response' );
}
}
Step 3: We need to inject a custom javascript in order page to redirect to our above Url (post-popup-redirect) after popup close.
Here we call our popup and make a redirect to above php function:
public function custom_scripts() {
$this->frontend_redirect_url = add_query_arg( 'post-popup-redirect', 'status', $this->get_return_url() );
// Javascript for listening or hash change (it occurs after popup closes), once hash change detected, it will invoke showpopup and
// on popup close, it will redirect to base url with status
add_action( 'wp_head', function () {
echo '<script>' . PHP_EOL;
echo 'function locationHashChanged() {' . PHP_EOL;
echo ' if (location.hash.includes(\'#open-popup\')) {' . PHP_EOL;
echo ' somescript.openPopup().then(status => {' . PHP_EOL;
echo ' console.log(status);' . PHP_EOL;
echo ' window.location =\'' . $this->frontend_redirect_url . '&status=\'+status' . PHP_EOL;
echo ' })' . PHP_EOL;
echo ' .catch(err => {' . PHP_EOL;
echo ' window.location =\'' . $this->frontend_redirect_url . '&status=failed\'' . PHP_EOL;
echo ' })' . PHP_EOL;
echo ' }' . PHP_EOL;
echo '}' . PHP_EOL;
echo 'window.onhashchange = locationHashChanged;' . PHP_EOL;
echo '</script>' . PHP_EOL;
} );
}
Register this function also in constructor:
add_action( 'wp_enqueue_scripts', array( $this, 'custom_scripts' ) );