I have a code snippet in my functions.php file that works correctly…
/**
* Disable free shipping for select products
*
* @param bool $is_available
*/
function my_free_shipping( $is_available ) {
global $woocommerce;
$ineligible = array( '4616', '14031' );
$cart_items = $woocommerce->cart->get_cart();
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $ineligible ) ) {
return false;
}
}
return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );
However I don’t want my client to be messing with the functions.php file, so I made a plugin instead.
Created a my-plugin folder, and inside that folder a my-plugin.php file. In that PHP file I copy and pasted the above function exactly, and removed it from the functions.php. Once I did that it stopped working.
Since the plugin is in a different folder than the functions.php file, I’m assuing I have to add something to get it to read from the my-plugin folder, but I’m not sure what that is. I’m not even sure the add_filter can be used outside of the functions.php file.
Editing to include Full Plugin File
<?php
/*
Plugin Name: WooCommerce - Disable Free Shipping
Plugin URI: http://www.gfishdesigns.com
Description: Allows the user to disable free shipping on a per product basis by entering in the IDs of specific products.
Author: Karen Gill
Version: 1.0
Author URI: http://www.gfishdesigns.com
Copyright 2016 Karen Gill (email : karen@gfishdesigns.com)
*/
/**
* Disable free shipping for select products
*
* @param bool $is_available
*/
function my_free_shipping( $is_available ) {
global $woocommerce;
$excluded = array( '4616', '14031' );
$cart_items = $woocommerce->cart->get_cart();
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $excluded ) ) {
return false;
}
}
return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );
#---------------------------------------------------
# Load CSS
#---------------------------------------------------
function dfs_load_scripts() {
wp_enqueue_style( 'style-name', plugins_url( '/dfs-plugin/dfs_plugin_style.css' ) );
}add_action( 'wp_enqueue_scripts', 'dfs_load_scripts' );
#---------------------------------------------------
# Load other plugin files and configuration
#---------------------------------------------------
include_once(plugin_dir_path( __FILE__ ) . 'dfs-plugin-shortcode.php');
include_once(plugin_dir_path( __FILE__ ) . 'dfs-plugin-options.php');
?>