I am trying to change the currency of my store depending upon the checkout fields posted. I am using woocommerce_checkout_update_order_review action to determine if I have to relaod the my checkout page or not.
function switch_currency_using_shipping_country($posted_data) {
global $woocommerce;
parse_str( $posted_data, $output );
$is_shipping_different = $_POST['ship_to_different_address'] ? $_POST['ship_to_different_address'] : $output['ship_to_different_address'];
$currency = get_woocommerce_currency();
if( isset( $is_shipping_different ) ) {
$country = $output['shipping_country'];
} else {
$country = $output['billing_country'];
}
if ( $country == 'IN' && $currency != 'INR' ) {
// Did something to change currency. [custom plugin code.]
// I Want to declare that the checkout page be loaded if reached here.
}
}
add_action('woocommerce_checkout_update_order_review', __NAMESPACE__."\\switch_currency_using_shipping_country");
How can I declare that the checkout page be reloaded given that some condition is met ?
According to WooCommerce class-wc-ajax.php between line no. 371 - 394 you can see the following code.
// Get messages if reload checkout is not true.
$reload_checkout = isset( WC()->session->reload_checkout );
if ( ! $reload_checkout ) {
$messages = wc_print_notices( true );
} else {
$messages = '';
}
unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
wp_send_json(
array(
'result' => empty( $messages ) ? 'success' : 'failure',
'messages' => $messages,
'reload' => $reload_checkout,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
'.woocommerce-checkout-payment' => $woocommerce_checkout_payment,
)
),
)
);
According to this code it is clear that $reload_checkout is responsible for deciding if the page reloads or not. And to change the value of $reload_checkout you have to set a session value reload_checkout with either true or false.
So to make checkout reload after update_checkout is completed I will have to set the value of session as true.
...
if ( $country == 'IN' && $currency != 'INR' ) {
$woocommerce->session->set( 'reload_checkout ', 'true' ); // This will make sure that checkout relaods.
}
...
Tested and WORKS.
所有评论(0)