Advertisement
drkskwlkr

Restrict ordering for a given shipping class, for a given country

Aug 22nd, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | Source Code | 0 0
  1. add_action('woocommerce_before_single_product', function() {
  2.     $restricted_class = 'restricted-netherlands';
  3.     $user_country = WC_Geolocation::geolocate_ip()['country'] ?? '';
  4.  
  5.     global $product;
  6.     $shipping_class_id = $product->get_shipping_class_id();
  7.     $shipping_class = get_term_by('id', $shipping_class_id, 'product_shipping_class');
  8.  
  9.     if ($shipping_class && $shipping_class->slug === $restricted_class && $user_country === 'NL') {
  10.         add_filter('woocommerce_get_price_html', '__return_false');
  11.         add_filter('woocommerce_is_purchasable', '__return_false');
  12.  
  13.         add_action('woocommerce_single_product_summary', function() {
  14.             echo '<p class="woocommerce-notice">' . esc_html__('We are sorry, this product is unavailable for order from your country.', 'geoip-filter-product') . '</p>';
  15.         }, 25);
  16.     }
  17. }, 10);
  18.  
  19. add_filter('woocommerce_loop_add_to_cart_link', function($button, $product) {
  20.     $restricted_class = 'restricted-netherlands';
  21.     $user_country = WC_Geolocation::geolocate_ip()['country'] ?? '';
  22.  
  23.     $shipping_class_id = $product->get_shipping_class_id();
  24.     $shipping_class = get_term_by('id', $shipping_class_id, 'product_shipping_class');
  25.  
  26.     if ($shipping_class && $shipping_class->slug === $restricted_class && $user_country === 'NL') {
  27.         return '<p class="woocommerce-notice">' . esc_html__('We are sorry, this product is unavailable for order from your country.', 'geoip-filter-product') . '</p>';
  28.     }
  29.  
  30.     return $button;
  31. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement