This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Removing or Modifying TB's WooCommerce Actions or Filters

  • Creator
    Topic
  • #23092
    askwpgirl
    Participant

    Hi Jason,

    I’ve been asked by a couple people how to modify the functions you’ve created that override default WooCommerce actions. In particular, how to:

    1 – Change the href on the product image on the archive/shop page to have the product image link to the lightbox (as it does on the single product page) rather than link to the single product page.

    2 – Remove the product thumbnail cross fade and just have the standard WC thumb functionality.

    Both of these requests are related to the function added via the Theme_Blvd_Compat_WooCommerce class in the framework > compat > woocommerce > class-tb.compat-woocommerce.php file:

    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail' );
    add_action( 'woocommerce_before_shop_loop_item_title', array($this, 'show_product_image'), 20 );

    So, in this class, you have removed the default WC product thumbnail loop and added your own. In each of these cases, the users should unhook your function and then hook back in their own function to the woocommerce_before_shop_loop_item_title hook location.

    There’s a great discussion here http://hardcorewp.com/2012/enabling-action-and-filter-hook-removal-from-class-based-wordpress-plugins/ on how developers can enable users more flexible access to plugin’s (or in this case the parent theme’s) instance from outside the class.

    Anyway, I’ve gotten as far as loading the remove action in a function that loads after_setup_theme, but nada. I almost have it but not quite. Help me! 🙂 I think learning how to work with these parent theme classes will be hugely helpful to everyone.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #23093
    Jason Bobich
    Keymaster

    Hi Angela,

    This class you’re referencing, like many classes in the framework, I’ve developed it with the singleton pattern in mind. This means that there can never be more than one instance of the class.

    What does this mean for you? — It means that there always only one object to reference for modifying the action hooks.

    How to get this one instance of the object? — You can grab the instance of this object by calling the static method get_instance() of the class.

    $obj = ThemeBlvd_Foo::get_instance();

    When passing a callback function into to your WordPress functions (i.e. like add_action, add_filter, etc), you do it as an array referencing that object.

    So, where before you may have done this:

    remove_action( 'foo', 'themeblvd_bar' );

    Now, you’ll do this:

    remove_action( 'foo', array($obj, 'bar') );

    Specifically when dealing with these plugin compat classes, you need to do your modifications after the theme has had a chance to include the classes and establish these objects for the first time, which is done at the after_setup_theme hook.

    So, using the WooCommerce compat class as an example, here’s an example of how you might organize your customizations.

    /**
     * Action and filter mods for WooCommerce
     */
    function my_woocommerce_mods() {
    
    	// Avoid errors, if you ever disable WooCommerce
    	if ( ! class_exists('Theme_Blvd_Compat_WooCommerce') ) {
    		return;
    	}
    
    	// Get the single instance of our object
    	$tbwc = Theme_Blvd_Compat_WooCommerce::get_instance();
    
    	// Remove framework product image, and hook in custom one
    	remove_action( 'woocommerce_before_shop_loop_item_title', array($tbwc, 'show_product_image'), 20 );
    	add_action( 'woocommerce_before_shop_loop_item_title', 'my_show_product_image' );
    
    	// Continue other action and filter mods ...
    
    }
    add_action( 'after_setup_theme', 'my_woocommerce_mods', 11 ); // Using priority 11, after framework
    
    /**
     * Custom callback for woocommerce_before_shop_loop_item_title
     */
    function my_show_product_image() {
    	// Your custom display for product image ...
    }
    #23094
    askwpgirl
    Participant

    So helpful. I was pretty close:

    function remove_thumb_test() {
    
        $instance = Theme_Blvd_Compat_WooCommerce::this();
        remove_action( 'woocommerce_before_shop_loop_item_title', array( $instance, 'show_product_image' ), 20 );
    
    }
    
    add_action( 'after_setup_theme', 'remove_thumb_test');

    However, with this I would get an error: Call to undefined method Theme_Blvd_Compat_WooCommerce::this()

    So what I needed was get_instance() instead of this()

    Wow! I’m kind of proud of myself that I got that close. I also didn’t have the priority 11, so that was also a good tip. Yay! Thanks! Will play with it now.

    #26224
    askwpgirl
    Participant

    You should add these instructions to the dev.themeblvd.com articles. Some helpful FAQs on WooCommerce/TB interaction would be great! 🙂

    #26225
    Jason Bobich
    Keymaster
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.