'Weight quote', 'page callback' => 'uc_weightquote_admin_methods', 'access arguments' => array('configure quotes'), 'type' => MENU_LOCAL_TASK, 'file' => 'uc_weightquote.admin.inc', ); $items['admin/store/settings/quotes/methods/weightquote/add'] = array( 'title' => 'Add weight quote', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_weightquote_admin_method_edit_form'), 'access arguments' => array('configure quotes'), 'type' => MENU_LOCAL_ACTION, 'file' => 'uc_weightquote.admin.inc', ); $items['admin/store/settings/quotes/methods/weightquote/%'] = array( 'title' => 'Edit weight quote method', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_weightquote_admin_method_edit_form', 6), 'access arguments' => array('configure quotes'), 'file' => 'uc_weightquote.admin.inc', ); $items['admin/store/settings/quotes/weightquote/%/delete'] = array( 'title' => 'Delete weight quote method', 'page callback' => 'drupal_get_form', 'page arguments' => array('uc_weightquote_admin_method_confirm_delete', 5), 'access arguments' => array('configure quotes'), 'file' => 'uc_weightquote.admin.inc', ); return $items; } /** * Implements hook_form_alter(). * * Add a form element for the shipping rate of a product. */ function uc_weightquote_form_alter(&$form, &$form_state, $form_id) { if (uc_product_is_product_form($form)) { $sign_flag = variable_get('uc_sign_after_amount', FALSE); $currency_sign = variable_get('uc_currency_sign', '$'); $weight = variable_get('uc_quote_method_weight', array()); $result = db_query("SELECT mid, title, product_rate FROM {uc_weightquote_methods}"); foreach ($result as $method) { // Ensure the default weight is set. $weight += array('weightquote_' . $method->mid => 0); if (!isset($form['shipping']['weightquote'])) { $form['shipping']['weightquote'] = array( '#type' => 'fieldset', '#title' => t('Shipping rates by weight'), '#description' => t("Overrides the default shipping rate per product for each flat rate shipping method. Leave field empty to use the method's default value."), '#tree' => TRUE, '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => 0, ); } $form['shipping']['weightquote'][$method->mid] = array( '#type' => 'textfield', '#title' => $method->title, '#default_value' => isset($form['#node']->weightquote[$method->mid]) ? $form['#node']->weightquote[$method->mid] : '', '#description' => t('Default rate: %price per @unit', array('%price' => uc_currency_format($method->product_rate), '@unit' => variable_get('uc_weight_unit', 'lb'))), '#size' => 16, '#field_prefix' => $sign_flag ? '' : $currency_sign, '#field_suffix' => ($sign_flag ? ($currency_sign . ' ') : '') . t('per @unit', array('@unit' => variable_get('uc_weight_unit', 'lb'))), '#weight' => $weight['weightquote_' . $method->mid], ); } } } /** * Implements hook_node_insert(). */ function uc_weightquote_node_insert($node) { uc_weightquote_node_update($node); } /** * Implements hook_node_update(). */ function uc_weightquote_node_update($node) { if (uc_product_is_product($node->type)) { if (isset($node->weightquote) && is_array($node->weightquote)) { if (!$node->revision) { db_delete('uc_weightquote_products') ->condition('vid', $node->vid) ->execute(); } $query = db_insert('uc_weightquote-products') ->fields(array('vid', 'nid', 'mid', 'rate')); foreach ($node->weightquote as $mid => $rate) { if ($rate !== '') { $query->values(array( 'vid' => $node->vid, 'nid' => $node->nid, 'mid' => $mid, 'rate' => $rate, )); } } $query->execute(); } } } /** * Implements hook_node_load(). */ function uc_weightquote_node_load($nodes, $types) { $vids = array(); $product_types = uc_product_types(); foreach ($nodes as &$node) { if (in_array($node->type, $product_types)) { $vids[$node->nid] = $node->vid; } } if ($vids) { $result = db_query("SELECT nid, mid, rate FROM {uc_weightquote_products} WHERE vid IN (:vids)", array(':vids' => $vids)); foreach ($result as $method) { $nodes[$method->nid]->weightquote[$method->mid] = $method->rate; } } } /** * Implements hook_node_delete(). */ function uc_weightquote_node_delete($node) { db_delete('uc_weightquote_products') ->condition('nid', $node->nid) ->execute(); } /** * Implements hook_node_revision_delete(). */ function uc_weightquote_node_revision_delete($node) { db_delete('uc_weightquote_products') ->condition('vid', $node->vid) ->execute(); } /****************************************************************************** * Ubercart Hooks * ******************************************************************************/ /** * Implements hook_uc_shipping_method(). */ function uc_weightquote_uc_shipping_method() { $methods = array(); $enabled = variable_get('uc_quote_enabled', array()); $weight = variable_get('uc_quote_method_weight', array()); $result = db_query("SELECT mid, title, label FROM {uc_weightquote_methods}"); foreach ($result as $method) { // Ensure the default values are set. $enabled += array('weightquote_' . $method->mid => FALSE); $weight += array('weightquote_' . $method->mid => 0); $methods['weightquote_' . $method->mid] = array( 'id' => 'weightquote_' . $method->mid, 'module' => 'uc_weightquote', 'title' => $method->title, 'enabled' => isset($enabled['weightquote_' . $method->mid]) ? $enabled['weightquote_' . $method->mid] : FALSE, 'quote' => array( 'type' => 'order', 'callback' => 'uc_weightquote_quote', 'accessorials' => array( $method->label, ), ), 'weight' => isset($weight['weightquote_' . $method->mid]) ? $weight['weightquote_' . $method->mid] : 0, ); } return $methods; } /****************************************************************************** * Module Functions * ******************************************************************************/ /** * Standard callback to return a shipping rate via the weight quote method. * * @param $products * The order's products. * @param $details * Other order details including a shipping address. * * @return * A JSON object containing the shipping quote for the order. */ function uc_weightquote_quote($products, $details, $method) { $method = explode('_', $method['id']); $mid = $method[1]; if ($method = db_query("SELECT * FROM {uc_weightquote_methods} WHERE mid = :mid", array(':mid' => $mid))->fetchObject()) { // Start at the base rate. $rate = $method->base_rate; foreach ($products as $product) { if (empty($product->weightquote) || is_null($product->weightquote[$mid])) { // Add the method's default product rate. $product_rate = $method->product_rate * $product->qty; } else { // Add the product-specific rate. $product_rate = $product->weightquote[$mid] * $product->qty; } $rate += $product_rate * $product->weight * uc_weight_conversion($product->weight_units, variable_get('uc_weight_unit', 'lb')); } $quotes[] = array('rate' => $rate, 'option_label' => check_plain($method->label)); } return $quotes; }