You are here

function uc_product_kit_update in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_product_kit/uc_product_kit.module \uc_product_kit_update()
  2. 7.3 uc_product_kit/uc_product_kit.module \uc_product_kit_update()

Implementation of hook_update().

Updates information in {uc_products} as well as {uc_product_kits}. Because component products are known when the form is loaded, discount information can be input and saved.

Parameters

&$node The node to be updated.:

See also

uc_product_update

File

uc_product_kit/uc_product_kit.module, line 135
The product kit module for Übercart.

Code

function uc_product_kit_update(&$node) {
  $obj = new stdClass();
  $obj->vid = $node->vid;
  $obj->nid = $node->nid;
  $obj->model = '';
  $obj->list_price = 0;
  $obj->cost = 0;
  $obj->sell_price = 0;
  $obj->weight = 0;
  $obj->weight_units = variable_get('uc_weight_unit', 'lb');
  $obj->default_qty = $node->default_qty;
  $obj->ordering = $node->ordering;
  $obj->shippable = false;
  $values = array();
  $placeholders = array();
  foreach ($node->products as $nid) {
    $values[] = $node->vid;
    $values[] = $node->nid;
    $values[] = $nid;
    $values[] = $node->mutable;
    $product = node_load($nid);
    if (is_null($node->items[$nid]['qty']) || $node->items[$nid]['qty'] === '') {
      $node->items[$nid]['qty'] = 1;
    }
    $values[] = $node->items[$nid]['qty'];
    if (is_null($node->items[$nid]['discount']) || $node->items[$nid]['discount'] === '') {
      $node->items[$nid]['discount'] = $product->sell_price;
    }
    $values[] = $node->items[$nid]['discount'];
    $values[] = $node->items[$nid]['ordering'];
    $placeholders[] = '(%d, %d, %d, %d, %d, %f, %d)';
    $product->qty = $node->items[$nid]['qty'];
    if ($node->items[$nid]['discount'] < 0) {
      $product->sell_price += $node->items[$nid]['discount'];
    }
    else {
      $product->sell_price = $node->items[$nid]['discount'];
    }
    $obj->model .= $product->model . ' / ';
    $obj->list_price += $product->list_price * $product->qty;
    $obj->cost += $product->cost * $product->qty;
    $obj->sell_price += $product->sell_price * $product->qty;
    $obj->weight += $product->weight * $product->qty * uc_weight_conversion($product->weight_units, $obj->weight_units);
    if ($product->shippable) {
      $obj->shippable = true;
    }
  }
  $obj->model = rtrim($obj->model, ' / ');
  if (!$node->revision) {
    db_query("DELETE FROM {uc_product_kits} WHERE nid = %d", $node->nid);
  }
  db_query("INSERT INTO {uc_product_kits} (vid, nid, product_id, mutable, qty, discount, ordering) VALUES " . implode(',', $placeholders), $values);
  if ($node->revision) {
    db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . time()), $obj->ordering, $obj->shippable);
  }
  else {
    db_query("UPDATE {uc_products} SET model = '%s', list_price = %f, cost = %f, sell_price = %f, weight = %f, weight_units = '%s', default_qty = %d, ordering = %d, shippable = %d WHERE vid = %d", $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, $obj->ordering, $obj->shippable, $obj->vid);
    if (!db_affected_rows()) {
      db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . $obj->shippable . time()), $obj->ordering, $boj->shippable);
    }
  }
}