Add "Hide price if not orderable" option

Created on 15 August 2024, 8 months ago
Updated 19 August 2024, 8 months ago

Problem/Motivation

We just discussed, if a boolean option to hide the price, if a product isn't orderable might also make sense?

Might need a snippet like this: https://julian.pustkuchen.com/en/snippet-drupal-commerce-hide-price-fiel...

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Postponed

Version

1.0

Component

Code (commerce_product_availability)

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Anybody
  • Assigned to Anybody
  • 🇩🇪Germany Anybody Porta Westfalica

    Ok seems we need it -.-

  • First commit to issue fork.
  • 🇩🇪Germany Grevil

    Not as simple as it seems first...

    It seems, that we need to hook into the view, instead of using "hook_entity_field_access" as we do not have access to the entity through the field definition or any other parameter provided.

  • 🇩🇪Germany Anybody Porta Westfalica

    @grevil yes I know. Please wait with the implementation here, we first have to discuss if and how we can solve this best.
    For that reason, it's assigned to me.

  • Issue was unassigned.
  • Status changed to Postponed 8 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Update: For now we won't need it, as I now solved it differently (apart from this module) by hiding the price, if it's empty:

    /**
     * Implements hook_entity_field_access().
     */
    function drowl_customer_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, ?\Drupal\Core\Field\FieldItemListInterface $items = NULL) {
      // Hide the price fields displays:
      if ($operation === 'view') {
        if (in_array($field_definition->getName(), ['price', 'list_price', 'unit_price', 'total_price'])) {
          $priceValue = $items->getValue();
          // Hide price if empty:
          if(isset($priceValue['number']) && empty($priceValue)){
            return \Drupal\Core\Access\AccessResult::forbidden();
          }
        }
      }
      return \Drupal\Core\Access\AccessResult::neutral();
    }
    

    Still this option may make sense for this module in the future.

Production build 0.71.5 2024