• Resolved gamernamer

    (@gamernamer)


    Hey…
    I’m back again ^^ But this time I do not have any errors or bugs. Just a simple question.
    The last days I’m trying to switch from Elementor to Oxygen as page builder because oxygen delivers much cleaner code then Elementor does. At the moment I’m overriding a template of your plugin in my themes folder (as it’s normal for probably all plugins which does have that feature).

    The Problem is… That Oxygen disables the WordPress themes completely. So I’m not able to edit the plugin templates in my themes folder.

    In WooCommerce there is a filter where you can hook into the “woocommerce_locate_template” and add for example a plugin path where you can locate the plugin templates instead of the standard theme path.
    I’ve searched a bit through your code but sadly I wasn’t able to find the right place, where you add the “check” for your plugin templates (something like the locate_template function from WordPress or anything like that)

    Is there probably any hook where I can add this like with WooCommerce?

    Best,
    Justus

    //BTW. Already rated 5 Stars before months ??

    • This topic was modified 4 years ago by gamernamer.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Payment Plugins

    (@mrclayton)

    Hi @gamernamer,

    The Stripe plugin calls function wc_Stripe_get_template which is a wrapper for wc_get_template. That means you can hook into the woocommerce_locate_template filter.

    Look in woo-stripe-payment/includes/wc-stripe-functions.php line 13.

    https://docs.paymentplugins.com/wc-stripe/api/source-function-wc_stripe_get_template.html#4-15

    Kind Regards,

    Thread Starter gamernamer

    (@gamernamer)

    Hey @mrclayton :),
    Thanks a lot!

    Best,
    Justus

    Thread Starter gamernamer

    (@gamernamer)

    Mhhh @mrclayton
    Sorry for that probably dump question but I can’t get it work. :/

    As you wrote I can see that you hook into wc_get_template with your plugin.
    wc_get_template uses the function ‘woocommerce_locate_template’ so normally as you already said I should be able to add my plugin path for the “check” but anything I’m it doesn’t work. To change the WooCommerce directory works like a sharm.

    I’m using this code for WooCommerce:

    // Helper function to load a WooCommerce template or template part file from the
    // active theme or a plugin folder.
    function bb_load_wc_template_file( $template_name ) {
    // Check theme folder first - e.g. wp-content/themes/bb-theme/woocommerce.
    $file = get_stylesheet_directory() . '/woocommerce/' . $template_name;
    if ( @file_exists( $file ) ) {
    return $file;
    }
    
    // Now check plugin folder - e.g. wp-content/plugins/bb/woocommerce.
    $file = 'wp-content/plugins/bb' . '/templates/woocommerce/' . $template_name;
    if ( @file_exists( $file ) ) {
    return $file;
    }
    }
    add_filter( 'woocommerce_template_loader_files', function( $templates, $template_name ){
    // Capture/cache the $template_name which is a file name like single-product.php
    wp_cache_set( 'bb_wc_main_template', $template_name ); // cache the template name
    return $templates;
    }, 10, 2 );
    
    add_filter( 'template_include', function( $template ){
    if ( $template_name = wp_cache_get( 'bb_wc_main_template' ) ) {
    wp_cache_delete( 'bb_wc_main_template' ); // delete the cache
    if ( $file = bb_load_wc_template_file( $template_name ) ) {
    return $file;
    }
    }
    return $template;
    }, 11 );
    add_filter( 'wc_get_template_part', function( $template, $slug, $name ){
    $file = bb_load_wc_template_file( "{$slug}-{$name}.php" );
    return $file ? $file : $template;
    }, 10, 3 );
    add_filter( 'woocommerce_locate_template', function( $template, $template_name ){
    $file = bb_load_wc_template_file( $template_name );
    return $file ? $file : $template;
    }, 10, 2 );

    I’ve tried to add the folder of your plugin into my_plugin/templates/woocommerce/ because I’m defining that as the folder for the templates of WooCommerce but sadly that doesn’t work…

    • This reply was modified 4 years ago by gamernamer.
    Plugin Author Payment Plugins

    (@mrclayton)

    @gamernamer,

    I think the issue is you’re getting your argument order wrong in your bb_load_wc_template_file function. Also I don’t see where you’re adding your custom function to the wc_get_template filter which is required. Try this:

    
    function bb_load_wc_template_file($template, $template_name){
        // Check theme folder first - e.g. wp-content/themes/bb-theme/woocommerce.
        $file = get_stylesheet_directory() . '/woocommerce/' . $template_name;
        if ( @file_exists( $file ) ) {
            return $file;
        }
    
    // Now check plugin folder - e.g. wp-content/plugins/bb/woocommerce.
        $file = 'wp-content/plugins/bb' . '/templates/woocommerce/' . $template_name;
        if ( @file_exists( $file ) ) {
            return $file;
        }
    }
    
    add_filter('wc_get_template', 'bb_load_wc_template_file', 10, 2);
    Thread Starter gamernamer

    (@gamernamer)

    @mrclayton

    That’s it! I’ve just added the code to woocommerce_locate_template but not to wc_get_template… Haha that was very stupid. I tried all day yesterday and today I managed it after 5 minutes…

    The Important thing is: You need to add the subfolder “cc-forms” for example into the defined WooCommerce Folder. Not just the file or the woo-stripe-payment directory!

    If someone also wants to do that, here is my final code:

    # Helper for Oxygen / Tempalte to Plugin #
    ##################################################### 
    // Helper function to load a WooCommerce template or template part file from the
    // active theme or a plugin folder.
    function bb_load_wc_template_file( $template_name ) {
        // Check theme folder first - e.g. wp-content/themes/bb-theme/woocommerce.
        $file = get_stylesheet_directory() . '/woocommerce/' . $template_name;
        if ( @file_exists( $file ) ) {
            return $file;
        }
    
        // Now check plugin folder - e.g. wp-content/plugins/myplugin/woocommerce.
        $file = 'wp-content/plugins/myplugin' . '/assets/templates/woocommerce/' . $template_name;
        if ( @file_exists( $file ) ) {
            return $file;
        }
    }
    
    add_filter( 'woocommerce_template_loader_files', function( $templates, $template_name ){
        // Capture/cache the $template_name which is a file name like single-product.php
        wp_cache_set( 'bb_wc_main_template', $template_name ); // cache the template name
        return $templates;
    }, 10, 2 );
    
    add_filter( 'template_include', function( $template ){
        if ( $template_name = wp_cache_get( 'bb_wc_main_template' ) ) {
            wp_cache_delete( 'bb_wc_main_template' ); // delete the cache
            if ( $file = bb_load_wc_template_file( $template_name ) ) {
                return $file;
            }
        }
        return $template;
    }, 11 );
    
    add_filter( 'wc_get_template_part', function( $template, $slug, $name ){
        $file = bb_load_wc_template_file( "{$slug}-{$name}.php" );
        return $file ? $file : $template;
    }, 10, 3 );
    
    add_filter( 'wc_get_template', function( $template, $template_name ){
        $file = bb_load_wc_template_file( $template_name );
        return $file ? $file : $template;
    }, 10, 2 );
    

    First it will search in the template folder and after that it will search in /plugins/myplugin/assets/templates/woocommerce .

    Have a great day!
    Best,
    Justus

    • This reply was modified 4 years ago by gamernamer.

    Thanks for sharing the code @gamernamer. I came across this searching for a similar problem I’d spent hours on and your solution worked great for what was needed.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filter to add a “child plugin” to change the Plugins template files?’ is closed to new replies.