espaƱol do_action apply_filters add_filter php wordpress plugins woocommerce

php - do_action - woocommerce_locate_template filter from plugin en lugar de theme



apply_filters (0)

Encontré este artículo sobre el uso de complementos en lugar de temas para anular una plantilla de WooCommerce: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/

Parece que casi funciona, pero no funciona para ninguna plantilla que esté en la raíz del directorio woocommerce / templates (como single-product.php o content-single-product.php).

Para ilustrar, tengo este filtro:

add_filter(''woocommerce_locate_template'', array($this, ''woocommerce_locate_template''), 10, 3);

llamando a esta función:

public function woocommerce_locate_template($template, $template_name, $template_path) { global $woocommerce; al_log(''Looking for '' . $template . '', '' . $template_name . '', '' . $template_path); $_template = $template; if (! $template_path) $template_path = $woocommerce->template_url; $plugin_path = $this->my_plugin_path() .''/woocommerce/''; //look within passed path within the theme. This is the priority $template = locate_template( array( $template_path . $template_name, $template_name, ) ); //modification: get the template from this plugin, if it exists if (!$template && file_exists($plugin_path . $template_name)) $template = $plugin_path . $template_name; //use default template if (! $template) { $template = $_template; } return $template; }

Resulta en estas líneas de registro:

[davetbo@dev wp-content]$ [05-Feb-2016 17:10:46 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/breadcrumb.php, global/breadcrumb.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/sale-flash.php, single-product/sale-flash.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-image.php, single-product/product-image.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-thumbnails.php, single-product/product-thumbnails.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/title.php, single-product/title.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/rating.php, single-product/rating.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/price.php, single-product/price.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/short-description.php, single-product/short-description.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/meta.php, single-product/meta.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/share.php, single-product/share.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/tabs.php, single-product/tabs/tabs.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/description.php, single-product/tabs/description.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/tabs/additional-information.php, single-product/tabs/additional-information.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/product-attributes.php, single-product/product-attributes.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/up-sells.php, single-product/up-sells.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/single-product/related.php, single-product/related.php, woocommerce/ [05-Feb-2016 17:10:47 UTC] Looking for /var/www/html/wp-content/plugins/woocommerce/templates/global/sidebar.php, global/sidebar.php, woocommerce/

Como puede ver, nunca busca nada en la raíz del directorio woocommerce / templates. ¿Alguna idea de por qué ese filtro no funciona para single-product.php o content-single-product.php?

Actualmente tengo una carpeta woocommerce en la raíz de mi carpeta de plugins y dentro hay un archivo single-product.php con una sentencia de matriz cerca de la parte superior, por lo que debería ver inmediatamente si está funcionando. Puse el mismo archivo single-product.php en una carpeta de woocommerce en mi tema y funciona allí, pero no desde el complemento. ¿Hay un gancho diferente que debería usar? Por favor avise.