# WooCommerce, Sage9 & ACF (Advanced Custom Fields)

**URL:** https://discourse.roots.io/t/woocommerce-sage9-acf-advanced-custom-fields/10783
**Category:** sage
**Tags:** woocommerce, sage9, blade
**Created:** 2017-10-31T17:16:06Z
**Posts:** 14

## Post 1 by @vdrnn — 2017-10-31T17:16:07Z

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](https://discourse.roots.io/t/latest-sage-9-blade-and-woocommerce/9462/3?u=vdrnn)

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

```
@extends('layouts.app')

@section('content')
    @php(woocommerce_content())
@endsection
```

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?

---

## Post 2 by @alwaysblank — 2017-10-31T17:23:24Z

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.

---

## Post 3 by @vdrnn — 2017-10-31T17:43:43Z

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

---

## Post 4 by @MWDelaney — 2017-10-31T18:05:57Z

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.

---

## Post 5 by @vdrnn — 2017-10-31T18:11:45Z

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?

---

## Post 6 by @MWDelaney — 2017-10-31T18:13:30Z

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.

---

## Post 7 by @vdrnn — 2017-10-31T18:21:11Z

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

---

## Post 8 by @MWDelaney — 2017-10-31T18:35:58Z

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?

---

## Post 9 by @alwaysblank — 2017-10-31T18:37:17Z

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`.

---

## Post 10 by @vdrnn — 2017-10-31T18:50:55Z

I’m on the regular woocommerce shop site, [domain.com/shop/](http://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?

---

## Post 11 by @alwaysblank — 2017-10-31T18:56:47Z

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

---

## Post 12 by @MWDelaney — 2017-10-31T18:57:56Z

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:

> **[ACF | Value not loading on Posts page](https://www.advancedcustomfields.com/resources/value-loading-posts-page/)**
>
> When viewing an archive page, you may find that ACF is unable to load data from the page as expected. This is because an archive page modifies the WP Query

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

> <https://gist.github.com/Bradley-D/7287723>

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>
```

---

## Post 13 by @vdrnn — 2017-10-31T19:05:54Z

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

---

## Post 14 by @MWDelaney — 2017-10-31T19:07:20Z

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