uc_order_handler_field_order_weight_total.inc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @file
  4. * Total weight field handler.
  5. */
  6. /**
  7. * Field handler: displays a weight multiplied by the quantity.
  8. */
  9. class uc_order_handler_field_order_weight_total extends uc_product_handler_field_weight {
  10. function option_definition() {
  11. $options = parent::option_definition();
  12. $options['weight_units'] = array('default' => variable_get('uc_weight_unit', 'lb'));
  13. return $options;
  14. }
  15. function options_form(&$form, &$form_state) {
  16. parent::options_form($form, $form_state);
  17. $form['weight_units'] = array('#type' => 'select',
  18. '#title' => t('Unit of measurement'),
  19. '#default_value' => $this->options['weight_units'],
  20. '#options' => array(
  21. 'lb' => t('Pounds'),
  22. 'kg' => t('Kilograms'),
  23. 'oz' => t('Ounces'),
  24. 'g' => t('Grams'),
  25. ),
  26. );
  27. }
  28. /**
  29. * Overrides views_handler_field::query().
  30. */
  31. function query() {
  32. $this->ensure_my_table();
  33. $this->add_additional_fields();
  34. }
  35. /**
  36. * Overrides views_handler_field::render().
  37. */
  38. function render($values) {
  39. $oid = $values->{$this->aliases['order_id']};
  40. $order = uc_order_load($oid);
  41. $total = 0;
  42. foreach ($order->products as $product) {
  43. $unit_conversion = uc_weight_conversion($product->weight_units, $this->options['weight_units']);
  44. $total += $product->qty * $product->weight * $unit_conversion;
  45. }
  46. $this->field_alias = 'order_weight';
  47. $values->{$this->field_alias} = $total;
  48. if ($this->options['format'] == 'numeric') {
  49. return parent::render($values);
  50. }
  51. if ($this->options['format'] == 'uc_weight') {
  52. return uc_weight_format($values->{$this->field_alias}, $this->options['weight_units']);
  53. }
  54. }
  55. }