Hello,

How difficult would it be to provide a formatter option in Views similar to the commerce price field type? For example:

  • Display the original price as loaded.
  • Display the calculated sell price for the current user.

Thanks for a great module,
Paul

Comments

pcambra’s picture

Status: Active » Fixed

I'd suggest you to take a look to Commerce price by components module.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

rbosscher’s picture

Issue summary: View changes

For what its worth.

I've created a custom formatter for views to display the smallest price in the table .
Maybe it saves somebody some efforts:


function MYMODULE_field_formatter_info() {
  return array(
    'commerce_min_max_table_price' => array(  //Machine name of the formatter
      'label' => t('View max/min Price'),
      'field types' => array('commerce_price_table'),   //This will only be available to price table fields
      'settings' => array(//Array of the settings we'll create
        'calculation' => FALSE,
        'price_label' => t('Price'),
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function MYMODULE_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];
  //This gets the actual settings
  $settings = $display['settings'];
  //Initialize the element variable
  $element = array();
  //Add your select box
  $element['price_sort'] = array(
    '#type' => 'select', // Use a select box widget
    '#title' => t('Show the Price'), // Widget label
    '#description' => t('Select what amount of the table must be visible'), // Helper text
    '#default_value' => $settings['price_sort'], // Get the value if it's already been set
    '#options' => array(
      'max' => 'Maximum price',
      'min' => 'Minimum price',
    ),
  );

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function MYMODULE_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Use the @amount for displaying the price"', array(
    '@size' => $settings['price_sort'],)); // we use t() for translation and placeholders to guard against attacks
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  $element = array();
  if ($display['type'] == 'commerce_min_max_table_price' && !empty($items)) {
    //load all amounts in array
    foreach ($items as $delta => $item) {
      if (isset($item['min_qty']) && $item['max_qty'] && $item['amount']) {
        $tArray[$delta] = $item['amount'];
        }
      }
    //select min amount
    if (isset($display['settings']['price_sort']) && $display['settings']['price_sort'] == 'min') {
      $delta = array_keys($tArray, min($tArray));
      }
    //select max amount
    if (isset($display['settings']['price_sort']) && $display['settings']['price_sort'] == 'max') {
      $delta = array_keys($tArray, max($tArray));
      }
    //generate output.
    if (isset($delta[0])) {
      $delta = $delta[0];
      $output = commerce_currency_format($items[$delta]['amount'], $items[$delta]['currency_code'], $entity);
      $element = array();
      $element[0]['#markup'] = "<span>" . $output . "</span>";
      }

    return $element;
  }
}