Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change loop-start.php and loop-end.php with themes function.php #5197

Closed
Willem-Siebe opened this issue Mar 23, 2014 · 8 comments
Closed

Change loop-start.php and loop-end.php with themes function.php #5197

Willem-Siebe opened this issue Mar 23, 2014 · 8 comments

Comments

@Willem-Siebe
Copy link

Hi,

I have a feature request, that we can change loop-end.php and loop-start.php with our themes fuction.php.

Now it outputs (loop-start.php):

<ul class="products">

When I want to change to a div for example, I have to overwrite the template file, which I prefer not to do so. If I'm overlooking something and it is possible in fuctions.php, please let me know.

Kind regards,

Willem

@mikejolley
Copy link
Member

Looking at the code:

if ( ! function_exists( 'woocommerce_product_loop_start' ) ) {

    /**
     * Output the start of a product loop. By default this is a UL
     *
     * @access public
     * @param bool $echo
     * @return string
     */
    function woocommerce_product_loop_start( $echo = true ) {
        ob_start();
        wc_get_template( 'loop/loop-start.php' );
        if ( $echo )
            echo ob_get_clean();
        else
            return ob_get_clean();
    }
}

This is pluggable.

@Willem-Siebe
Copy link
Author

Hi Mike, I asked a plugin developer about this, but he told me it's not possible because there is no hook/filter. So what do you mean with pluggable?

@LC43
Copy link

LC43 commented May 18, 2014

i know its been 6 weeks, but

wc_get_template( 'loop/loop-start.php' );

this is the line that you should be looking at. open this file and you can change the

<ul class="products">

@Willem-Siebe
Copy link
Author

Hi LC43, no problem, I'm always willing to learn, even 6 weeks later. But I know I can overwrite that template file, but I was thinking maybe there is a way to overwrite it just by using functions.php so I don't have to 'overwrite' template files ;-). But I think the only way it's possible is how you mention it.

@LC43
Copy link

LC43 commented May 18, 2014

Hi @Willem-Siebe thats a great attitude! you should overwrite them if you need to! just overwrite the templates files that you copied to your theme directory.

@greguly
Copy link
Contributor

greguly commented May 19, 2014

Hi,

Function wc_get_template is pluggable itself, look the source code to find the hooks and use them at your functions.php file.

Yet the easier way is to add the 'loop/loop-start.php' template to your theme. (Don't forget to make a child theme if your theme is not custom)

Another approach is to create your own 'woocommerce_product_loop_start' & 'woocommerce_product_loop_end' functions.

Cheers

@Willem-Siebe
Copy link
Author

Hi all, because it isn't a support forum here I asked greguly some more explenation per e-mail, and luckily he was very helpfull and his answers made me learn some new stuff. Because it might be helpfull to other readers as well I asked him if I could share the more in depth solutions here. So here they are.

This is from lines 73-74 of wc-core-functions.php

    // Allow 3rd party plugin filter template file from their plugin
    $template = apply_filters( 'wc_get_template_part', $template, $slug, $name );

To learn about filters, check this codex page: http://codex.wordpress.org/Plugin_API

Basically you will add a filter like this at your functions.php file:

add_filter( 'wc_get_template_part', 'unique_function_name', 10, 3 );
function unique_function_name( $template, $slug, $name )  {
    if ( 'loop/loop-start.php' == $page ) 
        $template = 'path/to/your/loop-start.php;
    if ( 'loop/loop-end.php' == $page ) 
        $template = 'path/to/your/loop-end.php;
}

And then your loop-start.php will have whatever you want, like '

' for instance.
Same for loop-end.php, e.g., '
';

  1. Overwriting template files, the most easy way and explained in the WooThemes docs.

  2. Just create your own functions and WooCommerce will not use theirs.

http://codex.wordpress.org/Pluggable_Functions

function woocommerce_product_loop_start() { echo '<div>'; }

function woocommerce_product_loop_end() { echo '</div>'; }

Kind regards,

Willem

@carasmo
Copy link

carasmo commented Sep 29, 2017

I don't know whether it's Genesis connect or not but when using:
function woocommerce_product_loop_start() { echo '<ul class="something">'; }

It echos in the wrong place and so the woocommerce_no_products_found(); is now part of the loop content. Which puts the <p> as a child of the ul. Not a good plan, I don't like wrong structure.

This works in my child theme to create the new loop start in the correct location.

/** 
 * Add Custom WooCommerce Loop Start
 */
function woocommerce_product_loop_start( $echo = true ) {
	ob_start();
	echo '<ul class="something">'; 
	if ( $echo )
		echo ob_get_clean();
	else
		return ob_get_clean();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants