1

I'm trying to customise WooCommerce's Shop page (called 'Products' in this instance) to include a template part from my main WordPress theme.

get_template_part() does not work. I believe that's because a plugin can't tell which files will exist in a theme so it's disabled. Alternatively, I wonder if it's because the function is being called from within the plugin directory, so it's looking in the wrong directory.

include() does not work. I get the following errors:

Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in ...\woocommerce\archive-product.php on line 52
Warning: include(.../partials/acf-flexible-layout.php): failed to open stream: no suitable wrapper could be found in ...\woocommerce\archive-product.php on line 52
Warning: include(): Failed opening '.../partials/acf-flexible-layout.php' for inclusion (include_path='.;C:\php\pear') in ...\woocommerce\archive-product.php on line 52

note: the three dots (...) in the file paths are a reference to my theme.

Something else to note is that the template part I'm trying to include reference Advanced Custom Fields associated with the Products / Shop page in WordPress.

copying the template part's code directly into the archive-product.php does not work. I'm not sure why this is.

Any help would be much appreciated.


EDIT I have already checked How to include a file using get_template_part() in a plugin? but it doesn't seem suitable:

  1. The answer chosen gives include() as a solution. I've already said this doesn't work for me.
  2. It's not a plugin I'm developing. It's WooCommerce. Any updates to the plugin could delete the files I add / change.
  3. I don't fully understand the other promising answer given, so I'm not able to adapt it to suit my needs (if that's even possible).
5

1 Answer 1

1

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...
1
  • You can use this $post_id = get_option( 'woocommerce_shop_page_id' ); to get the ID of the shop page rather than hard coding it. Commented Mar 16, 2020 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.