WooCommerce 使用辅助 PHP 文件(召回模板) [英] WooCommerce use Secondary PHP File (Recalling Template)
问题描述
我应该如何调用辅助 PHP 文件?这是我的代码.
How am I supposed to call a secondary PHP file? Here is my code.
add_filter( 'woocommerce_product_tabs', 'woo_simfree_product_tab' );
function woo_simfree_product_tab( $tabs ) {
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
$tabs['simfree-plans'] = array( 'title' => __( 'SIM Free', 'woocommerce' ), 'priority' => 20, 'callback' => 'woo_simfree_product_tab_content' );
return $tabs;
} else {
return $tabs;
}
}
}
function woo_simfree_product_tab_content() {
require get_template_directory() . "/custom-groups/grouped-simfree.php";
}
问题是在此处获取文件...(从底部算起第 3 行)
The problem is fetching the file right here... (3rd line from the bottom)
require get_template_directory() . "/custom-groups/grouped-simfree.php";
这不起作用并导致奇怪的行为.我有一个自定义 PHP 文件,我想在我创建的这个选项卡中加载 (grouped-simfree.php),但我不知道如何让它运行.
This does not work and causes strange behaviour. I have a custom PHP file I want to load in this tab I have created (grouped-simfree.php) but I don't know how to make it run.
从钩子上的函数在 wordpress 中加载自定义 PHP 文件的正确方法是什么?
What is the correct way to load a custom PHP file in wordpress from a function on a hook?
(这张图片有什么问题?)我实际上在几年前解决了这个问题,但现在我又回到了同样的问题,但同样的解决方案由于某种原因不起作用.来源(我 2014 年的问题):https://stackoverflow.com/questions/30233440/woocommerce-woocommerce-grouped-add-to-cart-function
(What's wrong with this picture?) I actually solved this problem years ago but now I've come back to the same problem but the same solution is not working for some reason. source (my question from 2014): https://stackoverflow.com/questions/30233440/woocommerce-woocommerce-grouped-add-to-cart-function
function woocommerce_grouped_add_to_cart2() {
global $product;
wc_get_template( get_template_directory() . '/custom-groups/grouped-simfree.php', array(
'grouped_product' => $product,
'grouped_products' => $product->get_children(),
'quantites_required' => false
) );
}
function woo_simfree_product_tab_content() {
woocommerce_grouped_add_to_cart2();
}
<小时>
编辑 2如果我将自定义模板移动到 woocommerce 插件模板文件夹中.
EDIT 2 If I move the custom template into the woocommerce plugin templates folder.
@Reigel 这有效,但现在当我更新 woocommerce 时我会丢失模板我刚刚意识到这是我几年前所做的,现在我意识到为什么我的网站崩溃了,因为模板在 woocommerce 更新期间被覆盖>
@Reigel this works but now im gonna lose the template when ever I update woocommerce I just realised this is what I did a couple years ago and now I realise why my site crashed because the templates were overwritten during a woocommerce update
function woocommerce_grouped_add_to_cart2() {
global $product;
wc_get_template( 'single-product/add-to-cart/grouped-simfree.php', array(
'grouped_product' => $product,
'grouped_products' => $product->get_children(),
'quantites_required' => false
) );
}
function woo_simfree_product_tab_content() {
woocommerce_grouped_add_to_cart2();
}
推荐答案
您需要使用 get_stylesheet_directory()
来包含您的文件,如果它是子主题,请执行以下操作:>
You'll need to use get_stylesheet_directory()
to include your file, if it's a child theme do something like this:
require get_stylesheet_directory() . "/custom-groups/grouped-simfree.php";
该文件应位于 wp-content/themes/your-child-theme/custom-groups/grouped-simfree.php
这篇关于WooCommerce 使用辅助 PHP 文件(召回模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!