Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('init', 'set_country_based_on_parameter');
- function set_country_based_on_parameter() {
- if (isset($_GET['set_country'])) {
- $country = wc_clean($_GET['set_country']); // Sanitize user input
- if ($country == 'BE' || $country == 'NL') {
- WC()->customer->set_shipping_country($country);
- WC()->customer->set_billing_country($country);
- if($country == 'BE'){
- $country_name = 'Belgium';
- }else{
- $country_name = 'Netherlands';
- }
- wc_add_notice('Your shipping country has been set to '.$country_name.'. You can change it in your account settings.', 'success');
- }
- }
- }
- add_action( 'woocommerce_before_cart', 'show_shipping_message_based_on_country' );
- function show_shipping_message_based_on_country() {
- $cart_total = round(WC()->cart->get_subtotal(),2);
- $shipping_country = WC()->customer->get_shipping_country();
- if ($shipping_country === 'NL') { // Netherlands
- $free_shipping_limit = 25;
- $switch_country_code = 'BE';
- $switch_country_name = 'België';
- } elseif ($shipping_country === 'BE') { // Belgium
- $free_shipping_limit = 60;
- $switch_country_code = 'NL';
- $switch_country_name = 'Nederland';
- } else { // Default case for other countries (optional)
- $free_shipping_limit = 50;
- $switch_country_code = 'BE';
- $switch_country_name = 'België';
- }
- if ($cart_total < $free_shipping_limit) {
- $remaining = $free_shipping_limit - $cart_total;
- $link = add_query_arg('set_country', $switch_country_code, wc_get_cart_url());
- wc_print_notice( sprintf( 'Nog €%s voor gratis verzending! Ben je in %s? <a href="%s">Klik hier</a> om je land te veranderen.', $remaining, $switch_country_name, $link ), 'notice' );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment