Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active February 13, 2017 10:55
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 damiencarbery/685de5bea3808939e97e9a803eccc934 to your computer and use it in GitHub Desktop.
Save damiencarbery/685de5bea3808939e97e9a803eccc934 to your computer and use it in GitHub Desktop.
Woocommerce - Promote Free Shipping
<?php
// Code inspired by http://ibenic.com/ultimate-guide-woocommerce-shipping-zones/
function rac_get_free_shipping_amount( $package ) {
$all_zones = WC_Shipping_Zones::get_zones();
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$min_amounts = array();
// Verify that the shipping zone is in $all_zones (zone id 0 isn't!)
if ( array_key_exists( $shipping_zone->get_id(), $all_zones ) ) {
$methods = $all_zones[ $shipping_zone->get_id() ][ 'shipping_methods' ];
foreach ( $methods as $method_id => $method_obj ) {
error_log( "Method: $method_id, $method_obj->id" );
if ( 'free_shipping' == $method_obj->id ) {
$min_amounts[] = $method_obj->min_amount;
}
}
}
// Return false if no free shipping.
$free_shipping_min_amount = ( empty( $min_amounts ) ) ? false : min( $min_amounts );
return $free_shipping_min_amount;
}
add_action( 'woocommerce_load_shipping_methods', 'rac_load_shipping_methods_debug' );
function rac_load_shipping_methods_debug( $package ) {
$free_shipping_min_amount = rac_get_free_shipping_amount( $package );
error_log( "Free Shipping Min Amount: $free_shipping_min_amount" );
}
<?php
// Encourage customers to add items to cart to avail of free shipping.
add_action('woocommerce_cart_totals_before_order_total', 'rac_promote_free_shipping');
add_action('woocommerce_review_order_after_shipping', 'rac_promote_free_shipping');
function rac_promote_free_shipping() {
if (WC()->cart->cart_contents_total) {
$total = WC()->cart->get_displayed_subtotal();
// If cart is under free shipping threshold AND shipping to a country that has free shipping option,
// then tell them how much to add to the cart to get free shipping.
$free_shipping_threshold = 25;
if ($total < $free_shipping_threshold) {
$country = WC()->checkout->get_value('billing_country');
$free_shipping_countries = array('IE', 'GB', 'IM', 'JE'); // Free shipping to Ireland, UK, Isle of Man and Jersey
if (in_array($country, $free_shipping_countries)) {
echo '<tr class="promote_free_shipping"><td colspan="2">To avail of free shipping add <em>'.wc_price($free_shipping_threshold-$total).'</em> to your cart.</td></tr>';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment