You are here

function uc_quote_node_load in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 shipping/uc_quote/uc_quote.module \uc_quote_node_load()

Implements hook_node_load().

File

shipping/uc_quote/uc_quote.module, line 84
The controller module for fulfillment modules that process physical goods.

Code

function uc_quote_node_load($nodes) {
  $nids = [];
  foreach ($nodes as $node) {
    if (uc_product_is_product($node)) {
      $nids[] = $node
        ->id();
    }
  }
  if (empty($nids)) {
    return;
  }
  $connection = \Drupal::database();
  $quote_config = \Drupal::config('uc_quote.settings');
  $shipping_type = $quote_config
    ->get('shipping_type');
  $shipping_types = $connection
    ->query("SELECT id, shipping_type FROM {uc_quote_shipping_types} WHERE id_type = :type AND id IN (:ids[])", [
    ':type' => 'product',
    ':ids[]' => $nids,
  ])
    ->fetchAllKeyed();
  $addresses = $connection
    ->query("SELECT nid, first_name, last_name, company, street1, street2, city, zone, postal_code, country, phone FROM {uc_quote_product_locations} WHERE nid IN (:nids[])", [
    ':nids[]' => $nids,
  ], [
    'fetch' => 'Address',
  ])
    ->fetchAllAssoc('nid');
  foreach ($nids as $nid) {
    if (isset($shipping_types[$nid])) {
      $nodes[$nid]->shipping_type = $shipping_types[$nid];
    }
    else {
      $nodes[$nid]->shipping_type = $shipping_type;
    }
    if (isset($addresses[$nid])) {
      $nodes[$nid]->shipping_address = (array) $addresses[$nid];
      unset($nodes[$nid]->shipping_address['nid']);
    }
    else {
      $nodes[$nid]->shipping_address = (array) $quote_config
        ->get('ship_from_address');
    }
  }
}