Support

Account

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

  • 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;
    }