You are here

function uc_product_kit_insert in Ubercart 5

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

Implementation of hook_insert().

Add a row to {uc_products} to make a product. Extra information about the component products are stored in {uc_product_kits}.

Parameters

&$node The node object being saved.:

See also

uc_product_insert

File

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

Code

function uc_product_kit_insert(&$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->ordering = $node->ordering;
  $obj->shippable = false;
  $values = array();
  $placeholders = array();
  foreach ($node->products as $product) {
    $product = node_load($product);
    $values[] = $node->vid;
    $values[] = $node->nid;
    $values[] = $product->nid;
    $values[] = $node->mutable;
    $values[] = 1;
    $values[] = $product->sell_price;
    $placeholders[] = '(%d, %d, %d, %d, %d, %f)';
    $obj->model .= $product->model . ' / ';
    $obj->list_price += $product->list_price;
    $obj->cost += $product->cost;
    $obj->sell_price += $product->sell_price;
    $obj->weight += $product->weight * uc_weight_conversion($product->weight_units, $obj->weight_units);
    if ($product->shippable) {
      $obj->shippable = true;
    }
  }
  db_query("INSERT INTO {uc_product_kits} (vid, nid, product_id, mutable, qty, discount) VALUES " . implode(',', $placeholders), $values);
  $obj->model = rtrim($obj->model, ' / ');
  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, $obj->shippable);
}