Skip to content

Instantly share code, notes, and snippets.

@vanbo
Created September 14, 2020 12:14
Show Gist options
  • Select an option

  • Save vanbo/a3fe1c7c54fe8078d1cfbcd61b307d26 to your computer and use it in GitHub Desktop.

Select an option

Save vanbo/a3fe1c7c54fe8078d1cfbcd61b307d26 to your computer and use it in GitHub Desktop.
WC Borica - Work around Polylang language storage
/**
* IMPORTANT: Past this snippet to your "child-theme\functions.php" file.
*/
add_action( 'init', 'vanbo_store_polylang_language', 99 );
/**
* Saves the Polylang last used language by the current user
*/
function vanbo_store_polylang_language() {
if ( ! function_exists( 'WC' ) || ! WC()->session ) {
return;
}
if ( ! function_exists( 'pll_current_language' ) ) {
return;
}
WC()->session->set( 'vanbo_borica_polylang_language', pll_current_language() );
}
add_action( 'init', 'vanbo_borica_process_response', 90 );
/**
* Processes the WC Borica response
*/
function vanbo_borica_process_response() {
if ( ! isset( $_GET['eBorica'] ) || '' == $_GET['eBorica'] ) {
return;
}
/**
* @var \WcBorica\Gateway $borica
*/
$borica = vanbo_get_gateway( 'borica' );
if ( false == $borica ) {
return;
}
$borica->handle_server_response();
}
add_action( 'wc_borica_payment_response_processed', 'vanbo_borica_redirect_to_return_url', 10, 2 );
/**
* Redirects to the return URL based on the language saved in the WC session
*
* @param $order
* @param string $response_code
*/
function vanbo_borica_redirect_to_return_url( $order, $response_code = '' ) {
if ( ! isset( $_GET['eBorica'] ) || '' == $_GET['eBorica'] ) {
return;
}
if ( ! function_exists( 'pll_current_language' ) ) {
return;
}
$borica = vanbo_get_gateway( 'borica' );
if ( false == $borica ) {
return;
}
$checkout_id = wc_get_page_id( 'checkout' );
$stored_lang = WC()->session->get( 'vanbo_borica_polylang_language', pll_current_language() );
$redirect_id = pll_get_post( $checkout_id, $stored_lang );
$order_received_url = wc_get_endpoint_url( 'order-received', $order->get_id(), get_permalink( $redirect_id ) );
$order_received_url = add_query_arg( 'key', $order->get_order_key(), $order_received_url );
ob_clean();
wp_redirect( $order_received_url );
exit;
}
/**
* Returns the gateway from the loaded WC gateways
*
* @param $name
*
* @return bool|mixed
*/
function vanbo_get_gateway( $name ) {
foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) {
if ( $name == $gateway->id ) {
return $gateway;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment