NO BUG: Condition logic not working with custom dynamic data tag

Hi,

I implemented a custom dynamic data tag that returns the number of items in a Woo Commerce cart. While the tag works well in the page content, it is not functioning as expected with creating conditions for a block – eg. to hide a block when the cart count is “0”

image

I’ve tried returning a number of different values to test, using different conditions – empty, ==, >=, both text and numbers. No matter what I try, the conditions logic does not seem to evaluate the returned value correctly against the condition’s value.

Interestingly, it works fine using something like {post_id} and other built-in DD fields. It also works perfectly if I use an {echo:} to return the value from a custom function. Which is fine for this case, but for simplicity’s sake I would really like it to work with my custom DD field instead.

I might be missing something, but it seems that the conditions logic is not seeing my returned value correctly. Even doing a simple var_dump on the conditions.php logic shows that $value is a string longer than the actual content, and is not comparing it correctly to $required.

Hi @digifish,

How did you define your custom tag? Did you follow the example in the Academy?

Also, if you add your dynamic tag to the “basic text” element, does it work fine? Is the correct number shown?

Thanks,
Matej

Hi Matej,

Yes, I started with the example in the Academy. The only difference is that I generally put my filter functions in a class and built the function to handle multiple tags instead of just one. All of my tags are rendering correctly in basic text and other places, like urls.

  public static function get_tag_value($tag, $post, $context = null) {

        if(false === strpos($tag, '{el_prj_')) {
            return $tag;
        }

        switch($tag) {
            case '{el_prj_count}':
                return WC()->cart->get_cart_contents_count();
        
            //other tags... 
  }
    }

   public static function render_tag( $content, $post, $context = 'text' ) {

        if(false === strpos($content, '{el_prj_')) {
            return $content;
        }
 
        $tags = ['{el_prj_count}', '{el_prj_variation_id}', '{el_prj_variation_url}'];

        foreach($tags as $tag) {
            if(strpos($content, $tag)) {           
                $value =  self::get_tag_value($tag, $post);
                $content =  str_replace($tag, $value, $content);
            } 
        }
       
        return $content;
        
    }

Thanks.

Hi @digifish,

I believe this is NO BUG, but I will wait for your confirmation before I mark the topic as so.

In your code, you are checking for a tag position.

If the tag is in the beginning, the result will be 0, therefore it will be false, and this will not execute.
That is probably why it is not working in a condition, but it’s working in a basic text. If you only add your dynamic tag into the basic text, I am quite sure it will not render, right?

So, try to remove this check or compare it by adding !== false, and it should work.

Let me know if this will solve the problem.
Thanks!

1 Like

HI @Matej

thanks… That was the issue. I changed that to str_contains and that works. It was rendering fine in text but not for the condition. My bad. I spent to much time chasing my tail on that one.

1 Like

Hi,
perfect. I’m happy that it’s working now :slight_smile: I’ll mark this as a no bug, since it’s not really a bug o nour end. :slight_smile:

Best regards,
Matej

1 Like