Skip to content

Instantly share code, notes, and snippets.

@bomsn
Created January 31, 2024 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bomsn/f73177f5748c1eb23622c5ade1966d07 to your computer and use it in GitHub Desktop.
Save bomsn/f73177f5748c1eb23622c5ade1966d07 to your computer and use it in GitHub Desktop.
<?php
/*
*
* Every snippet can be used by placing it in your theme's functions.php file
* Everything here is made specifically for https://codecanyon.net/item/shipping-rate-by-distance-for-woocommerce/21671361/comments
*
*/
// Use this snippet in case the totals are not being automatically updated on your cart page.
function fix_cart_calculation()
{
if (!defined('WOOCOMMERCE_CART')) {
define('WOOCOMMERCE_CART', true);
}
WC()->cart->calculate_totals();
}
add_action('woocommerce_init', 'fix_cart_calculation', 10, 1);
// Change `free shipping` label
function change_wpali_distance_shipping_rate($data)
{
if (isset($data['label']) && strpos($data['label'], 'Free Shipping') !== false) {
$data['label'] = str_replace('Free Shipping', 'new label', $data['label']);
}
return $data;
}
add_filter('wpali_distance_shipping_rate', 'change_wpali_distance_shipping_rate');
// Set the default country for calculations ( usefull if you have disabled country field )
function wpali_target_single_country($params)
{
$params['region'] = "uk"; // Change this value to your country code https://developers.google.com/maps/documentation/distance-matrix/overview#region
return $params;
}
add_filter('wpali_distance_calculating_params', 'wpali_target_single_country');
// Make distance shipping method untaxable (it's taxable by default)
function make_wpali_distance_shipping_rate_taxable($rate)
{
$rate['taxes'] = false;
return $rate;
}
add_filter('wpali_distance_shipping_rate', 'make_wpali_distance_shipping_rate_taxable');
// Charge per mile/KM ( now available by default in latest version )
function srbd_price_per_mile_field($field)
{
$field['srbd_price_per_mile'] = array(
'title' => __('Price per Mile/Km', 'shippingdistance'),
'type' => 'number',
'default' => 0.50,
'custom_attributes' => array(
'step' => '0.01',
),
);
return $field;
}
add_filter('woocommerce_settings_api_form_fields_shipping_rate_by_distance', 'srbd_price_per_mile_field', 10, 1);
function srbd_add_price_per_mile_to_rate($rate, $obj)
{
if (isset($rate['cost'])) {
$price = $obj->get_option('srbd_price_per_mile', 0.50);
$meters = $rate['meta_data']['value'];
if ('km' === $obj->unit) {
$distance = $meters * 0.001;
} else {
$distance = $meters * 0.000621371;
}
if ($price > 0) {
$rate['cost'] = $distance * (float)$price;
}
}
return $rate;
}
add_filter('wpali_distance_shipping_rate', 'srbd_add_price_per_mile_to_rate', 10, 2);
// 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;
}
// Exclude products from shipping
add_filter('wpali_distance_shipping_rate', 'wpali_exclude_gift_cards', 10, 2);
function wpali_exclude_gift_cards($rate, $obj)
{
$cart_have_gift_card = false;
$non_gift_card_products = 0;
// The IDs of product you want to exclude from shipping
$gift_cards_ids = array(1, 2);
if (!WC()->cart->is_empty()) {
// Loop though cart items
foreach (WC()->cart->get_cart() as $cart_item) {
// Check if the product is a gift card
if (in_array($cart_item['product_id'], $gift_cards_ids)) {
$cart_have_gift_card = true;
} else {
// Increment non-gift card products count
$non_gift_card_products++;
}
}
}
// Apply the condition only if the cart has gift cards and no other products
if ($cart_have_gift_card && $non_gift_card_products === 0) {
$rate['cost'] = 0;
$data['label'] = 'Shipping fee not applicable';
}
return $rate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment