Search code examples
wordpresswoocommercewoothemes

Woocommerce Filter Hooks


I'm hoping to get a little insite for using filters in Woocommerce. My main question is, what am I looking for in the template files? or what are the variables that can be targeted using filters? If we look at the filters list I see filter name and files. Using this filter

single_product_small_thumbnail_size

Files - product-thumbnail.php and woocommerce-template.php

What am I looking for in those files that can be targeted and changed? Would you give me a simple example? Maybe something like change thumbnail size?

add_filter('filter_name', 'your_function_name');

function your_function_name( $variable ) {
// Your code
return $variable;
}

I understand what each part of the function and filter are, but I'm not sure what code to write for "Your code." What variable am I grabbing from the file? How do I apply the change? I can't completely wrap my mind around this. Any help would be greatly appreciated.

Thanks, ~MK


Solution

  • As you can see in e.g. woocommerce-template.php you can filter the shop_catalog string:

    $small_thumbnail_size  = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
    

    That string is used in the subsequent code to determine the image size to be used:

    $image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size  );
    

    So, if you would like to use another image size, you can filter the string like e.g.:

    add_filter( 'single_product_small_thumbnail_size', 'my_single_product_small_thumbnail_size', 25, 1 );
    function my_single_product_small_thumbnail_size( $size ) {
        $size = 'large';
        return $size;
    }