WooCommerce, Sage9 & ACF (Advanced Custom Fields)

I have an issue with WooCommerce and ACF.
I added the WooCommerce templates the way it’s explained here Latest Sage 9, Blade, and WooCommerce

So there is theme/resources/views/woocommerce.blade.php



Then theme/resources/woocommerce/archive-product.php

<?php echo App\Template('woocommerce');

My issue now is, that I can’t call any data from ACF.
get_field('field') always returns null even though it works everywhere else. Simply not on the WooCommerce templates.

Sage 9.0.0-beta.4

Anyone an idea what the problem could be?

Does it work when you pass an ID to it, i.e. get_field('field', 2)? I’ve occasionally run into issues where templates have trouble finding the correct ID for a page.

Yes, I tried that too. Not working :frowning:

Are you using get_field() or the_field()?

Since the WooCommerce templates aren’t Blade templates, you’ll probably want to use the_field() to echo the value.

I tried both now. Not working.

To explain a bit further, I’m vardumping the acf field in header.blade.php where I load certain data like background images, etc. Everything works on all other pages except the woocommerce ones.

So it looks to me that the ACF data somehow is not passed to the woocommerce.blade.php template?

Could you try echoing (or vardumping) the field from inside the loop? I can’t remember if $post is set yet by WooCommerce when the header is loaded.

I’m trying to get page specific ACF fields. So ‘theoretically’ I’m not in a loop. Does that make sense? :thinking:

Page specific? All ACF fields want to be called from the loop; before then the $post variable isn’t reliably set up.

What URL are you at when you’re trying to call fields? What content are the fields attached to?

If you’re getting null even when you directly pass an object ID, then I feel like one of the following must be true:

  • That key doesn’t exist in the page/post/object you’re targeting, so there is nothing to return.
  • You’re somehow calling get_field() in a context where it cannot be reached (i.e. ACF isn’t loaded yet, or you’re in a namespace). Maybe try calling it as \get_field() to get out of any namespacing.
  • The ACF data is being filtered in some way that makes it null.

I’m on the regular woocommerce shop site, domain.com/shop/ (which calls the product archive)

When woocommerce deactivated, the the fields get populated.
I have a ACF field group which is page specific, I add background colors and images for the header. It works on all other pages.

So it has to be a specific woocommerce/blade/acf problem?

I deactivated all plugins except woocommerce and acf and removed theme specific functionality.

Is it possible that the acf data is not getting through the woocommerce blade templates?

Can you request ACF data stored in WooCommerce pages from non-WooCommerce page templates?

Ah. I see the problem. When WooCommerce “takes over” the /shop/ slug it doesn’t reference the placeholder page at all. As far as WordPress is concerned you aren’t on that “Shop” page, you’re on WooCommerce’s product archive. This is the same as if you changed your “Blog” page in Settings/Reading. The “Page” you choose is not part of the query WordPress runs when requesting that slug.

The solution for the “Blog” page example can be found here:

Using this example I googled “woocommerce get shop page id” and found the following helpful Gist:

So adapting the ACF example above you should be able to do this:

	<h1><?php the_field('heading', get_option( 'woocommerce_shop_page_id' )); ?></h1>

Wow, that was it! So whatever pages WooCommerce takes over, this problem appears.
Now I just need to figure out a nice way to implement that into my file without too many ifelses :slight_smile:

Thanks a lot. Appreciate the help @MWDelaney and @alwaysblank

Controllers: the official Sage place to hide all your hideous logic.