protected function getProductDimensions($product) { if (!$product) { return array(1, 1, 1, 1); } return array($product->width ? max(1, ceil(woocommerce_get_dimension($product->width, 'in'))) : 1, $product->length ? max(1, ceil(woocommerce_get_dimension($product->length, 'in'))) : 1, $product->height ? max(1, ceil(woocommerce_get_dimension($product->height, 'in'))) : 1, $product->weight ? max(1, ceil(woocommerce_get_weight($product->weight, 'lbs'))) : 1); }
/** * 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; }
private function per_item_shipping($package) { $to_ship = array(); $group_id = 1; // Get weight of order 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']->get_weight()) { $this->debug(sprintf(__('Product # is missing weight. Aborting.', 'wf-shipping-fedex'), $item_id), 'error'); return; } $group = array(); $group = array('GroupNumber' => $group_id, 'GroupPackageCount' => $values['quantity'], 'Weight' => array('Value' => max('0.5', round(woocommerce_get_weight($values['data']->get_weight(), 'lbs'), 2)), 'Units' => 'LB'), 'packed_products' => array($values['data'])); if ($values['data']->length && $values['data']->height && $values['data']->width) { $dimensions = array($values['data']->length, $values['data']->width, $values['data']->height); sort($dimensions); $group['Dimensions'] = array('Length' => max(1, round(woocommerce_get_dimension($dimensions[2], 'in'), 2)), 'Width' => max(1, round(woocommerce_get_dimension($dimensions[1], 'in'), 2)), 'Height' => max(1, round(woocommerce_get_dimension($dimensions[0], 'in'), 2)), 'Units' => 'IN'); } $group['InsuredValue'] = array('Amount' => round($values['data']->get_price()), 'Currency' => $this->wf_get_fedex_currency()); $to_ship[] = $group; $group_id++; } return $to_ship; }
/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package, $domestic) { global $woocommerce; $box_packages = $this->box_shipping_packages($package); if ($box_packages === false) { return; } $size = 'REGULAR'; // Get weight of order foreach ($box_packages as $key => $box_package) { $dimensions = array(woocommerce_get_dimension($box_package->length, 'in'), woocommerce_get_dimension($box_package->height, 'in'), woocommerce_get_dimension($box_package->width, 'in')); sort($dimensions); $girth = $dimensions[0] + $dimensions[0] + $dimensions[1] + $dimensions[1]; if (max($dimensions) > 12) { $size = 'LARGE'; } if ($domestic) { $piece = file_get_contents(USPS_PATH . '/xml/package.xml'); } else { $piece = file_get_contents(USPS_PATH . '/xml/package_intl.xml'); } $pounds = woocommerce_get_weight($box_package->weight, 'lbs'); $ounces = round(($pounds - floor($pounds)) * 16, 2); if ($ounces <= 0 && $pounds <= 0) { $ounces = 1; } $piece = str_replace('#ID#', $key . ':1', $piece); $piece = str_replace('#VALUE#', round($box_package->value, 2), $piece); $piece = str_replace('#SIZE#', $size, $piece); $piece = str_replace('#LENGTH#', $dimensions[2], $piece); $piece = str_replace('#WIDTH#', $dimensions[1], $piece); $piece = str_replace('#HEIGHT#', $dimensions[0], $piece); $piece = str_replace('#GIRTH#', round($girth), $piece); $piece = str_replace('#POUNDS#', floor($pounds), $piece); $piece = str_replace('#OUNCES#', $ounces, $piece); $this->package .= $piece; } }
/** * box_shipping function. * * @access private * @param mixed $package * @return void */ private function box_shipping($package) { global $woocommerce; $this->packages = array(); $box_packages = $this->box_shipping_packages($package); if ($box_packages === false) { return; } // Get weight of order foreach ($box_packages as $key => $box_package) { $piece = file_get_contents(UPS_PATH . '/xml/package.xml'); $piece = str_replace('#LENGTH#', number_format(woocommerce_get_dimension($box_package->length, $this->units['woo_dimensions']), 2, '.', ''), $piece); $piece = str_replace('#WIDTH#', number_format(woocommerce_get_dimension($box_package->width, $this->units['woo_dimensions']), 2, '.', ''), $piece); $piece = str_replace('#HEIGHT#', number_format(woocommerce_get_dimension($box_package->height, $this->units['woo_dimensions']), 2, '.', ''), $piece); $weight = woocommerce_get_weight($box_package->weight, $this->units['woo_weight']); $piece = str_replace('#WEIGHT#', round($weight, 1) < 0.1 ? 0.1 : round($weight, 1), $piece); $piece = str_replace('#WEIGHTUNIT#', $this->units['method_weight'], $piece); $piece = str_replace('#DIMENSIONUNIT#', $this->units['method_dimensions'], $piece); if ($this->insurance) { $package_option = "<PackageServiceOptions>\n\t\t\t\t\t\t\t\t\t\t<InsuredValue>\n\t\t\t\t\t\t\t\t\t\t\t<CurrencyCode>" . get_woocommerce_currency() . "</CurrencyCode>\n\t\t\t\t\t\t\t\t\t\t\t<MonetaryValue>" . round($box_package->value, 2) . "</MonetaryValue>\n\t\t\t\t\t\t\t\t\t\t</InsuredValue>\n\t\t\t\t\t\t\t\t\t</PackageServiceOptions>"; $piece = str_replace('#PACKAGESERVICEOPTIONS#', $package_option, $piece); } else { $piece = str_replace('#PACKAGESERVICEOPTIONS#', '', $piece); } if (!isset($this->packages[0])) { $this->packages[0] = null; } $this->packages[0] .= $piece; } }
/** * Extracts the weight and dimensions from the package. * * @param array $package * * @return array */ protected function get_package_data() { $count = 0; $height = array(); $width = array(); $length = array(); $weight = array(); // Shipping per item. foreach ($this->package['contents'] as $item_id => $values) { $product = $values['data']; $qty = $values['quantity']; if ($qty > 0 && $product->needs_shipping()) { if (version_compare(WOOCOMMERCE_VERSION, '2.1', '>=')) { $_height = wc_get_dimension($this->fix_format($product->height), 'cm'); $_width = wc_get_dimension($this->fix_format($product->width), 'cm'); $_length = wc_get_dimension($this->fix_format($product->length), 'cm'); $_weight = wc_get_weight($this->fix_format($product->weight), 'kg'); } else { $_height = woocommerce_get_dimension($this->fix_format($product->height), 'cm'); $_width = woocommerce_get_dimension($this->fix_format($product->width), 'cm'); $_length = woocommerce_get_dimension($this->fix_format($product->length), 'cm'); $_weight = woocommerce_get_weight($this->fix_format($product->weight), 'kg'); } $height[$count] = $_height; $width[$count] = $_width; $length[$count] = $_length; $weight[$count] = $_weight; if ($qty > 1) { $n = $count; for ($i = 0; $i < $qty; $i++) { $height[$n] = $_height; $width[$n] = $_width; $length[$n] = $_length; $weight[$n] = $_weight; $n++; } $count = $n; } $count++; } } return array('height' => array_values($height), 'length' => array_values($length), 'width' => array_values($width), 'weight' => array_sum($weight)); }
/** * 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); } }
protected function frenet_calculate($package) { $values = array(); $RecipientCEP = $package['destination']['postcode']; $RecipientCountry = $package['destination']['country']; // Checks if services and zipcode is empty. if (empty($RecipientCEP) && $RecipientCountry == 'BR') { if ('yes' == $this->debug) { $this->log->add($this->id, "ERRO: CEP destino não informado"); } return $values; } if (empty($this->zip_origin)) { if ('yes' == $this->debug) { $this->log->add($this->id, "ERRO: CEP origem não configurado"); } return $values; } // product array $shippingItemArray = array(); $count = 0; // Shipping per item. foreach ($package['contents'] as $item_id => $values) { $product = $values['data']; $qty = $values['quantity']; if ('yes' == $this->debug) { $this->log->add($this->id, 'Product: ' . print_r($product, true)); } $shippingItem = new stdClass(); if ($qty > 0 && $product->needs_shipping()) { if (version_compare(WOOCOMMERCE_VERSION, '2.1', '>=')) { $_height = wc_get_dimension($this->fix_format($product->height), 'cm'); $_width = wc_get_dimension($this->fix_format($product->width), 'cm'); $_length = wc_get_dimension($this->fix_format($product->length), 'cm'); $_weight = wc_get_weight($this->fix_format($product->weight), 'kg'); } else { $_height = woocommerce_get_dimension($this->fix_format($product->height), 'cm'); $_width = woocommerce_get_dimension($this->fix_format($product->width), 'cm'); $_length = woocommerce_get_dimension($this->fix_format($product->length), 'cm'); $_weight = woocommerce_get_weight($this->fix_format($product->weight), 'kg'); } if (empty($_height)) { $_height = $this->minimum_height; } if (empty($_width)) { $_width = $this->minimum_width; } if (empty($_length)) { $_length = $this->minimum_length; } if (empty($_weight)) { $_weight = 1; } $shippingItem->Weight = $_weight * $qty; $shippingItem->Length = $_length; $shippingItem->Height = $_height; $shippingItem->Width = $_width; $shippingItem->Diameter = 0; $shippingItem->SKU = $product->get_sku(); // wp_get_post_terms( your_id, 'product_cat' ); $shippingItem->Category = ''; $shippingItem->isFragile = false; if ('yes' == $this->debug) { $this->log->add($this->id, 'shippingItem: ' . print_r($shippingItem, true)); } $shippingItemArray[$count] = $shippingItem; $count++; } } if ('yes' == $this->debug) { $this->log->add($this->id, 'CEP ' . $package['destination']['postcode']); } $service_param = array('quoteRequest' => array('Username' => $this->login, 'Password' => $this->password, 'SellerCEP' => $this->zip_origin, 'RecipientCEP' => $RecipientCEP, 'RecipientDocument' => '', 'ShipmentInvoiceValue' => WC()->cart->cart_contents_total, 'ShippingItemArray' => $shippingItemArray, 'RecipientCountry' => $RecipientCountry)); if ('yes' == $this->debug) { $this->log->add($this->id, 'Requesting the Frenet WebServices...'); $this->log->add($this->id, print_r($service_param, true)); } // Gets the WebServices response. $client = new SoapClient($this->webservice, array("soap_version" => SOAP_1_1, "trace" => 1)); $response = $client->__soapCall("GetShippingQuote", array($service_param)); if ('yes' == $this->debug) { $this->log->add($this->id, $client->__getLastRequest()); $this->log->add($this->id, $client->__getLastResponse()); } if (is_wp_error($response)) { if ('yes' == $this->debug) { $this->log->add($this->id, 'WP_Error: ' . $response->get_error_message()); } } else { if (isset($response->GetShippingQuoteResult)) { if (count($response->GetShippingQuoteResult->ShippingSevicesArray->ShippingSevices) == 1) { $servicosArray[0] = $response->GetShippingQuoteResult->ShippingSevicesArray->ShippingSevices; } else { $servicosArray = $response->GetShippingQuoteResult->ShippingSevicesArray->ShippingSevices; } if (!empty($servicosArray)) { foreach ($servicosArray as $servicos) { if ('yes' == $this->debug) { $this->log->add($this->id, 'Percorrendo os serviços retornados'); } if (!isset($servicos->ServiceCode) || $servicos->ServiceCode . '' == '' || !isset($servicos->ShippingPrice)) { continue; } $code = (string) $servicos->ServiceCode; if ('yes' == $this->debug) { $this->log->add($this->id, 'WebServices response [' . $servicos->ServiceDescription . ']: ' . print_r($servicos, true)); } $values[$code] = $servicos; } } } } return $values; }
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; }
/** * 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; }
/** * per_item_shipping function. * * @access private * @param mixed $package * @return mixed $requests - an array of XML strings */ private function per_item_shipping($package) { global $woocommerce; $requests = array(); $ctr = 0; foreach ($package['contents'] as $item_id => $values) { $ctr++; if (!$values['data']->needs_shipping()) { $this->debug(sprintf(__('Product #%d is virtual. Skipping.', 'ups-woocommerce-shipping'), $ctr)); continue; } if (!$values['data']->get_weight()) { $this->debug(sprintf(__('Product #%d is missing weight. Aborting.', 'ups-woocommerce-shipping'), $ctr), 'error'); return; } // get package weight $weight = woocommerce_get_weight($values['data']->get_weight(), $this->weight_unit); // get package dimensions if ($values['data']->length && $values['data']->height && $values['data']->width) { $dimensions = array(number_format(woocommerce_get_dimension($values['data']->length, $this->dim_unit), 2, '.', ''), number_format(woocommerce_get_dimension($values['data']->height, $this->dim_unit), 2, '.', ''), number_format(woocommerce_get_dimension($values['data']->width, $this->dim_unit), 2, '.', '')); sort($dimensions); } // get quantity in cart $cart_item_qty = $values['quantity']; // 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"; if ($values['data']->length && $values['data']->height && $values['data']->width) { $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"; // WF: Calculating monetary value of cart item for insurance. $request .= ' <MonetaryValue>' . (string) ($values['data']->get_price() * $cart_item_qty) . '</MonetaryValue>' . "\n"; $request .= ' </InsuredValue>' . "\n"; } $request .= ' </PackageServiceOptions>' . "\n"; } $request .= '</Package>' . "\n"; for ($i = 0; $i < $cart_item_qty; $i++) { $requests[] = $request; } } return $requests; }
protected function box_shipping_packages($package) { global $woocommerce; if (!class_exists('SYN_Packing')) { require_once 'packing/class-syn-packing.php'; } $requests = array(); $boxpack = new SYN_Packing(); // Define boxes if (!empty($this->boxes)) { foreach ($this->boxes as $box) { $newbox = $boxpack->add_box($box); } } // Add items foreach ($package['contents'] as $item_id => $values) { if (!$values['data']->needs_shipping()) { $this->add_notice(sprintf(__('Product #%d is virtual. Skipping.', 'syn_shipping'), $values['product_id'])); continue; } if (!$values['data']->get_weight() || !$values['data']->length || !$values['data']->height || !$values['data']->width) { $this->add_notice(sprintf(__('Product <a href="%s" target="_blank">#%d</a> is missing dimensions and / or weight. Aborting %s quotes.', 'syn_shipping'), get_edit_post_link($values['product_id']), $values['product_id'], $this->method_title), 'error'); return false; } $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], $this->dimension_unit), 2, '.', ''), number_format(woocommerce_get_dimension($dimensions[1], $this->dimension_unit), 2, '.', ''), number_format(woocommerce_get_dimension($dimensions[0], $this->dimension_unit), 2, '.', ''), number_format(woocommerce_get_weight($values['data']->get_weight(), $this->weight_unit), 2, '.', ''), $values['data']->get_price()); } } // Pack it $boxpack->pack(); // Return packages return $boxpack->get_packages(); }
public function get_package_type( $package ) { $length = $package->length; $width = $package->width; $height = $package->height; $dimensions = array( woocommerce_get_dimension( $length, 'in' ), woocommerce_get_dimension( $height, 'in' ), woocommerce_get_dimension( $width, 'in' ) ); $girth = 2*( $width + $height ); if( ( ( $length >= 11.5 && $length <= 15 ) || ( $width >= 6.125 && $width <= 12 ) ) && ( $height >= 0.25 && $height <= 0.75 ) ) { $package_type = 'Large Envelope or Flat'; } else if( $girth + $length <= 84 ) { $package_type = 'Package'; } else if( ( $girth + $length > 84 ) && ( $length + $girth <= 108 ) ) { $package_type = 'Large Package'; } else if( ( max( $dimensions ) > 12 ) && ( $length + $girth > 108 && $length + $girth >= 130 ) ) { $package_type = 'Oversized Package'; } return $package_type; }
/** * per_item_shipping function. * * @access private * @param mixed $package * @return mixed $requests - an array of XML strings */ private function per_item_shipping($package, $params = array()) { global $woocommerce; $requests = array(); $ctr = 0; $this->cod = sizeof($package['contents']) > 1 ? false : $this->cod; // For multiple packages COD is turned off foreach ($package['contents'] as $item_id => $values) { $ctr++; if (!($values['quantity'] > 0 && $values['data']->needs_shipping())) { $this->debug(sprintf(__('Product #%d is virtual. Skipping.', 'ups-woocommerce-shipping'), $ctr)); continue; } if (!$values['data']->get_weight()) { $this->debug(sprintf(__('Product #%d is missing weight. Aborting.', 'ups-woocommerce-shipping'), $ctr), 'error'); return; } // get package weight $weight = woocommerce_get_weight($values['data']->get_weight(), $this->weight_unit); // get package dimensions if ($values['data']->length && $values['data']->height && $values['data']->width) { $dimensions = array(number_format(woocommerce_get_dimension($values['data']->length, $this->dim_unit), 2, '.', ''), number_format(woocommerce_get_dimension($values['data']->height, $this->dim_unit), 2, '.', ''), number_format(woocommerce_get_dimension($values['data']->width, $this->dim_unit), 2, '.', '')); sort($dimensions); } // get quantity in cart $cart_item_qty = $values['quantity']; $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"; if ($values['data']->length && $values['data']->height && $values['data']->width) { $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"; } if (isset($params['service_code']) && $params['service_code'] == 92) { if ($this->weight_unit == 'LBS') { // make sure weight in pounds $weight_ozs = $weight * 16; } else { $weight_ozs = $weight * 35.274; // From KG } $request .= ' <PackageWeight>' . "\n"; $request .= ' <UnitOfMeasurement>' . "\n"; $request .= ' <Code>OZS</Code>' . "\n"; $request .= ' </UnitOfMeasurement>' . "\n"; $request .= ' <Weight>' . $weight_ozs . '</Weight>' . "\n"; $request .= ' </PackageWeight>' . "\n"; } else { $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 || $this->cod) { $request .= ' <PackageServiceOptions>' . "\n"; // InsuredValue if ($this->insuredvalue) { $request .= ' <InsuredValue>' . "\n"; $request .= ' <CurrencyCode>' . get_woocommerce_currency() . '</CurrencyCode>' . "\n"; // WF: Calculating monetary value of cart item for insurance. $request .= ' <MonetaryValue>' . (string) ($values['data']->get_price() * $cart_item_qty) . '</MonetaryValue>' . "\n"; $request .= ' </InsuredValue>' . "\n"; } //Code if ($this->cod) { $cod_value = $this->cod_total; $request .= '<COD>' . "\n"; $request .= '<CODCode>3</CODCode>' . "\n"; $request .= '<CODFundsCode>0</CODFundsCode>' . "\n"; $request .= '<CODAmount>' . "\n"; $request .= '<CurrencyCode>' . get_woocommerce_currency() . '</CurrencyCode>' . "\n"; $request .= '<MonetaryValue>' . $cod_value . '</MonetaryValue>' . "\n"; $request .= '</CODAmount>' . "\n"; $request .= '</COD>' . "\n"; } $request .= ' </PackageServiceOptions>' . "\n"; } $request .= '</Package>' . "\n"; for ($i = 0; $i < $cart_item_qty; $i++) { $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(); // 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; }