private function box_shipping($package)
 {
     if (!class_exists('WF_Boxpack')) {
         include_once 'class-wf-packing.php';
     }
     $boxpack = new WF_Boxpack();
     // Merge default boxes
     foreach ($this->default_boxes as $key => $box) {
         $box['enabled'] = isset($this->boxes[$box['id']]['enabled']) ? $this->boxes[$box['id']]['enabled'] : true;
         $this->boxes[] = $box;
     }
     // Define boxes
     foreach ($this->boxes as $key => $box) {
         if (!is_numeric($key)) {
             continue;
         }
         if (!$box['enabled']) {
             continue;
         }
         $newbox = $boxpack->add_box($box['length'], $box['width'], $box['height'], $box['box_weight']);
         if (isset($box['id'])) {
             $newbox->set_id(current(explode(':', $box['id'])));
         }
         if ($box['max_weight']) {
             $newbox->set_max_weight($box['max_weight']);
         }
     }
     // Add items
     foreach ($package['contents'] as $item_id => $values) {
         if (!$values['data']->needs_shipping()) {
             $this->debug(sprintf(__('Product # is virtual. Skipping.', 'wf-shipping-fedex'), $item_id), 'error');
             continue;
         }
         if ($values['data']->length && $values['data']->height && $values['data']->width && $values['data']->weight) {
             $dimensions = array($values['data']->length, $values['data']->height, $values['data']->width);
             for ($i = 0; $i < $values['quantity']; $i++) {
                 $boxpack->add_item(woocommerce_get_dimension($dimensions[2], 'in'), woocommerce_get_dimension($dimensions[1], 'in'), woocommerce_get_dimension($dimensions[0], 'in'), woocommerce_get_weight($values['data']->get_weight(), 'lbs'), $values['data']->get_price(), array('data' => $values['data']));
             }
         } else {
             $this->debug(sprintf(__('Product #%s is missing dimensions. Aborting.', 'wf-shipping-fedex'), $item_id), 'error');
             return;
         }
     }
     // Pack it
     $boxpack->pack();
     $packages = $boxpack->get_packages();
     $to_ship = array();
     $group_id = 1;
     foreach ($packages as $package) {
         if ($package->unpacked === true) {
             $this->debug('Unpacked Item');
         } else {
             $this->debug('Packed ' . $package->id);
         }
         $dimensions = array($package->length, $package->width, $package->height);
         sort($dimensions);
         $group = array('GroupNumber' => $group_id, 'GroupPackageCount' => 1, 'Weight' => array('Value' => max('0.5', round($package->weight, 2)), 'Units' => 'LB'), 'Dimensions' => array('Length' => max(1, round($dimensions[2], 2)), 'Width' => max(1, round($dimensions[1], 2)), 'Height' => max(1, round($dimensions[0], 2)), 'Units' => 'IN'), 'InsuredValue' => array('Amount' => round($package->value), 'Currency' => $this->wf_get_fedex_currency()), 'packed_products' => array(), 'package_id' => $package->id);
         if (!empty($package->packed) && is_array($package->packed)) {
             foreach ($package->packed as $packed) {
                 $group['packed_products'][] = $packed->get_meta('data');
             }
         }
         if ($this->freight_enabled) {
             $highest_freight_class = '';
             if (!empty($package->packed) && is_array($package->packed)) {
                 foreach ($package->packed as $item) {
                     if ($item->get_meta('data')->get_shipping_class_id()) {
                         $freight_class = $this->get_freight_class($item->get_meta('data')->get_shipping_class_id());
                         if ($freight_class > $highest_freight_class) {
                             $highest_freight_class = $freight_class;
                         }
                     }
                 }
             }
             $group['freight_class'] = $highest_freight_class ? $highest_freight_class : '';
         }
         $to_ship[] = $group;
         $group_id++;
     }
     return $to_ship;
 }
Exemplo n.º 2
0
 /**
  * calculate_flat_rate_box_rate function.
  *
  * @access private
  * @param mixed $package
  * @return void
  */
 private function calculate_flat_rate_box_rate($package, $box_type = 'priority')
 {
     global $woocommerce;
     $cost = 0;
     if (!class_exists('WF_Boxpack')) {
         include_once 'class-wf-packing.php';
     }
     $boxpack = new WF_Boxpack();
     $domestic = in_array($package['destination']['country'], $this->domestic) ? true : false;
     $added = array();
     // Define boxes
     foreach ($this->flat_rate_boxes as $service_code => $box) {
         if ($box['box_type'] != $box_type) {
             continue;
         }
         $domestic_service = substr($service_code, 0, 1) == 'd' ? true : false;
         if ($domestic && $domestic_service || !$domestic && !$domestic_service) {
             $newbox = $boxpack->add_box($box['length'], $box['width'], $box['height']);
             $newbox->set_max_weight($box['weight']);
             $newbox->set_id($service_code);
             if (isset($box['volume']) && method_exists($newbox, 'set_volume')) {
                 $newbox->set_volume($box['volume']);
             }
             if (isset($box['type']) && method_exists($newbox, 'set_type')) {
                 $newbox->set_type($box['type']);
             }
             $added[] = $service_code . ' - ' . $box['name'] . ' (' . $box['length'] . 'x' . $box['width'] . 'x' . $box['height'] . ')';
         }
     }
     $this->debug('Calculating USPS Flat Rate with boxes: ' . implode(', ', $added));
     // Add items
     foreach ($package['contents'] as $item_id => $values) {
         if (!$values['data']->needs_shipping()) {
             continue;
         }
         if ($values['data']->length && $values['data']->height && $values['data']->width && $values['data']->weight) {
             $dimensions = array($values['data']->length, $values['data']->height, $values['data']->width);
         } else {
             $this->debug(sprintf(__('Product #%d is missing dimensions! Using 1x1x1.', 'usps-woocommerce-shipping'), $item_id), 'error');
             $dimensions = array(1, 1, 1);
         }
         for ($i = 0; $i < $values['quantity']; $i++) {
             $boxpack->add_item(wc_get_dimension($dimensions[2], 'in'), wc_get_dimension($dimensions[1], 'in'), wc_get_dimension($dimensions[0], 'in'), wc_get_weight($values['data']->get_weight(), 'lbs'), $values['data']->get_price(), $item_id);
         }
     }
     // Pack it
     $boxpack->pack();
     // Get packages
     $flat_packages = $boxpack->get_packages();
     if ($flat_packages) {
         foreach ($flat_packages as $flat_package) {
             if (isset($this->flat_rate_boxes[$flat_package->id])) {
                 $this->debug('Packed ' . $flat_package->id . ' - ' . $this->flat_rate_boxes[$flat_package->id]['name']);
                 // Get pricing
                 $box_pricing = $this->settings['shippingrates'] == 'ONLINE' && isset($this->flat_rate_pricing[$flat_package->id]['online']) ? $this->flat_rate_pricing[$flat_package->id]['online'] : $this->flat_rate_pricing[$flat_package->id]['retail'];
                 if (is_array($box_pricing)) {
                     if (isset($box_pricing[$package['destination']['country']])) {
                         $box_cost = $box_pricing[$package['destination']['country']];
                     } else {
                         $box_cost = $box_pricing['*'];
                     }
                 } else {
                     $box_cost = $box_pricing;
                 }
                 // Fees
                 if (!empty($this->flat_rate_fee)) {
                     $sym = substr($this->flat_rate_fee, 0, 1);
                     $fee = $sym == '-' ? substr($this->flat_rate_fee, 1) : $this->flat_rate_fee;
                     if (strstr($fee, '%')) {
                         $fee = str_replace('%', '', $fee);
                         if ($sym == '-') {
                             $box_cost = $box_cost - $box_cost * (floatval($fee) / 100);
                         } else {
                             $box_cost = $box_cost + $box_cost * (floatval($fee) / 100);
                         }
                     } else {
                         if ($sym == '-') {
                             $box_cost = $box_cost - $fee;
                         } else {
                             $box_cost += $fee;
                         }
                     }
                     if ($box_cost < 0) {
                         $box_cost = 0;
                     }
                 }
                 $cost += $box_cost;
             } else {
                 return;
                 // no match
             }
         }
         if ($box_type == 'express') {
             $label = !empty($this->settings['flat_rate_express_title']) ? $this->settings['flat_rate_express_title'] : ($domestic ? '' : 'International ') . 'Priority Mail Express Flat Rate&#0174;';
         } else {
             $label = !empty($this->settings['flat_rate_priority_title']) ? $this->settings['flat_rate_priority_title'] : ($domestic ? '' : 'International ') . 'Priority Mail Flat Rate&#0174;';
         }
         return array('id' => $this->id . ':flat_rate_box_' . $box_type, 'label' => $label, 'cost' => $cost, 'sort' => $box_type == 'express' ? -1 : -2);
     }
 }