1

My question conserns the Woocommerce plugin.

As you know we can create a custom single-page.php within our theme provided it is inside woocommerce subfolder.

What I need is the ability to have the single-product.php file inside my plugin.

I have tried a lot of ways and it works for other pages, for example I can redirect single-product/title.php to another page but I have had no luck redirect single-product.php.

I understand I probably can redirect it through httaccess file but what I need is redirecting the file through a plugin.

It seems that woocommerce specifies the template file after template_include hook.

So far I managed to use woocommerce_locate_template filter to redirect to some files but for single-product.php there seems to be a complete different story.

Any idea how I can have my own single-product.php inside my custom plugin?

UPDATE: I tried the solution given by one of the answer as follow:

add_filter('single_template', 'my_custom_template');
function my_custom_template($single) {
    global $post;
    if ( $post->post_type == 'product' ) {
        echo 'test';// So its here
        return plugin_dir_url( __FILE__ ).'woocommerce/single-product.php';
    }
    return $single;
}

The single-product page still goes to the default woocommerce template pages.

3
  • Possible duplicate of Custom Post Type Templates from Plugin Folder?
    – kero
    Nov 20, 2018 at 17:01
  • /woocommerce/single-page.php and all other files within /woocommerce/ (inside theme folder) work with WooCommerce logic. /single-product.php is default WordPress logic (aka template hierarchy). So the solution from the linked answer should work here as well
    – kero
    Nov 20, 2018 at 17:02
  • 1
    I have tried that answer and edited my question to reflect the result, it didn't work. As you see this is not a duplicate.
    – agahi
    Nov 20, 2018 at 17:14

1 Answer 1

1

As per this post (https://stackoverflow.com/questions/43621049/woocommerce-multiple-single-product-templates-using-single-product-php-to-redire) I would update your code to look like this:

function so_43621049_template_include( $template ) {
  if ( is_singular('product') ) {
    $template = plugin_dir_path( __FILE__ ).'woocommerce/single-product.php';
  } 
  return $template;
}
add_filter( 'template_include', 'so_43621049_template_include', 20 );

I haven't had a chance to test this but I believe it will work.

5
  • template_include hook doesn't work for single-product.php. The post mentioned uses get_stylesheet_directory() not plugin_dir_url. For redirecting to woocommerce folder inside a theme we don't need a filter.
    – agahi
    Nov 20, 2018 at 18:51
  • 2
    @agahi, template_include does work. If it doesn't for you, try lowering the priority (i.e. use a greater number) - add_filter( 'template_include', 'your_function', 20 ). And as for the $template, you can simply set it to the proper file path - $template = 'path/to/your/custom-single-product.php'
    – Sally CJ
    Nov 21, 2018 at 3:27
  • 2
    Thanks Sally CJ. I'll mark Fence04's as answer but should change plugin_dir_url to plugin_dir_path and add a proirity to the filter.
    – agahi
    Nov 21, 2018 at 6:22
  • 1
    Thanks @SallyCJ I was coming on after testing last night to make exactly that change.
    – Fencer04
    Nov 21, 2018 at 14:08
  • You're welcome @agahi and Fencer04 :)
    – Sally CJ
    Nov 21, 2018 at 14:40

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.