Mu-plugin - [MarketPress] Allows free shipping if Cart total greater than X amount based on specified Shipping method.
<?php
/**
* Plugin Name: [MarketPress] - Custom Free Shipping based on Cart total
* Plugin URI: https://premium.wpmudev.org/
* Description: Free shipping based on cart total for selected Shipping method
* Author: Vaughan Montgomery @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
$free_ship_minimum = 100; // minimum total for free shipping to be applied
$shipping_method = 'weight_rate'; // the shipping method for this to be applied on.
add_filter( 'mp_cart/cart_meta/shipping_total', 'mp_custom_shipping_price', 88, 2);
/*
* Changes the estimated shipping total text to Free if cart total is above $free_ship_minimum
*/
function mp_custom_shipping_price( $shipping_line, $this ) {
global $mp_cart, $free_ship_minimum, $shipping_method;
$cart_total = $mp_cart->total(false) - $mp_cart->shipping_total(false);
if ($shipping_method == mp_get_setting( 'shipping->method' ) && $cart_total >= $free_ship_minimum) {
$shipping_line = '
<div class="mp_cart_resume_item mp_cart_resume_item-shipping-total">
<span class="mp_cart_resume_item_label">' . __( 'Shipping', 'mp' ) . '</span>
<span class="mp_cart_resume_item_amount">Free</span>
</div><!-- end mp_cart_resume_item-shipping-total -->';
}
return $shipping_line;
}
add_filter( 'mp_cart/total', 'mp_custom_shipping_price_total', 99, 3);
/*
* Deducts shipping total from cart Estimaed total if cart total above $free_ship_minimum
*/
function mp_custom_shipping_price_total( $total, $item_total, $this ) {
global $mp_cart, $free_ship_minimum, $shipping_method;
$cart_total = $total - $mp_cart->shipping_total(false);
if ($shipping_method == mp_get_setting( 'shipping->method' ) && $cart_total >= $free_ship_minimum) {
$total = $total - $mp_cart->shipping_total(false);
}
return $total;
}
add_filter( 'mp_calculate_shipping_weight_rate', 'mp_custom_shipping_total', 100, 10 );
/*
* Sets the shipping cost to 0 if cart total greater than $free_ship_minimum
*/
function mp_custom_shipping_total($custom_total, $total, $cart, $address1, $address2, $city, $state, $zip, $country, $selected_option) {
global $mp_cart, $free_ship_minimum, $shipping_method;
$cart_total = $total - $mp_cart->shipping_total(false);
if ($shipping_method == mp_get_setting( 'shipping->method' ) && $cart_total >= $free_ship_minimum) {
return 0;
}
return $total;
}