You are here

function uc_product_node_load in Ubercart 8.4

Implements hook_node_load().

File

uc_product/uc_product.module, line 107
The product module for Ubercart.

Code

function uc_product_node_load($nodes) {
  $vids = [];
  foreach ($nodes as $node) {
    if (uc_product_is_product($node)) {
      $vids[$node
        ->id()] = $node
        ->getRevisionId();
    }
  }
  if (!empty($vids)) {
    $connection = \Drupal::database();
    $result = $connection
      ->query('SELECT nid, model, cost, price, weight, weight_units, length, width, height, length_units, pkg_qty, default_qty, shippable FROM {uc_products} WHERE vid IN (:vids[])', [
      ':vids[]' => $vids,
    ]);
    $fields = [
      'model',
      'cost',
      'price',
      'weight',
      'pkg_qty',
      'default_qty',
      'shippable',
    ];
    foreach ($result as $record) {
      foreach ($fields as $name) {
        $nodes[$record->nid]->{$name}->value = $record->{$name};
      }
      $nodes[$record->nid]->weight->units = $record->weight_units;
      $nodes[$record->nid]->dimensions->length = $record->length;
      $nodes[$record->nid]->dimensions->width = $record->width;
      $nodes[$record->nid]->dimensions->height = $record->height;
      $nodes[$record->nid]->dimensions->units = $record->length_units;
      $nodes[$record->nid]->display_price = $nodes[$record->nid]->price->value;
      $nodes[$record->nid]->display_price_suffixes = [];
    }
  }
}