You are here

function uc_quote_nodeapi in Ubercart 5

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

Implementation of hook_nodeapi().

Load, save, and delete the shipping type and default shipping address of products.

File

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

Code

function uc_quote_nodeapi(&$node, $op, $arg3 = NULL, $arg4 = NULL) {
  if (in_array($node->type, module_invoke_all('product_types'))) {
    switch ($op) {
      case 'insert':
      case 'update':
        if (isset($node->shipping_type)) {
          uc_quote_set_shipping_type('product', $node->nid, $node->shipping_type);
        }
        db_query("DELETE FROM {uc_quote_product_locations} WHERE nid = %d", $node->nid);
        if ($node->street1) {
          db_query("INSERT INTO {uc_quote_product_locations} (nid, first_name, last_name, company, street1, street2, city, zone, postal_code, country, phone) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, '%s')", $node->nid, $node->first_name, $node->last_name, $node->company, $node->street1, $node->street2, $node->city, $node->zone, $node->postal_code, $node->country, $node->phone);
        }
        break;
      case 'load':

        // Can't just use uc_product_get_shipping_type() because $node->manufacturer doesn't necessarily exist yet.
        if (module_exists('uc_manufacturer')) {
          $manufacturer = uc_product_get_manufacturer($node->nid);
        }
        $shipping_type = db_result(db_query("SELECT shipping_type FROM {uc_quote_shipping_types} WHERE id_type = 'product' AND id = %d", $node->nid));
        if (!$shipping_type) {
          if (isset($manufacturer)) {
            $shipping_type = db_result(db_query("SELECT shipping_type FROM {uc_quote_shipping_types} WHERE id_type = 'manufacturer' AND id = %d", $manufacturer->tid));
          }
          if (!$shipping_type) {
            $shipping_type = variable_get('uc_store_shipping_type', 'small_package');
          }
        }
        $address = db_fetch_object(db_query("SELECT first_name, last_name, company, street1, street2, city, zone, postal_code, country, phone FROM {uc_quote_product_locations} WHERE nid = %d", $node->nid));
        if ($address === false) {
          if (module_exists('uc_manufacturer')) {
            $address = db_fetch_object(db_query("SELECT first_name, last_name, company, street1, street2, city, zone, postal_code, country, phone FROM {uc_quote_manufacturer_locations} WHERE tid = %d", $manufacturer->tid));
          }
          if ($address === false) {
            $address = variable_get('uc_quote_store_default_address', new stdClass());
          }
        }
        return array(
          'shipping_type' => $shipping_type,
          'shipping_address' => $address,
        );
        break;
      case 'delete':
        db_query("DELETE FROM {uc_quote_shipping_types} WHERE id_type = 'product' AND id = %d", $node->nid);
        db_query("DELETE FROM {uc_quote_product_locations} WHERE nid = %d", $node->nid);
        break;
    }
  }
}