Reverse engineering of WooCommerce Storefront filters

Storefront is an ecommerce theme for WooCommerce. It is build by the guys from WooCommerce and on the WooCommerce website it is displayed as the “official” theme for WooCommerce.

Ok, what does that mean for us?

The link between this theme and WooCommerce is stronger than in most of the other themes. As you look in the code of the function storefront_do_shortcode you can see, that this function is only a wrapper for the PHP function call_user_func.

function storefront_do_shortcode( $tag, array $atts = array(), $content = null ) {
    global $shortcode_tags;
    if ( ! isset( $shortcode_tags[ $tag ] ) ) {
        return false;
    }
    return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}

The PHP function call_user_func is used for dynamical function calls on runtime. The first paramter of this function defines the callback. If we extend the function storefront_do_shortcode by an output of $shortcode_tags[ $tag ], we can “look” at that callback parameter.

function storefront_do_shortcode( $tag, array $atts = array(), $content = null ) {
    global $shortcode_tags;

    if ( ! isset( $shortcode_tags[ $tag ] ) ) {
        return false;
    }

    if($tag == 'product_categories' )
    {
        var_dump($shortcode_tags[ $tag ]);

        die();
    }


    return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}

The output is WC_Shortcodes::product_categories.

If you search in the theme folder of the storefront theme for a class with the name WC_Shortcodes, you will find out that there is no class with this name.

But if you search in the WooCommerce plugin folder you will find that class.

The class WC_Shortcodes is the place where WooCommerce defines its shortcodes and the function product_categories creates your shortcode.

To render the categorie data WooCommerce used the content-product_cat.php template. That template defines a bunch of action maybe one them helps you.