1

I'm using WooCommerce and Advanced Custom Fields plugin. I have a function for calculate my product's discount, like this:

function woo_add_cart_fee() {

  global $woocommerce;

  $discount = floatval(10);
    if(!empty($discount) || $discount != 0){
    $discount *= -1;
    $woocommerce->cart->add_fee('discount', $discount, true, '' );
  } 
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

It's working, but my discount value is dynamic, so must work with custom fields. When I add this line to the function, I get an empty value:

$value = get_field( "text_field" );

if( $value ) {
    echo $value;
} else {
    echo 'empty';
}

But actually, the custom field is not empty in the WordPress admin panel. What am I doing wrong here, and how can I fix it?

0

1 Answer 1

3

This is because you're doing it too early. You need to wrap your code in the necessary hook so that it occurs at the correct time.

Because you don't tell get_field the ID of the current post, it needs to know which post to get the field from, and it does this by referencing the global variable $post, which gets set when you call the_post, or by WP Core early on during a page load.

But if you try to do this before it's set, the function doesn't know what the current post is, and fails. This is what you're seeing in functions.php. When functions.php is loaded, WordPress hasn't set up the post loop, or queried the database, it's still in the process of loading the theme and plugin files. The earliest event/hook that the $post is set, would be the wp event, your code runs earlier than this.

So instead, lets refer to the core load diagram, which says it's safe to run code from the init hook and onwards. I'm going to suggest using the next hook, wp_loaded instead, but change this at your discretion:

function karmacoma_206593() {
    $value = get_field( "text_field" );
    if( $value ) {
        echo $value;
    } else {
        echo 'empty';
    }
}
add_action( 'wp_loaded', 'karmacoma_206593' );

Here I've put your code inside a function and told WordPress to call it on the init event/hook/action. To make sure it doesn't clash with any other code you have, I've used your username and the ID of this question as the functions name.

Next, we have 2 new issues you're unaware of. First, your if statements check could be better. For example, if your value is false, your check will fail, even if it's a valid value, so I'm going to improve it by using empty:

if( !empty( $value ) ) {

Secondly, you're outputting the value of this field without any form of escaping. What if the value of the field is a script tag? Lets secure this code by using esc_html to strip out any html tags:

echo esc_html( $value );

Giving us this:

function karmacoma_206593() {
    $value = get_field( "text_field" );
    if ( !empty( $value ) ) {
        echo esc_html( $value );
    } else {
        echo 'empty';
    }
}
add_action( 'wp_loaded', 'karmacoma_206593' );
2
  • Thanks for your great answer. I used your karmacoma_206593 function in my file but still doesn't work. I don't know why my value always empty. Whereas when I put get_field('test_field') in theme file, it works good.
    – Karmacoma
    Commented Oct 26, 2015 at 14:51
  • 2
    Yes! Problem is I don't tell post ID, added and fixed! Thank you.
    – Karmacoma
    Commented Oct 26, 2015 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.