How can you include a theme template file from within a plugin (i.e. WooCommerce’s Shop page)?

I have a solution to this, at least in my specific case.


Solution

get_template_part() did work in the end, but ACF needed the ID of the post (the Shop page). When calling ACF fields from outside of the loop you need to specify a post ID.

For all of the other pages on the site where the template is being used, it knew the ID already so I didn’t have to specify. Seemingly, where I was calling it from in archive-product.php was outside of the loop. If someone could clarify that, it may be helpful.

So, in archive-product.php:

<?php get_template_part( 'partials/acf', 'a-template-file' ); ?>

Then in the template file:

$post_id = '';
if ( is_shop() ) {
    $post_id = 20;  // 20 being the ID of my Shop page
} else {
    $post_id = get_the_ID();
}

if ( have_rows('flexible_content_layout', $post_id) ) {

    while ( have_rows('flexible_content_layout', $post_id) ) {

        the_row();

        // etc, etc...