ACF field doesn't show on my WooCommerce membership fields

278 views Asked by At

I'm recently working on WordPress. I faced a problem regarding ACF. ACF field was not shown on my WooCommerce membership fields

I set post type equal to membership plan here:

enter image description here

This post_type is not available in ACF post_type options and no custom fields are there in membership plan:

enter image description here

I expect only one thing, that how this above custom field (ACF) can be shown in membership plan editor.

1

There are 1 answers

0
Curious Toad On

I ran into this issue myself today and have figured it out. It's because the WooCommerce Memberships plugin restricts the meta boxes that can be displayed on the edit pages.

I had the same issue in their sister plugin, Teams for WooCommerce Memberships.

In both plugins, they provide a filter to add your meta box to the white list. For WooCommerce Memberships it's:

wc_memberships_allowed_meta_box_ids

If you were wanting to allow a custom meta box called my_meta_box, you would do this:

add_filter( 'wc_memberships_allowed_meta_box_ids', 'my_custom_wc_memberships_allowed_meta_box_ids' );

function my_custom_wc_memberships_allowed_meta_box_ids( $ids ) {

   $ids[] = 'my_meta_box';

   return $ids;

}

But here, rather than a meta box we've created from scratch, we're looking to add a meta box created by Advanced Custom Fields. How do we find the meta box ID for that?

If you go to the edit Field Group page for your desired field group, we can find it.

  1. Open Screen Options.
  2. Tick slug.
  3. In the slug panel, grab the group name. For example, mine is group_649caa05c7671.
  4. Prepend this value with 'acf-' and you have your meta box ID! Mine would be acf-group_649caa05c7671.

So a complete filter function would be:

add_filter( 'wc_memberships_allowed_meta_box_ids', 'my_acf_wc_memberships_allowed_meta_box_ids' );

function my_acf_wc_memberships_allowed_meta_box_ids( $ids ) {

   $ids[] = 'acf-group_649caa05c7671';

   return $ids;

}

I tested saving and retrieving a field on a membership plan and it worked a treat.

Bonus content!

Adding fields via plan details tabs

For those of you who want to add regular custom fields without ACF, you can add extra tabs to the Membership Plan settings panel using the wc_membership_plan_data_tabs filter. Misha Rudrastyh has a great tutorial on this.

Adding to Teams for WooCommerce Memberships

As mentioned above, I initially joined the dots on this question because I'd seen similar on the sister plugin. For those of you who need to add meta boxes on Team edit pages, the filter is very similar:

wc_memberships_for_teams_allowed_meta_box_ids

Thanks to:

Credit for finding an ACF meta box goes to Advanced Ads. Thanks guys!