Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code for https://codecanyon.net/item/shipping-rate-by-distance-for-woocommerce/21671361
- // Charge a base shipping price if distance is below xx KM/mile, and a base price + per KM/mile rate otherwise
- add_filter('wpali_distance_shipping_rate', 'wpali_add_base_price_to_rate', 10, 2);
- function wpali_add_base_price_to_rate($rate, $obj)
- {
- if (!empty($rate['meta_data'])) {
- $meters = $rate['meta_data'];
- $distance_in_km = $meters * 0.001;
- $distance_in_km = round($distance_in_km, 2);
- } else {
- $distance_in_km = null;
- }
- if ($distance_in_km !== null) {
- if ($distance_in_km < 20) {
- $rate['cost'] = 70;
- } else {
- $price_per_km = $rate['cost'] / $distance_in_km;
- $cost_per_first_20km = 70;
- $cost_per_remaining_kms = ($distance_in_km - 20) * $price_per_km;
- $rate['cost'] = $cost_per_first_20km + $cost_per_remaining_kms;
- }
- }
- return $rate;
- }
Advertisement
Add Comment
Please, Sign In to add comment