【问题标题】:WooCommerce use Secondary PHP File (Recalling Template)WooCommerce 使用辅助 PHP 文件(调用模板)
【发布时间】:2016-02-15 09:10:39
【问题描述】:

我应该如何调用辅助 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";

} 

问题是在此处获取文件...(从底部开始的第 3 行)

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

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

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


编辑:(这张图片有什么问题?)我实际上在几年前就解决了这个问题,但现在我又回到了同样的问题,但同样的解决方案由于某种原因不起作用。来源(我从 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 插件模板文件夹中。

@Reigel 这行得通,但现在当我更新 woocommerce 时我会丢失模板p>

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();

} 

【问题讨论】:

  • 不工作是什么意思?这个文件在哪里?你的functions.php上有这段代码吗?
  • 是的,这是我的 functions.php 代码,该文件位于我的子主题文件夹中的“/custom-groups/grouped-simfree.php”,它会导致我的产品选项卡没有响应并且我的页面当我单击其中一个但不切换选项卡时滚动 - 这是使用自定义 php 文件的正确方法吗?
  • 你能把grouped-simfree.php的代码包含进来吗...只需将其粘贴到pastebin.com或其他地方并给我们链接...
  • 它只是来自 woocommerce 的 grouped.php 的一个修改版本尝试做的是在组中的所有产品中使用一个选项卡过滤器并仅显示具有属性 A 的产品并通过具有属性 B 的产品进行第二个选项卡过滤器,在这里查看来自 woocommerce 的原始 grouped.php 我刚刚修改了 HTML github.com/woothemes/woocommerce/blob/master/templates/…
  • 我只是想到我可能正在调用 grouped-simfree.php 但错过了 woocommerce_template_single_add_to_cart(); 提供的东西。

标签: php wordpress woocommerce


【解决方案1】:

您需要使用get_stylesheet_directory() 来包含您的文件,如果它是子主题,请执行以下操作:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2021-03-14
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多