-
-
Save eugenf/90df5500f0116fa1bca117584d615b1b to your computer and use it in GitHub Desktop.
| <?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 | |
| * Author URI: http://speakinginbytes.com/ | |
| * Version: 1.0.1 | |
| * | |
| * 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 has the right class. free shipping enabled. | |
| return true; | |
| } | |
| } | |
| // return default value | |
| return $is_available; | |
| } | |
| /** | |
| * 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; |
Hi.
The plugin works well, but do you know how can I check the free shipping radio button at the same time?
thanks
Thank for your your time putting this together! Unfortunately, I am not able to get this working. I have uploaded the plugin to my plugins folder and activated it. I have created a Shipping Zone of Free Shipping with a Shipping Method of Flat Rate with 0 assigned to the cost of the Free Shipping Class. I also have a shipping class of Free Shipping with the slug of free-shipping. I have assigned this Free Shipping Class to one item, my virtual gift cards, which I'd like to be purchased without shipping. When I add only this item to my cart, it is still applying the flat rate shipping charge to the item ($10) and free shipping. Do you have any ideas what I'm doing wrong? Thanks!
Hi.
Is this already tested in WC 2.6.7? Where should I install the file or the code? Sorry but I´m pretty new on this. thanks