Guest User

Shipping rate by distance custom snippet

a guest
Sep 27th, 2023
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. // Code for https://codecanyon.net/item/shipping-rate-by-distance-for-woocommerce/21671361
  2. // Charge a base shipping price if distance is below xx KM/mile, and a base price + per KM/mile rate otherwise
  3. add_filter('wpali_distance_shipping_rate', 'wpali_add_base_price_to_rate', 10, 2);
  4. function wpali_add_base_price_to_rate($rate, $obj)
  5. {
  6.     if (!empty($rate['meta_data'])) {
  7.         $meters = $rate['meta_data'];
  8.         $distance_in_km = $meters * 0.001;
  9.         $distance_in_km = round($distance_in_km, 2);
  10.     } else {
  11.         $distance_in_km = null;
  12.     }
  13.  
  14.     if ($distance_in_km !== null) {
  15.         if ($distance_in_km < 20) {
  16.             $rate['cost'] = 70;
  17.         } else {
  18.             $price_per_km = $rate['cost'] / $distance_in_km;
  19.             $cost_per_first_20km = 70;
  20.             $cost_per_remaining_kms = ($distance_in_km - 20) * $price_per_km;
  21.             $rate['cost'] = $cost_per_first_20km + $cost_per_remaining_kms;
  22.         }
  23.     }
  24.  
  25.     return $rate;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment