You are here

function uc_shipping_new_package_submit in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_shipping/uc_shipping.admin.inc \uc_shipping_new_package_submit()
  2. 7.3 shipping/uc_shipping/uc_shipping.admin.inc \uc_shipping_new_package_submit()

Submit handler for uc_shipping_new_package().

File

shipping/uc_shipping/uc_shipping.module, line 347
Organizes ordered products into packages and sets them up for shipment. Shipping method modules may add functionality to generate shipping labels and tracking numbers.

Code

function uc_shipping_new_package_submit($form_id, $form_values) {
  if ($form_values['op'] != t('Cancel')) {
    $packages = array(
      0 => array(),
    );
    foreach ($form_values['shipping_types'] as $shipping_type => $products) {
      foreach ($products as $id => $product) {
        if ($product['checked']) {
          if ($form_values['op'] == t('Create one package')) {
            $product['package'] = 1;
          }
          if ($product['package'] != 0) {
            $packages[$product['package']]['products'][$id] = (object) $product;
            if (!isset($packages[$product['package']]['shipping_type'])) {
              $packages[$product['package']]['shipping_type'] = $shipping_type;
            }
          }
          else {
            $packages[0][$shipping_type][$id] = (object) $product;
          }
        }
      }
      if (is_array($packages[0][$shipping_type])) {
        foreach ($packages[0][$shipping_type] as $id => $product) {
          $qty = $product->qty;
          $product->qty = 1;
          for ($i = 0; $i < $qty; $i++) {
            $packages[] = array(
              'products' => array(
                $id => $product,
              ),
              'shipping_type' => $shipping_type,
            );
          }
        }
      }
      unset($packages[0][$shipping_type]);
    }
    if (empty($packages[0])) {
      unset($packages[0]);
    }
    foreach ($packages as $package) {
      $package['order_id'] = $form_values['order_id'];
      uc_shipping_package_save($package);
    }
  }
  return 'admin/store/orders/' . $form_values['order_id'] . '/packages';
}