I installed "Physical Fields" and I created a view (block) to display the total weight of the order in the shopping cart. In my view I added the "price" and "quantity" fields, then I add a "Global: Mathematical Expression" field with "[field_weight] * [quantity]". I activated the aggregation on the view and I configure the parameters of the group for Global field: Mathematical expression sue "sum".

The view does not work and does not display sums. I have seen that the field of mathematical expression can not be aggregated.

How do I get the sum of this field ?

Can I do this in the "template.php" file of my theme? If yes, how ?

Here is the export of my view :

Thank you.

$view = new view();
$view->name = 'commerce_order_weight';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'commerce_order';
$view->human_name = 'Poids de la commande';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Poids panier';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['use_more_text'] = 'plus';
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'role';
$handler->display->display_options['access']['role'] = array(
  7 => '7',
  3 => '3',
);
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['submit_button'] = 'Appliquer';
$handler->display->display_options['exposed_form']['options']['reset_button_label'] = 'Réinitialiser';
$handler->display->display_options['exposed_form']['options']['exposed_sorts_label'] = 'Trier par';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relation: Commande Drupal Commerce : Referenced line items */
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['id'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['table'] = 'field_data_commerce_line_items';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['field'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['required'] = TRUE;
/* Relation: Commerce Line item : Referenced products */
$handler->display->display_options['relationships']['commerce_product_product_id']['id'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['table'] = 'field_data_commerce_product';
$handler->display->display_options['relationships']['commerce_product_product_id']['field'] = 'commerce_product_product_id';
$handler->display->display_options['relationships']['commerce_product_product_id']['relationship'] = 'commerce_line_items_line_item_id';
/* Relation: Commerce Product : Referencing Nœud */
$handler->display->display_options['relationships']['field_produit']['id'] = 'field_produit';
$handler->display->display_options['relationships']['field_produit']['table'] = 'commerce_product';
$handler->display->display_options['relationships']['field_produit']['field'] = 'field_produit';
$handler->display->display_options['relationships']['field_produit']['relationship'] = 'commerce_product_product_id';
/* Champ: Commande Drupal Commerce : Identifiant (ID) de la commande */
$handler->display->display_options['fields']['order_id']['id'] = 'order_id';
$handler->display->display_options['fields']['order_id']['table'] = 'commerce_order';
$handler->display->display_options['fields']['order_id']['field'] = 'order_id';
$handler->display->display_options['fields']['order_id']['label'] = '';
$handler->display->display_options['fields']['order_id']['exclude'] = TRUE;
$handler->display->display_options['fields']['order_id']['element_label_colon'] = FALSE;
/* Champ: Commerce Product : Poids */
$handler->display->display_options['fields']['field_weight']['id'] = 'field_weight';
$handler->display->display_options['fields']['field_weight']['table'] = 'field_data_field_weight';
$handler->display->display_options['fields']['field_weight']['field'] = 'field_weight';
$handler->display->display_options['fields']['field_weight']['relationship'] = 'commerce_product_product_id';
$handler->display->display_options['fields']['field_weight']['label'] = '';
$handler->display->display_options['fields']['field_weight']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_weight']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_weight']['click_sort_column'] = 'weight';
$handler->display->display_options['fields']['field_weight']['group_column'] = 'entity_id';
/* Champ: Commerce Line Item : Quantité */
$handler->display->display_options['fields']['quantity']['id'] = 'quantity';
$handler->display->display_options['fields']['quantity']['table'] = 'commerce_line_item';
$handler->display->display_options['fields']['quantity']['field'] = 'quantity';
$handler->display->display_options['fields']['quantity']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['fields']['quantity']['label'] = '';
$handler->display->display_options['fields']['quantity']['exclude'] = TRUE;
$handler->display->display_options['fields']['quantity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['quantity']['precision'] = '0';
$handler->display->display_options['fields']['quantity']['separator'] = '';
/* Champ: SUM(Global : Expression mathématique) */
$handler->display->display_options['fields']['expression']['id'] = 'expression';
$handler->display->display_options['fields']['expression']['table'] = 'views';
$handler->display->display_options['fields']['expression']['field'] = 'expression';
$handler->display->display_options['fields']['expression']['group_type'] = 'sum';
$handler->display->display_options['fields']['expression']['label'] = 'Poids de la commande';
$handler->display->display_options['fields']['expression']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['expression']['precision'] = '0';
$handler->display->display_options['fields']['expression']['separator'] = '';
$handler->display->display_options['fields']['expression']['expression'] = '[field_weight]*[quantity]';
/* Filtre contextuel: Commande Drupal Commerce : Identifiant (ID) de la commande */
$handler->display->display_options['arguments']['order_id']['id'] = 'order_id';
$handler->display->display_options['arguments']['order_id']['table'] = 'commerce_order';
$handler->display->display_options['arguments']['order_id']['field'] = 'order_id';
$handler->display->display_options['arguments']['order_id']['default_action'] = 'default';
$handler->display->display_options['arguments']['order_id']['exception']['title'] = 'Tout';
$handler->display->display_options['arguments']['order_id']['default_argument_type'] = 'commerce_cart_current_cart_order_id';
$handler->display->display_options['arguments']['order_id']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['order_id']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['order_id']['summary_options']['items_per_page'] = '25';
/* Critère de filtrage: Commande Drupal Commerce : Type de commande */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'commerce_order';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'commerce_order' => 'commerce_order',
);

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
$translatables['commerce_order_weight'] = array(
  t('Master'),
  t('Poids panier'),
  t('plus'),
  t('Appliquer'),
  t('Réinitialiser'),
  t('Trier par'),
  t('Asc'),
  t('Desc'),
  t('Line items referenced by commerce_line_items'),
  t('Products referenced by commerce_product'),
  t('Nœud referencing products from field_produit'),
  t('.'),
  t('Poids de la commande'),
  t('Tout'),
  t('Block'),
);

Comments

zenimagine created an issue. See original summary.

rivimey’s picture

Status: Active » Closed (works as designed)

Hi, I have just tested using numbers from default node entity: the node id and user id. Note that you must ensure the values of these fields are actually numeric (not, e.g. an html link, which is default for user-id!). I can output a field in a views row that is the product of these two.

However, I believe the issue you are seeing here is that the "mathematical expression" is being calculated after the SQL query is run, while the Aggregation feature in views creates an SQL aggregation expression, so is done in the SQL query. If this is indeed the case, there is nothing ctools can really do.

If the aggregation is done on the math expression field, you are essentially asking it to sum('[field_weight]*[field_quantity]', '[field_weight]*[field_quantity]', ...) or similar, which doesn't end well.

I believe in this situation you should either:

- persuade Views to create a SQL query including the required expression (weight*quantity) on the DB server (assuming that is possible with the Physical Field data: if weight values are stored in a single quantity it should be ok). Possibly the "views_raw_sql" module might help.

- write a custom sql query that performs the sum which is then injected into the html template later, using either php or mysql to aggregate as required;

- determine if it is possible to hook into the view dataset after sql query but before rendering; if possible, you could then inspect the data and perform the sum in php.

Maybe this article is helpful: http://tech-tamer.com/drupal-7-using-sql-expressions-in-views-3/

zenimagine’s picture

Thank you for your explanations.