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');

?>

1 Answer
1

The functions for filter and action manipulation are available at any point after WordPress loads wp-includes/plugin.php – this happens quite early as much of WordPress itself depends on these functions, so they are always accessible to theme and plugin files alike.

You need to add header information to your file such that WordPress may recognize it as a plugin, then activate the plugin in your installation’s administrative dashboard.

/*
Plugin Name: My Plugin
Description: Disable free shipping for select products
Author:      Keryn Gill
*/

/**
 * 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 );

Alternately, place your plugin file directly in wp-content/mu-plugins to have WordPress interpret it as a must-use plugin – this will remove your plugin from the dashboard list and treat it as though it is always active. This practice is best reserved for your own code, as third-party plugins placed in this directory will not trigger activation/deactivation hooks nor will they receive automatic updates.

Leave a Reply

Your email address will not be published. Required fields are marked *