'Add weight quote', 'description' => 'Create a new weight based shipping 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', 'description' => 'Edit an existing weight based shipping quote.', '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', 'description' => 'Delete a weight based shipping quote.', '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(). * * Adds 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)) { $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' => 'uc_price', '#title' => check_plain($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'))), '#field_suffix' => t('per @unit', array('@unit' => variable_get('uc_weight_unit', 'lb'))), '#weight' => $weight['weightquote_' . $method->mid], '#empty_zero' => FALSE, ); } } } /** * 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 (empty($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 (is_numeric($rate) && $rate >= 0) { $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(); } /** * Implements hook_uc_shipping_method(). */ function uc_weightquote_uc_shipping_method() { $methods = array(); $result = db_query("SELECT mid, title, label, base_rate, product_rate FROM {uc_weightquote_methods}"); foreach ($result as $method) { $methods['weightquote_' . $method->mid] = array( 'id' => 'weightquote_' . $method->mid, 'module' => 'uc_weightquote', 'title' => $method->title, 'description' => t('!base_rate + !product_rate per !unit', array('!base_rate' => uc_currency_format($method->base_rate), '!product_rate' => uc_currency_format($method->product_rate), '!unit' => variable_get('uc_weight_unit', 'lb'))), 'operations' => array( 'edit' => array( 'title' => t('edit'), 'href' => 'admin/store/settings/quotes/methods/weightquote/' . $method->mid, ), 'delete' => array( 'title' => t('delete'), 'href' => 'admin/store/settings/quotes/weightquote/' . $method->mid . '/delete', ), ), 'quote' => array( 'type' => 'order', 'callback' => 'uc_weightquote_quote', 'accessorials' => array( $method->label, ), ), 'enabled' => TRUE, ); } return $methods; } /** * 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. * @param $method * The shipping method to use to create the quote. * * @return * An array 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 (!isset($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, 'label' => check_plain($method->label), 'option_label' => check_plain($method->label), ); } return $quotes; }