1

Is there a simple way to get the calculated sell price for the current user?

I try to get it from an implementation of hook_field_formatter_view.

Context: I use multiple rules to alter the base price. Work find but I want to alter the displayed price on product page. Sadly, from my hook implementation, the only price I get is the base price.

1 Answer 1

3

Find the tips: Add this to the array return by my_price_formatters_field_formatter_info() :

'settings' => array(
    'calculation' => TRUE,
  ),

Like this:

/**
 * Implements hook_field_formatter_info().
 */
function my_price_formatters_field_formatter_info() {
    return array(
    'commerce_price_for_me' => array(
      'label' => 'Price my way',
      'field types' => array('commerce_price'),
      'settings' => array(
        'calculation' => TRUE,
      ),
    ),
  );
}

Then add this function:

/**
 * Implements hook_field_formatter_settings_form().
 */
function my_price_formatters_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();

  switch($display['type']) {
    case 'commerce_price_for_me':
      $element['calculation'] = array(
        '#type' => 'checkbox',
        '#value' => TRUE,
        '#title' => t('Show calculated price'),
        '#default_value' => empty($settings['calculation']) ? 'TRUE' : $settings['calculation'],
      );
      break;
  }
  return $element;
}

And this one too:

/**
 * Implements hook_field_formatter_settings_summary().
 */
function my_price_formatters_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();

  switch ($display['type']) {
    case 'commerce_price_for_me':
      if ($settings['calculation'] == TRUE){
        $summary[] = 'Show calculated price.';
      } else {
        $summary[] = 'Show base price.';
      }
    break;
  }
  return implode('<br />', $summary);
}

Then amount (in $items or $entity) will be the calculated price.

And that's all folks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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