最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce - Override the default templates location under the theme directory

programmeradmin0浏览0评论

As per official woocommerce documentation (here), the proper and safe way to override default woocomerce templates via theme is to create a sub-folder under the theme directory with the 'default' name /woocommerce and copy the targeted templates into it and make some changes ..

Example: to override the archive-product.php template, copy:

wp-content/plugins/woocommerce/templates/archive-product.php 

to

wp-content/themes/<your-theme>/woocommerce/archive-product.php

So is there any possible way to override the default woocommerce templates folder : /woocommerce under the theme directory and move it to an other folder let say /<your-theme>/plugins/woocommerce ?

in fact, many plugins out there (woocommerce, bbpress among others) suggest creating sub-folder under the theme directory with the name of the plugin (/woocommerce, /bbpress) to hold the overridden templates and in order to bring some organization to my theme folder it does make sens to me to create sub-folder /plugins under my theme directory where i can hold my own templates so archive-product.php would be located under

/<my-theme>/plugins/woocommerce/archive-product.php

and not

/<my-theme>/woocommerce/archive-product.php

as expected.

i looked at those core functions: wc_get_template_part, wc_get_template, wc_get_template_html and wc_locate_template under /includes/wc-core-functions.php but i can't figure out how to override the default location (applying filters ..)

i appreciate your kind help.

As per official woocommerce documentation (here), the proper and safe way to override default woocomerce templates via theme is to create a sub-folder under the theme directory with the 'default' name /woocommerce and copy the targeted templates into it and make some changes ..

Example: to override the archive-product.php template, copy:

wp-content/plugins/woocommerce/templates/archive-product.php 

to

wp-content/themes/<your-theme>/woocommerce/archive-product.php

So is there any possible way to override the default woocommerce templates folder : /woocommerce under the theme directory and move it to an other folder let say /<your-theme>/plugins/woocommerce ?

in fact, many plugins out there (woocommerce, bbpress among others) suggest creating sub-folder under the theme directory with the name of the plugin (/woocommerce, /bbpress) to hold the overridden templates and in order to bring some organization to my theme folder it does make sens to me to create sub-folder /plugins under my theme directory where i can hold my own templates so archive-product.php would be located under

/<my-theme>/plugins/woocommerce/archive-product.php

and not

/<my-theme>/woocommerce/archive-product.php

as expected.

i looked at those core functions: wc_get_template_part, wc_get_template, wc_get_template_html and wc_locate_template under /includes/wc-core-functions.php but i can't figure out how to override the default location (applying filters ..)

i appreciate your kind help.

Share Improve this question edited May 23, 2019 at 8:52 asked May 22, 2019 at 23:35 user34803user34803
Add a comment  | 

1 Answer 1

Reset to default 2

I believe you can use the woocommerce_template_path hook/filter:

// $path defaults to 'woocommerce/' (in your theme folder)
add_filter( 'woocommerce_template_path', function( $path ){
    $my_path = get_stylesheet_directory() . '/plugins/woocommerce/';
    return file_exists( $my_path ) ? 'plugins/woocommerce/' : $path;
} );

It should work whether you're overriding only specific files or all — i.e. you copy all files and folders from wp-content/plugins/woocommerce/templates to your theme.

发布评论

评论列表(0)

  1. 暂无评论