Woocommerce - Alternative for woocommerce_locate_template

I am developing a plugin based on woocommerce, and as part of it I had to redefine the location of the default template file for woocommerce. I mean, I'm looking for a custom woocommerce template to be loaded from my plugin.

For this, I read about woocommerce_locate_template in woocommerce based on this article , but I noticed that the same function is deprecated according to this link . Now I am wondering what could be an alternative function for this.

My whole intention was to change the default woocommerce template download location to my plugin folder. Any help in resolving this? Thanks in advance.

+4
source share
2 answers

The woocommerce_locate_template function is deprecated in favor of wc_locate_template: you can read the code here .

However, if you are looking for a filter , it is still woocommerce_locate_template and takes three arguments:

  • $ template , which is the result of the wp locate_template core function
  • $ template_name , this is just the file name
  • $ template_path is the woocommerce path for templates

So you can check if $ template_name is what you want to intercept and change the path if true, e.g.

function intercept_wc_template($template, $template_name, $template_path) {
    if ($template_name == 'that_template.php') {
        $template = 'the/path/of/your/plugin/template.php';
    }
    return $template;
}

add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);

, :)

, !

- 1: : P -
- 2: ! -

+9

, , , "variable.php".

$template_name woocommerce, . :

. :

function intercept_wc_template($template, $template_name, $template_path) {
    if ($template_name == 'single-product/add-to-cart/variable.php') {
        $template = 'wp-content/themes/theme-name/woocommerce/single-product/add-to-cart/variable.php';
    }

    return $template;
}

add_filter('woocommerce_locate_template', 'intercept_wc_template', 20, 3);
+2

Source: https://habr.com/ru/post/1606321/


All Articles