If you're using the wonderful Drupal Commerce, you may run into situations, where the product prices should not be shown (yet), e.g. if you're just preparing the shop or orders should just be requests first.
Sometimes you'd also want to hide prices from certain user roles.
The following snippet can be used to hide Drupal Commerce Product prices:
/**
* Implements hook_entity_field_access().
*/
function HOOK_entity_field_access($operation, FieldDefinitionInterface
// Hide the price fields displays:
if ($operation === 'view') {
if (in_array($field_definition->getName(), ['price', 'list_price', 'unit_price', 'total_price'])) {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}
It's inspired by the great field_permissions module!