/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package) { $requests = array(); if (!class_exists('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_Boxpack(); // Add Standard UPS boxes if (!empty($this->ups_packaging)) { foreach ($this->ups_packaging as $key => $box_code) { $box = $this->packaging[$box_code]; $newbox = $boxpack->add_box($this->get_packaging_dimension($box['length']), $this->get_packaging_dimension($box['width']), $this->get_packaging_dimension($box['height'])); $newbox->set_inner_dimensions($this->get_packaging_dimension($box['length']), $this->get_packaging_dimension($box['width']), $this->get_packaging_dimension($box['height'])); $newbox->set_id($box['name']); if ($box['weight']) { $newbox->set_max_weight($this->get_packaging_weight($box['weight'])); } } } // Define boxes if (!empty($this->boxes)) { foreach ($this->boxes as $box) { $newbox = $boxpack->add_box($box['outer_length'], $box['outer_width'], $box['outer_height'], $box['box_weight']); $newbox->set_inner_dimensions($box['inner_length'], $box['inner_width'], $box['inner_height']); if ($box['max_weight']) { $newbox->set_max_weight($box['max_weight']); } } } // Add items $ctr = 0; foreach ($package['contents'] as $item_id => $values) { $ctr++; if (!$values['data']->needs_shipping()) { $this->debug(sprintf(__('Product #%d is virtual. Skipping.', 'woocommerce-shipping-ups'), $ctr)); 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(number_format(wc_get_dimension($dimensions[2], $this->dim_unit), 2, '.', ''), number_format(wc_get_dimension($dimensions[1], $this->dim_unit), 2, '.', ''), number_format(wc_get_dimension($dimensions[0], $this->dim_unit), 2, '.', ''), number_format(wc_get_weight($values['data']->get_weight(), $this->weight_unit), 2, '.', ''), $values['data']->get_price()); } } else { $this->debug(sprintf(__('UPS Parcel Packing Method is set to Pack into Boxes. Product #%d is missing dimensions. Aborting.', 'woocommerce-shipping-ups'), $ctr), 'error'); return; } } // Pack it $boxpack->pack(); // Get packages $box_packages = $boxpack->get_packages(); $ctr = 0; foreach ($box_packages as $key => $box_package) { $ctr++; $this->debug("PACKAGE " . $ctr . " (" . $key . ")\n<pre>" . print_r($box_package, true) . "</pre>"); $weight = $box_package->weight; $dimensions = array($box_package->length, $box_package->width, $box_package->height); sort($dimensions); // get weight, or 1 if less than 1 lbs. // $_weight = ( floor( $weight ) < 1 ) ? 1 : $weight; $request = '<Package>' . "\n"; $request .= ' <PackagingType>' . "\n"; $request .= ' <Code>02</Code>' . "\n"; $request .= ' <Description>Package/customer supplied</Description>' . "\n"; $request .= ' </PackagingType>' . "\n"; $request .= ' <Description>Rate</Description>' . "\n"; $request .= ' <Dimensions>' . "\n"; $request .= ' <UnitOfMeasurement>' . "\n"; $request .= ' <Code>' . $this->dim_unit . '</Code>' . "\n"; $request .= ' </UnitOfMeasurement>' . "\n"; $request .= ' <Length>' . $dimensions[2] . '</Length>' . "\n"; $request .= ' <Width>' . $dimensions[1] . '</Width>' . "\n"; $request .= ' <Height>' . $dimensions[0] . '</Height>' . "\n"; $request .= ' </Dimensions>' . "\n"; $request .= ' <PackageWeight>' . "\n"; $request .= ' <UnitOfMeasurement>' . "\n"; $request .= ' <Code>' . $this->weight_unit . '</Code>' . "\n"; $request .= ' </UnitOfMeasurement>' . "\n"; $request .= ' <Weight>' . $weight . '</Weight>' . "\n"; $request .= ' </PackageWeight>' . "\n"; if ($this->insuredvalue) { $request .= ' <PackageServiceOptions>' . "\n"; // InsuredValue if ($this->insuredvalue) { $request .= ' <InsuredValue>' . "\n"; $request .= ' <CurrencyCode>' . get_woocommerce_currency() . '</CurrencyCode>' . "\n"; $request .= ' <MonetaryValue>' . $box_package->value . '</MonetaryValue>' . "\n"; $request .= ' </InsuredValue>' . "\n"; } $request .= ' </PackageServiceOptions>' . "\n"; } $request .= '</Package>' . "\n"; $requests[] = $request; } return $requests; }
/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package) { global $woocommerce; $requests = array(); if (!class_exists('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_Boxpack(); // Add Standard UPS boxes if (!empty($this->ups_packaging)) { foreach ($this->ups_packaging as $key => $box_code) { $box = $this->packaging[$box_code]; $newbox = $boxpack->add_box($key, $box['length'], $box['width'], $box['height']); $newbox->set_inner_dimensions($box['length'], $box['width'], $box['height']); if ($box['weight']) { $newbox->set_max_weight($box['weight']); } } } // Define boxes if (!empty($this->boxes)) { foreach ($this->boxes as $box) { $newbox = $boxpack->add_box($box['outer_length'], $box['outer_width'], $box['outer_height'], $box['box_weight']); $newbox->set_inner_dimensions($box['inner_length'], $box['inner_width'], $box['inner_height']); if ($box['max_weight']) { $newbox->set_max_weight($box['max_weight']); } } } // Add items $ctr = 0; foreach ($package['contents'] as $item_id => $values) { $ctr++; if (!$values['data']->needs_shipping()) { if ($this->debug) { $woocommerce->add_message(sprintf(__('Product #%d is virtual. Skipping.', 'wc_ups'), $ctr)); } 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(number_format(woocommerce_get_dimension($dimensions[2], 'in'), 2, '.', ''), number_format(woocommerce_get_dimension($dimensions[1], 'in'), 2, '.', ''), number_format(woocommerce_get_dimension($dimensions[0], 'in'), 2, '.', ''), number_format(woocommerce_get_weight($values['data']->get_weight(), 'lbs'), 2, '.', ''), $values['data']->get_price()); } } else { if ($this->debug) { $woocommerce->add_error(sprintf(__('Product #%d is missing dimensions. Aborting.', 'wc_ups'), $ctr)); } return; } } // Pack it $boxpack->pack(); // Get packages $box_packages = $boxpack->get_packages(); $ctr = 0; foreach ($box_packages as $key => $box_package) { $ctr++; if ($this->debug) { $woocommerce->add_error("PACKAGE " . $ctr . " (" . $key . ")\n<pre>" . print_r($box_package, true) . "</pre>"); } $weight = $box_package->weight; $dimensions = array($box_package->length, $box_package->width, $box_package->height); sort($dimensions); // get weight, or 1 if less than 1 lbs. $_weight = floor($weight) < 1 ? 1 : floor($weight); $request = '<Package>' . "\n"; $request .= ' <PackagingType>' . "\n"; $request .= ' <Code>02</Code>' . "\n"; $request .= ' <Description>Package/customer supplied</Description>' . "\n"; $request .= ' </PackagingType>' . "\n"; $request .= ' <Description>Rate</Description>' . "\n"; $request .= ' <Dimensions>' . "\n"; $request .= ' <UnitOfMeasurement>' . "\n"; $request .= ' <Code>IN</Code>' . "\n"; $request .= ' </UnitOfMeasurement>' . "\n"; $request .= ' <Length>' . $dimensions[2] . '</Length>' . "\n"; $request .= ' <Width>' . $dimensions[1] . '</Width>' . "\n"; $request .= ' <Height>' . $dimensions[0] . '</Height>' . "\n"; $request .= ' </Dimensions>' . "\n"; $request .= ' <PackageWeight>' . "\n"; $request .= ' <UnitOfMeasurement>' . "\n"; $request .= ' <Code>LBS</Code>' . "\n"; $request .= ' </UnitOfMeasurement>' . "\n"; $request .= ' <Weight>' . $_weight . '</Weight>' . "\n"; $request .= ' </PackageWeight>' . "\n"; $request .= '</Package>' . "\n"; $requests[] = $request; } return $requests; }
/** * 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('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_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.', 'woocommerce-shipping-usps'), $item_id), 'error'); $dimensions = array(1, 1, 1); } 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()); } } // 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®'; } else { $label = !empty($this->settings['flat_rate_priority_title']) ? $this->settings['flat_rate_priority_title'] : ($domestic ? '' : 'International ') . 'Priority Mail Flat Rate®'; } return array('id' => $this->id . ':flat_rate_box_' . $box_type, 'label' => $label, 'cost' => $cost, 'sort' => $box_type == 'express' ? -1 : -2); } }
/** * Retrieves order items and packs them into boxes by dimensions * * @params mixed $order for order we are working with * @params array $shipment for order we are working with * * @return mixed array */ public static function get_packages($order, $shipment) { global $woocommerce; //load box packer if not already loaded if (!class_exists('WC_Boxpack')) { include_once WP_CONTENT_DIR . '/plugins/woocommerce-shipping-usps/includes/box-packer/class-wc-boxpack.php'; } //pack items if not set if (!isset($shipment['_packages'])) { $boxpack = new WC_Boxpack(); self::$shipper = self::get_shipper($order); //get boxes from shipper class settings $boxes = self::$shipper->get_boxes(); //Add Standard and Custom Boxes if (!empty($boxes)) { foreach ($boxes as $key => $box) { $newbox = $boxpack->add_box($box['outer_length'], $box['outer_width'], $box['outer_height'], $box['box_weight']); $newbox->set_inner_dimensions($box['inner_length'], $box['inner_width'], $box['inner_height']); $newbox->set_max_weight($box['max_weight']); $newbox->set_id = $key; } } //retrieve order items for packing $items = $order->get_items(); //add order items foreach ($items as $key => $item) { $product = $order->get_product_from_item($item); $item_key = $key; $dim = explode(' ', str_replace(' x ', ' ', $product->get_dimensions())); for ($i = 0; $i < $item['qty']; ++$i) { $boxpack->add_item(number_format(wc_get_dimension($dim[0], 'in'), 2), number_format(wc_get_dimension($dim[1], 'in'), 2), number_format(wc_get_dimension($dim[2], 'in'), 2), number_format(wc_get_weight($product->get_weight(), 'lbs'), 2), $product->get_price(), array('id' => $item['variation_id'] ? $item['variation_id'] : $item['product_id'])); } } //Pack Items into boxes & return $boxpack->pack(); //get packed items $shipment['_packages'] = $boxpack->get_packages(); } //normalize array for later use if (!is_array($shipment['_packages'][0]->packed[0])) { //Parse through items and convert std objects to arrays foreach ($shipment['_packages'] as $package) { //if no tracking number set if (!isset($package->id) || empty($package->id)) { $package->id = 'No Tracking# Assigned'; } //remove unnecessay data unset($package->unpacked); //convert WC_Item to array foreach ($package->packed as $key => $line) { $line = (array) $line; array_shift($line); $package->packed[$key] = $line; } } //save changes to DB //self::update_shipment( $order->id, $shipment ); } //return shipment for further processing return $shipment; }
/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package) { global $woocommerce; $requests = array(); if (!class_exists('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_Boxpack(); // Define boxes foreach ($this->boxes as $box) { $newbox = $boxpack->add_box($box['outer_length'], $box['outer_width'], $box['outer_height'], $box['box_weight']); $newbox->set_inner_dimensions($box['inner_length'], $box['inner_width'], $box['inner_height']); if ($box['max_weight']) { $newbox->set_max_weight($box['max_weight']); } if ($box['name']) { $newbox->set_id($box['name']); } } // Add items foreach ($package['contents'] as $item_id => $values) { if (!$values['data']->needs_shipping()) { $this->debug(sprintf(__('Product #%d is virtual. Skipping.', 'wc_canada_post'), $item_id)); 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], 'cm'), woocommerce_get_dimension($dimensions[1], 'cm'), woocommerce_get_dimension($dimensions[0], 'cm'), woocommerce_get_weight($values['data']->get_weight(), 'kg'), $values['data']->get_price()); } } else { if (function_exists('wc_add_notice')) { wc_add_notice(sprintf(__('Product # is missing dimensions. Aborting.', 'wc_canada_post'), $item_id), 'error'); } else { $woocommerce->add_error(sprintf(__('Product # is missing dimensions. Aborting.', 'wc_canada_post'), $item_id)); } return; } } // Pack it $boxpack->pack(); // Get packages $packages = $boxpack->get_packages(); foreach ($packages as $package) { $dimensions = array($package->length, $package->width, $package->height); sort($dimensions); $request = '<parcel-characteristics>' . "\n"; $request .= ' <weight>' . round($package->weight, 2) . '</weight>' . "\n"; $request .= ' <dimensions>' . "\n"; $request .= ' <height>' . round($dimensions[0], 1) . '</height>' . "\n"; $request .= ' <width>' . round($dimensions[1], 1) . '</width>' . "\n"; $request .= ' <length>' . round($dimensions[2], 1) . '</length>' . "\n"; $request .= ' </dimensions>' . "\n"; $request .= '</parcel-characteristics>' . "\n"; // Package options if (!empty($this->options)) { $option_request = ''; foreach ($this->options as $option) { if ($package['destination']['country'] !== 'CA' && 'PA18' === $option) { continue; } $option_request .= ' <option>' . "\n"; $option_request .= ' <option-code>' . $option . '</option-code>' . "\n"; if ($option == 'COV') { $option_request .= ' <option-amount>' . $package->value . '</option-amount>' . "\n"; } $option_request .= ' </option>' . "\n"; } if ($option_request) { $request .= '<options>' . "\n" . $option_request . '</options>' . "\n"; } } $requests[] = $request; } return $requests; }
/** * box_shipping function. * * @access private * @param mixed $package * @return array */ private function box_shipping($package) { if (!class_exists('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_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.', 'woocommerce-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.', 'woocommerce-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' => get_woocommerce_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; }
/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package) { global $woocommerce; $requests = array(); if (!class_exists('WC_Boxpack')) { include_once 'box-packer/class-wc-boxpack.php'; } $boxpack = new WC_Boxpack(); // Define boxes foreach ($this->boxes + $this->default_boxes as $key => $box) { if (!is_numeric($key)) { continue; } $newbox = $boxpack->add_box($box['length'], $box['width'], $box['height'], $box['box_weight']); if (isset($box['id'])) { $newbox->set_id($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']->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()); } } else { $woocommerce->add_error(sprintf(__('Product # is missing dimensions. Aborting.', 'wc_fedex'), $item_id)); return; } } // Pack it $boxpack->pack(); // Get packages $packages = $boxpack->get_packages(); $group = 1; foreach ($packages as $package) { $dimensions = array($package->length, $package->width, $package->height); sort($dimensions); $request = array(); $request['GroupNumber'] = $group; $request['GroupPackageCount'] = 1; $request['Weight'] = array('Value' => max('0.5', round($package->weight, 2)), 'Units' => 'LB'); if ($values['data']->length && $values['data']->height && $values['data']->width) { $request['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'); } $request['InsuredValue'] = array('Amount' => round($package->value), 'Currency' => get_woocommerce_currency()); $requests[] = $request; $group++; } return $requests; }