Support

Account

Home Forums Backend Issues (wp-admin) Custom Field on Certain WooCommerce Product Category Page

Solving

Custom Field on Certain WooCommerce Product Category Page

  • Hi,

    I’m using Advanced Custom Fields PRO Version 5.3.8.1.

    I’m trying to add a custom field that only appears on a certain WooCommerce Product Category page. Right now I have “Show this field group if” set to:

    Taxonomy Term equals product_cat
    AND
    Post Taxonomy equals My Product Category Name

    For some reason, when I have both parameters set, it does not appear on the Product Category Page. If I remove Post Taxonomy equals My Product Category Name, it shows up.

    Any help or suggestions would be appreciated.

    Thanks!

  • Hi @bnafzinger

    That’s because the “Post Taxonomy equals My Product Category Name” rule works on the post editor page, not the category editor page. If you want it to show on certain term page editor, I’m afraid you need to create a custom location rule. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/custom-location-rules/.

    Hope this helps 🙂

  • Hi James,

    Thanks for pointing me in the right direction. I’m not the best with creating custom function items, but I’ll do some trial and error to see if I can accomplish showing the fields on a certain term page editor.

    Thanks again!

  • Following along with the instructions within the link, I’m able to accomplish all the steps except for “Matching the Rule” to the Category Editor page (Sorry for the sloppy code).

    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    
    function acf_location_rules_types( $choices ) {
    	
        $choices['WooCommerce Category']['woocommerce_category'] = 'WooCommerce Category';
    
        return $choices;
        
    }
    
    add_filter('acf/location/rule_values/woocommerce_category', 'acf_location_rules_values_woocommerce_category');
    
    function acf_location_rules_values_woocommerce_category( $choices ) {
    	
    	$args = array(
    	    'number'     => $number,
    	    'orderby'    => $orderby,
    	    'order'      => $order,
    	    'hide_empty' => $hide_empty,
    	    'include'    => $ids
    	);
    	
    	$product_categories = get_terms( 'product_cat', $args );
    
     
    
        if( $product_categories ) {
    	    
            foreach( $product_categories as $cat ) {
    	        
                $choices[  $cat->name ] =  $cat->name;
                
            }
            
        }
    
        return $choices;
    }
    
    add_filter('acf/location/rule_match/woocommerce_category', 'acf_location_rules_match_woocommerce_category', 10, 3);
    function acf_location_rules_match_woocommerce_category( $match, $rule, $options )
    {
        $args = array(
    	    'number'     => $number,
    	    'orderby'    => $orderby,
    	    'order'      => $order,
    	    'hide_empty' => $hide_empty,
    	    'include'    => $ids
    	);
    	
    	$product_categories = get_terms( 'product_cat', $args );
    	
    	$selected_category = (int) $rule['My Product Category Name'];
    	
    
        if($rule['operator'] == "==")
        {
        	foreach( $product_categories as $cat ) {
    	        
                $match = ( $cat->name == $selected_category );
                
            }
        }
    
        return $match;
    }
  • Hello,

    I think it’s because of this line :

    $selected_category = (int) $rule['My Product Category Name'];

    It’s not an integer and it should be ‘value’ instead of ‘My Product Category Name’ I think, so :

    $selected_category = (string) $rule['value'];

    Works for me.

    Regards,

    Deodat

  • In fact, it needs some additional code to work like a charm :

    add_filter('acf/location/rule_match/woocommerce_category', 'acf_location_rules_match_woocommerce_category', 10, 3);
    function acf_location_rules_match_woocommerce_category( $match, $rule, $options )
    {
    
        global $post;
        $terms = get_the_terms( $post->ID, 'product_cat' );
    
        $selected_category = (string) $rule['value'];
    
        if ( $terms && ! is_wp_error( $terms ) ) {
    
            if ( $rule['operator'] == "==" ) {
    
                foreach ( $terms as $term ) {
    
                    $match = ( $term->name == $selected_category );
    
                }
    
            } elseif ( $rule['operator'] == "!=" ) {
    
                foreach ( $terms as $term ) {
    
                    $match = ( $term->name != $selected_category );
    
                }
            }
        }
    
        return $match;
    }
  • Hey, this looks amazing and very promising.

    Only, on the field group I get the following errors:

    Notice: Undefined variable: number in ..path_to_theme/functions.php on line 256

    Notice: Undefined variable: orderby in ..path_to_theme/functions.php on line 257

    Notice: Undefined variable: order in ..path_to_theme/functions.php on line 258

    Notice: Undefined variable: hide_empty in ..path_to_theme/functions.php on line 259

    Notice: Undefined variable: ids in ..path_to_theme/functions.php on line 260

    And on the term edit page i get this:

    Notice: Trying to get property ‘ID’ of non-object in .path_to_theme/functions.php on line 286 (this line reads: global $post;).

    Do you have time to help me? Would be awesome!

    P.S. The ACF version I have running is 5.8.0 (i can upgrade or downgrade it if needed)

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Custom Field on Certain WooCommerce Product Category Page’ is closed to new replies.