Home › Forums › Backend Issues (wp-admin) › Trying to add field group to the WooCommerce 'Shop' page (only)
I’m using ACF Pro and WooCommerce, and looking for a way to add a field group specifically to the WooCommerce ‘Shop’ page (which is chosen in the WooCommerce settings). What I’d ideally like is to add that page type to the “Page Type” rules dropdown, similar to how Front Page and Posts Page are available. Does anyone have a method for adding that, or otherwise applying that field group only to the Shop Page (without selecting that specific page – as I want this to be flexible enough to handle if the user changes the page in WooCommerce settings.)
Thanks!
FWIW, I kept at it and built my own solution. I need to do another re-read of the documentation page to make sure I’m using both hooks correctly, but it at least works for now.
// Add WooCommerce 'Shop' location rule value
// https://www.advancedcustomfields.com/resources/custom-location-rules/
add_filter('acf/location/rule_values/page_type', 'dc_acf_location_rules_values_woo_shop');
function dc_acf_location_rules_values_woo_shop($choices){
$choices[ 'woo-shop-page' ] = 'WooCommerce Shop Page';
return $choices;
}
// Add WooCommerce 'Shop' location rule match
add_filter('acf/location/rule_match/page_type', 'dc_acf_location_rules_match_woo_shop', 10, 3);
function dc_acf_location_rules_match_woo_shop($match, $rule, $options){
if(is_admin() && $rule['value'] == 'woo-shop-page'){
$post_id = $options['post_id'];
$woo_shop_id = get_option( 'woocommerce_shop_page_id' );
if($rule['operator'] == "=="){
$match = $post_id == $woo_shop_id;
}elseif($rule['operator'] == "!="){
$match = $post_id != $woo_shop_id;
}
}
return $match;
}
Thanks @dangercode. I wanted the exact same thing. I used the docs and cheated by looking at your version. Here’s mine, a little shorter mostly bc of the closure.
add_filter( 'acf/location/rule_values/page_type', function ( $choices ) {
$choices['woo_shop_page'] = 'WooCommerce Shop Page';
return $choices;
});
add_filter( 'acf/location/rule_match/page_type', function ( $match, $rule, $options ) {
if ( $rule['value'] == 'woo_shop_page' )
{
if ( $rule['operator'] == '==' )
$match = ( $options['post_id'] == wc_get_page_id( 'shop' ) );
if ( $rule['operator'] == '!=' )
$match = ( $options['post_id'] != wc_get_page_id( 'shop' ) );
}
return $match;
}, 10, 3 );
The use of $options['post_id']
produced an undefined error on options pages, so I would add an isset check:
if ( $rule['value'] == 'woo_shop_page' && isset( $options['post_id'] ) )
The topic ‘Trying to add field group to the WooCommerce 'Shop' page (only)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.