Last active
October 29, 2019 19:11
-
-
Save BFTrick/d4a21524a8f7b25ec296 to your computer and use it in GitHub Desktop.
Enable Free Shipping on a per product basis in WooCommerce.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
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..
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
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