Support

Account

Home Forums ACF PRO Using Options Page values in functions.php

Helping

Using Options Page values in functions.php

  • I am trying to use values from my ACF Options page to control whether a function is executed in functions.php and then have those values used in the function.

    This is similar to a topic posted in 2013 where Elliot stated the issue would be fixed in the next major version. We’re several versions later now, but I am still having trouble doing this.

    Here is my code:

    $disabled_ground = get_field('ground_disable_for_states', 'option');
    if ($disabled_ground == '') {} else {
    	function disable_ups_ground_by_state( $rates ) {
    		global $woocommerce;
    		$excluded_states = array( $disabled_ground ); 
    		if( in_array( $woocommerce->customer->get_shipping_state(), $excluded_states ) ) {
    				unset( $rates['ups:03'] );
    		}
    		return $rates;
    	}
    	add_filter( 'woocommerce_package_rates', 'disable_ups_ground_by_state', 10 );
    }

    Here is a link to the topic from 2013 regarding this problem: http://support.advancedcustomfields.com/forums/topic/using-options-page-fields-in-functions-php/

    Hoping there is now a solution for this!

  • I don’t know it this is something that will be fixed or not. As a general rule I don’t use any plugin functions for ACF until after the init action.

    First thing is that you need to get the value inside your function because the value of outside the function will have no effect on the value inside the function.

    Since you have to get the value in the function anyway I would change the code to something like this:

    
     
      function disable_ups_ground_by_state( $rates ) {
        $disabled_ground = get_field('ground_disable_for_states', 'option');
        if (!$disabled_ground) {
          return;
        }
        global $woocommerce;
        // you also need to test to see if it's an array
        // before convering it ot an array
        if (!is_array($disabled_ground)) {
          $disabled_ground = array($disabled_ground);
        }
        if( in_array( $woocommerce->customer->get_shipping_state(), $disabled_ground ) ) {
          unset($rates['ups:03']);
        }
        return $rates;
      }
      add_filter( 'woocommerce_package_rates', 'disable_ups_ground_by_state', 10 );
     
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Using Options Page values in functions.php’ is closed to new replies.