0

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'),
);

enter image description here

0

Views relies on the database to perform aggregation. Since the math field's value does not live in the database it doesn't work.

You can however use Views Aggregator plus that uses the calculated values instead.

this module:

  • enumerates group members
  • produces tallies, i.e. textual histograms
  • aggregates on Views PHP code-snippet evalutations
  • can filter out result rows on regexp patterns
  • gives you sorting on Views' Math expressions
  • can process Webform submissions
  • lets you add your own custom aggregation functions
  • Thank you for your reply, but I want to do this without add-on. How to do with a custom template ? I've created views-view-field--commerce-order-weight--block--expression.tpl.php in the templates folder of my theme. What should I write in this file ? – user54067 Jan 16 '17 at 13:03
  • It is not what templates are supposed to do... A template is for wrapping HTML tags around predefined variables. You should not be processing any logic in your template files. If you must process logic, do it in a preprocess hook in template.php file. I have no idea what to put there, always used the power of modules. – Neograph734 Jan 16 '17 at 13:36

Your Answer

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