diff --git a/modules/price/includes/views/commerce_price.views.inc b/modules/price/includes/views/commerce_price.views.inc index f836c3e..88519ef 100644 --- a/modules/price/includes/views/commerce_price.views.inc +++ b/modules/price/includes/views/commerce_price.views.inc @@ -16,6 +16,66 @@ function commerce_price_field_views_data($field) { $data[$table_name][$field_name]['filter']['handler'] = 'commerce_price_handler_filter_commerce_price_amount'; $data[$table_name][$field_name]['filter']['allow empty'] = FALSE; } - + + // Add the precalculated price table. + $data['commerce_calculated_price'] = array( + 'table' => array( + 'group' => t("Commerce Product"), + 'join' => array( + 'commerce_product' => array( + 'left_field' => 'product_id', + 'field' => 'entity_id', + 'handler' => 'commerce_price_handler_join_calculated_price', + ), + ), + ), + 'amount' => array( + 'title' => t("Precalculated price"), + 'help' => t("The price after applying price calculation rules"), + 'field' => array( + 'handler' => 'commerce_price_handler_field_calculated_price', + 'click sortable' => TRUE, + 'additional fields' => array('currency_code'), + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + 'click sortable' => TRUE, + ), + ), + ); + return $data; } + +/** + * Custom calculated price field handler that displays the price correctly + * formatted. + */ +class commerce_price_handler_field_calculated_price extends views_handler_field{ + + function render($values) { + $amount = $this->get_value($values); + $currency_code = $this->get_value($values, 'currency_code'); + + return commerce_currency_format($amount, $currency_code); + } +} + +/** + * Custom join class that ensures the correct module_key is used on the join. + */ +class commerce_price_handler_join_calculated_price extends views_join { + + function build_join($select_query, $table, $view_query) { + + $key = commerce_product_pre_calculation_key(); + // Add the constraint on the module_key column. + $this->extra[] = array( + 'field' => 'module_key', + 'value' => $key, + ); + + parent::build_join($select_query, $table, $view_query); + } +} +