How to un-cache a specific node field (also for anonymous) My goal is to replace (by a custom markup) the render array of field_product when some conditions are met. (in a ecommerce site)

Ex: If this is the weekend or if it is after 12pm the field_product must not be rendered (but a markup like "the webshop is closed")

In mymodule_preprocess_node(&amp;$vars) I have tried without success:

$vars['content']['field_product']['#cache']['max-age']=0;


or

$vars['content']['field_product']['#cache']['contexts'][] = 'session';


Unfortunately, it is not working for anonymous users

The only solution I found is:

$vars['#cache']['max-age']=0;


But as you can see, all the node is un-cached... not the field_product

What is wrong with my code?

Again: this should be uncached for anonymous users
You have to make sure you add #cache data inside of a render array only to render elements (containing #type, #theme or #markup). In other places of a render array it doesn't work.

Where you add cache max-age or contexts inside of an entity doesn't make a difference though, because there is no field level, only entity level caching.

Another problem is page caching for anonymous users. See How do I disable Twig and block cache for a specific module?