2

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

?>
6
  • 1
    That should be fine. Do you see the plugin listed on the admin screen? Did you activate it ok? In general there's no real difference between code in a plugin and code in a theme's functions file, so your assumption is correct and there must be something else stopping your plugin working as it should. Sep 15 '16 at 21:42
  • Yes I see the plugin listed, and it was activated fine. I actually have it doing a lot more than just the above function, but it wasn't working so I commented everything out and just tried to get the function to work as it does in functions.php and it's not. I can't seem to figure it out.
    – KVDD
    Sep 15 '16 at 22:21
  • Can you paste the code? I'm assuming that it may just be a typo or something. I spent ages on similar myself today before realising I'd pasted a brace in the wrong place :-s Sep 15 '16 at 22:23
  • Edited the question to include the full code. The function seems to work when copy/pasted into the functions.php, and does not give me an error in Dreamweaver. But maybe I missed something.
    – KVDD
    Sep 15 '16 at 23:00
  • By all means, your full source looks fine (except that add_action() call on the same line as a closing bracket). Do you have WP_DEBUG enabled?
    – bosco
    Sep 15 '16 at 23:03
3

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.

3
  • I do have header information, and I actually have a -options.php file where the it adds a menu item to the sidebar and the user can enter the ids themselves in WP backend. I didn't want to complicate the question since just adding a simple function in the main .php file isn't working
    – KVDD
    Sep 15 '16 at 22:18
  • Just to clarify, the file you are referring to as the "main" is this my-plugin.php file - and it contains the plugin header information, correct? Do you load the my-plugin-options.php file into the main file using include() or require() or some such?
    – bosco
    Sep 15 '16 at 22:24
  • I've edited my question above to include the full file. Yes I did include the other files. But right now I'm just trying to get the function in the main file to work, and have commented out all other code in other files.
    – KVDD
    Sep 15 '16 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.