WooCommerce use Secondary PHP File (Recalling Template)

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

} 

The problem is fetching the file right here… (3rd line from the bottom)

Read More
require get_template_directory() . "/custom-groups/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.

What is the correct way to load a custom PHP file in wordpress from a function on a hook?


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

} 

EDIT 2
If I move the custom template into the woocommerce plugin templates folder.

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

} 

Related posts

1 comment

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

    The file should be at wp-content/themes/your-child-theme/custom-groups/grouped-simfree.php

Comments are closed.