Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created March 19, 2018 15:02
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 wpmudev-sls/1dc22638d878105595a5ea5130f5b219 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/1dc22638d878105595a5ea5130f5b219 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: [MarketPress] - Custom Shipping Rates
Plugin URI: https://premium.wpmudev.org/
Description: Custom shipping weight rates. Requires to use the Weight rate option in MarketPress settings
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
add_filter( 'mp_calculate_shipping_weight_rate', function( $price, $total, $cart, $address1, $address2, $city, $state, $zip, $country, $selected_option ){
$weight_rates_per_country = array(
//Germany
'DE' => array(
'2' => 2, // 2 Kgs == 2$
'5' => 4, // 5 kgs == 4 $ etc
'10' => 8
),
//United Kingdom
'GB' => array(
'2' => 4,
'5' => 6,
'10' => 9
),
//United Kingdom
'FR' => array(
'2' => 1.5,
'5' => 3,
'10' => 7
),
);
$weight = $cart->shipping_weight();
if( isset( $weight_rates_per_country[ $country ] ) && ! empty( $weight_rates_per_country[ $country ] ) ){
foreach( $weight_rates_per_country[ $country ] as $kg => $shipping ){
if( $weight <= $kg ){
return $shipping;
}
}
}
return $price;
}, 20, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment