2

Why does Woocommerce use hooks rather than including template parts?

I'm in the process of creating my first Woocommerce theme, and it seems that deactivating the current hooks and then readding them to change the order of a section real overkill. To me, it makes more sense that you just include each of the template parts using get_template_part().

What am I missing / what's the benefit of doing it their way?!

2 Answers 2

2

I'm not familiar with Woocommerce, but in short, get_template_part() only look for template parts in parent and child themes, not in plugins.

If you look at the source code, get_template_part() uses locate_template which have the following source code (which is where the actual template part is searched for)

function locate_template($template_names, $load = false, $require_once = true ) {
    $located = '';
    foreach ( (array) $template_names as $template_name ) {
        if ( !$template_name )
            continue;
        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
            $located = STYLESHEETPATH . '/' . $template_name;
            break;
        } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
            $located = TEMPLATEPATH . '/' . $template_name;
            break;
        }
    }

    if ( $load && '' != $located )
        load_template( $located, $require_once );

    return $located;
}

As you can see, locate_template only look for templates in parent and child themes

That is why you cannot use get_template_part() in a plugin or use it to call template parts into a theme from a plugin.

EDIT from comments from @MarkKaplun

Apart from just the general way in which get_template_part() works in retrieving template parts, Mark has also pointed out a valid point in comments about the use of Woocommerce

I would say it differently - since WC is a plugin it can not have any assumptions about which themes are using it and what parts do they have without limiting its adoption, and WC is meant to work with any theme.

1
  • I would say it differently - since WC is a plugin it can not have any assumptions about which themes are using it and what parts do they have without limiting its adoption, and WC is meant to work with any theme. Nov 29, 2015 at 18:12
0
/**
 * @since 1.2.0
 *
 */
function add_action(...) {
    /* ... */
}

.

/**
 * @since 3.0.0
 *
 */
function get_template_part(...) {
    /* ... */
}

WooCommerce was launched in September 2011. However, it wasn't coded in a day, they must have started a few months earlier.

At the time, WP was just post 3.0 (huge update, btw) and nothing about updating WP, themes or plugins was working as smooth as now.

Besides, add_action() and add_filter() are the foundation behind WP concept. You can be sure they will never become obsolete. It's the way to go when you code something as large as WooCommerce.

As you probably know, WC provides a templating system.

1
  • It does not matter when get_template_part() was introduced, the fact still stand that it only searches themes for template parts, not plugins, which does make a lot of sense in a way. So it is unusable (out-of-the-box at least) in plugins, and even through the filters supplied in get_template_part(), I suspect that it would still be quite a messy route to go down and explore Nov 30, 2015 at 6:04

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.