Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Last active October 29, 2019 19:11
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save BFTrick/d4a21524a8f7b25ec296 to your computer and use it in GitHub Desktop.
Save BFTrick/d4a21524a8f7b25ec296 to your computer and use it in GitHub Desktop.
Enable Free Shipping on a per product basis in WooCommerce.
<?php
/**
* Plugin Name: WooCommerce Enable Free Shipping on a Per Product Basis
* Plugin URI: https://gist.github.com/BFTrick/d4a21524a8f7b25ec296
* Description: Enable free shipping for certain products
* Author: Patrick Rauland & eugenf
* Author URI: http://speakinginbytes.com/
* Version: 1.0.2
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
if ( ! class_exists( 'WC_Enable_Free_Shipping' ) ) :
class WC_Enable_Free_Shipping {
protected static $instance = null;
/**
* Initialize the plugin.
*
* @since 1.0
*/
private function __construct() {
// add our check
add_filter( 'woocommerce_shipping_free_shipping_is_available', array( $this, 'patricks_enable_free_shipping' ), 20 );
}
/**
* Enable free shipping for orders with products that have the free-shipping shipping class slug
*
* @param bool $is_available
* @return bool
* @since 1.0
*/
public function patricks_enable_free_shipping( $is_available ) {
global $woocommerce;
// set the shipping classes that are eligible
$eligible = array( 'free-shipping' );
// get cart contents
$cart_items = $woocommerce->cart->get_cart();
// loop through the items checking to make sure they all have the right class
foreach ( $cart_items as $key => $item ) {
if ( ! in_array( $item['data']->get_shipping_class(), $eligible ) ) {
// this item doesn't have the right class. return default availability
return $is_available;
}
}
// nothing out of the ordinary return true
return true;
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
* @since 1.0
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
add_action( 'init', array( 'WC_Enable_Free_Shipping', 'get_instance' ), 0 );
endif;
@jennsweb
Copy link

jennsweb commented Nov 5, 2015

Great plugin Patrick! Do you know of a method I could use to only enable free shipping is a Woocommerce Smart Offer is accepted?

@tripledm
Copy link

tripledm commented Jan 6, 2016

what do I do with the file? sorry a bit new but exactly what I need. Do I upload the file to the woocommerce directory or it's own plugin directory?

@mcnary
Copy link

mcnary commented Oct 17, 2016

Will code still continue to work with new Shipping Zones/Shipping Methods in Woo 2.6+?

@glascot
Copy link

glascot commented Oct 21, 2016

I am trying to get this to work with Woo 2.6.6 and I have the shipping class slug free-shipping and have free shipping class selected for the product and it does not do anything. Am I doing something wrong here?

@rubylaser
Copy link

I'm on Woo 2.6.8 and seeing the same behavior. I have followed all the directions, but my product does not get free shipping. Any ideas?

@Clairewalters
Copy link

Hello - I'm trying to use this method and not sure where to upload file?
I've added it to woocommerce directory - followed instructions for making class & it does not appear to be working.
Any assistance? thank you!

@rpalzona
Copy link

I uploaded the plugin and it doesnt work. Is there any other solution on this? thanks.

@starlajay
Copy link

Yes, same here it doesn't work for me. I have UPS shipping and Conditional Shipping and Payments plugins installed.

@heymicoo
Copy link

Hey guys! In order for it to work, you need to copy the code (no need to download zip) above and paste it to your child theme's functions.php. You're welcome.

@CinchJim
Copy link

Added code. Doesn't work. Bummer.

@tallcoder
Copy link

tallcoder commented Nov 15, 2017

Yea, my hopes were up for this snippet ... but it didn't work with Woo 3.1.2.

@sborelli
Copy link

THANKS - Works great for me!! Just upload the PHP file to your plugins directory on your server, then activate under Dashboard > Plugins.

**Also, as per the instructions, make sure you set the shipping classes that are eligible to match the shipping class you created. And make sure the eligible products are assigned to this shipping class.

@parepidemos
Copy link

Is this suitable for use with Woo/Shipping Zones? I cannot seem to find the right combination of settings to enable the free shipping on a per product basis. When I add Free Shipping to the zone I live in, I cannot see the proper Free Shipping choice that uses the slug I added (product-specific-free-shipping). Ant ideas?

@Stewart-NorfolkHoney
Copy link

Hi,
This looks just what I need. I have tried to install following the dowload zip file, unzip and install routine but get the following message
"The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature"
I'm afraid I'm not terribly tech savvy so don't know how to fix it, could someone help please.
Thanks
Stewart

@lobodasps
Copy link

I'm a novice and just implemented the code - it worked great! If I were to add a Shipping zone as an additional condition (i.e. only perform the per product "free shipping" function when the Shipping Zone is U. and the product is marked as free-shipping? if anyone has a suggestion I'm all ears..

@movementx
Copy link

I can't get this to work. I have free shipping zone for US orders, and a condition for coupon or minimum spend. I only have 2 products with the free-shipping shipping class. But the free shipping option shows up for all products above the minimum spend. Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment