php调用模板文件,WooCommerce使用辅助PHP文件(调用模板)

我应该如何调用辅助PHP文件?这是我的代码.

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";

}

问题是在这里获取文件…(底部第三行)

require get_template_directory() . "/custom-groups/grouped-simfree.php";

这不起作用,并导致奇怪的行为.我有一个自定义的PHP文件,我想在创建的该选项卡中加载(grouped-simfree.php),但我不知道如何使其运行.

从钩子上的函数在wordpress中加载自定义PHP文件的正确方法是什么?

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插件模板文件夹中.

@Reigel可以正常工作,但是现在我更新woocommerce时会丢失模板,我刚刚意识到这是我几年前所做的,现在我意识到为什么网站崩溃是因为在woocommerce更新期间模板被覆盖了

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()来包含文件,请执行以下操作:

require get_stylesheet_directory() . "/custom-groups/grouped-simfree.php";

该文件应位于wp-content / themes / your-child-theme / custom-groups / grouped-simfree.php

标签:php,wordpress,woocommerce

来源: https://codeday.me/bug/20191013/1906046.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值