Guest User

Untitled

a guest
Aug 3rd, 2023
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | Source Code | 0 0
  1. add_action('init', 'set_country_based_on_parameter');
  2.  
  3. function set_country_based_on_parameter() {
  4. if (isset($_GET['set_country'])) {
  5. $country = wc_clean($_GET['set_country']); // Sanitize user input
  6.  
  7. if ($country == 'BE' || $country == 'NL') {
  8. WC()->customer->set_shipping_country($country);
  9. WC()->customer->set_billing_country($country);
  10.  
  11. if($country == 'BE'){
  12. $country_name = 'Belgium';
  13. }else{
  14. $country_name = 'Netherlands';
  15. }
  16.  
  17. wc_add_notice('Your shipping country has been set to '.$country_name.'. You can change it in your account settings.', 'success');
  18. }
  19. }
  20. }
  21. add_action( 'woocommerce_before_cart', 'show_shipping_message_based_on_country' );
  22.  
  23. function show_shipping_message_based_on_country() {
  24. $cart_total = round(WC()->cart->get_subtotal(),2);
  25. $shipping_country = WC()->customer->get_shipping_country();
  26.  
  27. if ($shipping_country === 'NL') { // Netherlands
  28. $free_shipping_limit = 25;
  29. $switch_country_code = 'BE';
  30. $switch_country_name = 'België';
  31. } elseif ($shipping_country === 'BE') { // Belgium
  32. $free_shipping_limit = 60;
  33. $switch_country_code = 'NL';
  34. $switch_country_name = 'Nederland';
  35. } else { // Default case for other countries (optional)
  36. $free_shipping_limit = 50;
  37. $switch_country_code = 'BE';
  38. $switch_country_name = 'België';
  39. }
  40.  
  41. if ($cart_total < $free_shipping_limit) {
  42. $remaining = $free_shipping_limit - $cart_total;
  43. $link = add_query_arg('set_country', $switch_country_code, wc_get_cart_url());
  44. 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' );
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment