admin管理员组

文章数量:1130349

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.

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.

Share Improve this question edited Nov 20, 2018 at 17:32 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 20, 2018 at 16:48 agahiagahi 1013 silver badges12 bronze badges 3
  • Possible duplicate of Custom Post Type Templates from Plugin Folder? – kero Commented 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 Commented 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 Commented Nov 20, 2018 at 17:14
Add a comment  | 

1 Answer 1

Reset to default 1

As per this post (https://stackoverflow/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.

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.

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.

Share Improve this question edited Nov 20, 2018 at 17:32 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 20, 2018 at 16:48 agahiagahi 1013 silver badges12 bronze badges 3
  • Possible duplicate of Custom Post Type Templates from Plugin Folder? – kero Commented 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 Commented 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 Commented Nov 20, 2018 at 17:14
Add a comment  | 

1 Answer 1

Reset to default 1

As per this post (https://stackoverflow/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.

本文标签: Redirect woocommerce singleproduct page