• Resolved gamernamer

    (@gamernamer)


    Hello ??
    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. (yourtheme/customer-reviews-woocommerce/)
    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)
    Or are you adding it here:

    		public function load_custom_comments_template( $template ) {
    			if ( get_post_type() !== 'product' ) {
    				return $template;
    			}
    			$plugin_folder = 'customer-reviews-woocommerce';
    			$check_dirs = array(
    				trailingslashit( get_stylesheet_directory() ) . $plugin_folder,
    				trailingslashit( get_template_directory() ) . $plugin_folder
    			);
    			$template_file_name = 'ivole-single-product-reviews.php';
    			if( 'yes' === $this->ivole_ajax_reviews ) {
    				$template_file_name = 'cr-ajax-product-reviews.php';
    			}
    			foreach ( $check_dirs as $dir ) {
    				if ( file_exists( trailingslashit( $dir ) . $template_file_name ) ) {
    					return trailingslashit( $dir ) . $template_file_name;
    				}
    			}
    			return wc_locate_template( $template_file_name, '', plugin_dir_path ( __FILE__ ) . '/templates/' );
    		}

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

    Best,
    Justus

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

    (@ivole)

    Thanks for the interest in our plugin!

    As you can see in the code snippet you shared, the plugin calls wc_locate_template function. I think that the hook woocommerce_locate_template is called from inside wc_locate_template function. So, you should be able to use it to override the template. Does it make sense?

    Thread Starter gamernamer

    (@gamernamer)

    Thanks for your reply!

    Works great now.

    __
    For other people:

    // 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( 'woocommerce_locate_template', function( $template, $template_name ){
        $file = bb_load_wc_template_file( $template_name );
        return $file ? $file : $template;
    }, 10, 2 );
    
    add_filter( 'wc_get_template', function( $template, $template_name ){
        $file = bb_load_wc_template_file( $template_name );
        return $file ? $file : $template;
    }, 10, 2 );

    Files need to be located in plugins/myplugin/assets/templates/woocommerce/ without any subfolder, just the files.

    Best,
    Justus

    Plugin Author cusrev

    (@ivole)

    Great, I’m happy that it is resolved!

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