8

I'm Currently in the alpha stages of editing a child theme for a Wordpress theme. I do have programing experience and some experience managing wordpress but no direct experience editing wordpress related code. Right now it currently displays the first three categories alphabetically.

What I'm trying to do: Instead of displaying the first three categories alphabetically, I would like to display 3 random categories with more than x number of products.

I've ran into the following roadblocks/questions stopping me from proceeding.

  1. What mechanism/function controls the selection of these categories? (TheirCode)
  2. Is it theme based or something in the underlying WordPress system?
  3. What other information would be relevant to make this change?
  4. What information would be useful from the Firefox Dev Bar?
  5. How do I find "TheirCode" so I can replace it with "MyCode"?

The real question here is: How do I find "TheirCode" which is responsible for this selection using tools such as firefox Dev Bar and the actual source?

This question is not about WooCommerce (the plugin). I'm looking for a way to find a function in a theme that WooCommerce (the company) designed or in any theme really.

Opensource Theme: WooCommerce Storefront

  • 2
    Search for tag with specific class or id wrapping categories using Firefox Developer Inspector. Then, search for this class or id in theme source files. You should find some kind of function displaying categories. Overwriting function or use function filter to replace categories. – kierzniak Jul 9 '18 at 7:16
11
+100

The real question here is: How do I find "TheirCode" which is responsible for this selection using tools such as firefox dev bar and the actual source?

If you are referring to the HTML output/source, then for example on the official Storefront theme demo site, just right-click on the "Product Categories" heading or section and then you can easily inspect that section. See the MDN doc for other options such as the "Select Element" icon.

enter image description here

Now for the "the actual source" (i.e. the PHP code or function which generates the "Product Categories" section on Pages using the "Homepage" template), you can find it in inc/storefront-template-functions.php.

if ( ! function_exists( 'storefront_product_categories' ) ) {
    /**
     * Display Product Categories
     * Hooked into the `homepage` action in the homepage template
     *
     * @since  1.0.0
     * @param array $args the product section args.
     * @return void
     */
    function storefront_product_categories( $args ) {

        if ( storefront_is_woocommerce_activated() ) {

            $args = apply_filters( 'storefront_product_categories_args', array(
                'limit'             => 3,
                'columns'           => 3,
                'child_categories'  => 0,
                'orderby'           => 'name',
                'title'             => __( 'Shop by Category', 'storefront' ),
            ) );

            ...
        }
    }
}

So storefront_product_categories() is the PHP function that you're looking for and which you can completely override if you want to (see https://docs.woocommerce.com/document/set-up-and-use-a-child-theme/#section-5). But if you just want to display the product categories in random sorting, then you can simply use the storefront_product_categories_args to filter the query arguments (which in your case would be orderby):

add_filter( 'storefront_product_categories_args', function( $args ){
    $args['orderby'] = 'rand';
    return $args;
} );

That filter is called from within the storefront_product_categories() function, and these are the other filter/actions you can use:

  • Filter: storefront_product_categories_shortcode_args

  • Action: storefront_homepage_before_product_categories

  • Action: storefront_homepage_after_product_categories_title

  • Action: storefront_homepage_after_product_categories

See this if you're not sure of the differences between an "action" and a "filter".


UPDATE: How could you find the code?

Browse through the Storefront theme documentation:

I'm looking for a way to find a function in a theme that WooCommerce (the company) designed or in any theme really.

  • First, check (and read) the theme's documentation.

  • If none or you didn't/couldn't find the information you needed, then try what @motivast had suggested — Inspect the elements on the page, find the appropriate HTML code and/or CSS class/id, then search the theme files for that HTML or CSS class/id until you found the right file or PHP code/function.

For example, on the Storefront theme demo site, the HTML of the product categories section is:

<section class="storefront-product-section storefront-product-categories" aria-label="Product Categories">
    ...
</section>

So you could search the theme files using one of these keywords: (I'd start from the most specific or closest match to the generated HTML)

  • <section class="storefront-product-section storefront-product-categories"

  • class="storefront-product-section storefront-product-categories"

  • storefront-product-categories

  • storefront-product-section

Assuming you didn't know about the Storefront/theme documentation, performing the above searches would eventually bring you to the right file or PHP code/function.

If you need further assistance, let me know and I'll update this answer accordingly.

  • The updated portion to the answer, does not work with storefront 2.5.0 Not even the given filter examples on their own documentation site. – klewis Jun 5 at 19:03

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.