/** * Data provider for test_wc_get_dimension() * * @since 2.2.0 */ public function data_provider_wc_get_dimension() { // save default $default_unit = get_option('woocommerce_dimension_unit'); $cm = array(array(10, wc_get_dimension(10, 'cm')), array(3.937, wc_get_dimension(10, 'in')), array(0.10936133, wc_get_dimension(10, 'yd')), array(100, wc_get_dimension(10, 'mm')), array(0.1, wc_get_dimension(10, 'm'))); update_option('woocommerce_dimension_unit', 'in'); $in = array(array(25.4, wc_get_dimension(10, 'cm')), array(10, wc_get_dimension(10, 'in')), array(0.2777777782, wc_get_dimension(10, 'yd')), array(254, wc_get_dimension(10, 'mm')), array(0.254, wc_get_dimension(10, 'm'))); update_option('woocommerce_dimension_unit', 'm'); $m = array(array(1000, wc_get_dimension(10, 'cm')), array(393.7, wc_get_dimension(10, 'in')), array(10.936133, wc_get_dimension(10, 'yd')), array(10000, wc_get_dimension(10, 'mm')), array(10, wc_get_dimension(10, 'm'))); update_option('woocommerce_dimension_unit', 'mm'); $mm = array(array(1, wc_get_dimension(10, 'cm')), array(0.3937, wc_get_dimension(10, 'in')), array(0.010936133, wc_get_dimension(10, 'yd')), array(10, wc_get_dimension(10, 'mm')), array(0.01, wc_get_dimension(10, 'm'))); update_option('woocommerce_dimension_unit', 'yd'); $yd = array(array(914.4, wc_get_dimension(10, 'cm')), array(359.99928, wc_get_dimension(10, 'in')), array(10, wc_get_dimension(10, 'yd')), array(9144, wc_get_dimension(10, 'mm')), array(9.144, wc_get_dimension(10, 'm'))); $n = array(array(0, wc_get_dimension(-10, 'mm'))); // restore default update_option('woocommerce_dimension_unit', $default_unit); return array_merge($cm, $in, $m, $mm, $yd, $n); }
/** * Test wc_get_dimension() * * @since 2.2 */ public function test_wc_get_dimension() { // save default $default_unit = get_option('woocommerce_dimension_unit'); // cm (default unit) $this->assertEquals(10, wc_get_dimension(10, 'cm')); $this->assertEquals(3.937, wc_get_dimension(10, 'in')); $this->assertEquals(0.10936133, wc_get_dimension(10, 'yd')); $this->assertEquals(100, wc_get_dimension(10, 'mm')); $this->assertEquals(0.1, wc_get_dimension(10, 'm')); // in update_option('woocommerce_dimension_unit', 'in'); $this->assertEquals(25.4, wc_get_dimension(10, 'cm')); $this->assertEquals(10, wc_get_dimension(10, 'in')); $this->assertEquals(0.2777777782, wc_get_dimension(10, 'yd')); $this->assertEquals(254, wc_get_dimension(10, 'mm')); $this->assertEquals(0.254, wc_get_dimension(10, 'm')); // m update_option('woocommerce_dimension_unit', 'm'); $this->assertEquals(1000, wc_get_dimension(10, 'cm')); $this->assertEquals(393.7, wc_get_dimension(10, 'in')); $this->assertEquals(10.936133, wc_get_dimension(10, 'yd')); $this->assertEquals(10000, wc_get_dimension(10, 'mm')); $this->assertEquals(10, wc_get_dimension(10, 'm')); // mm update_option('woocommerce_dimension_unit', 'mm'); $this->assertEquals(1, wc_get_dimension(10, 'cm')); $this->assertEquals(0.3937, wc_get_dimension(10, 'in')); $this->assertEquals(0.010936133, wc_get_dimension(10, 'yd')); $this->assertEquals(10, wc_get_dimension(10, 'mm')); $this->assertEquals(0.01, wc_get_dimension(10, 'm')); // yd update_option('woocommerce_dimension_unit', 'yd'); $this->assertEquals(914.4, wc_get_dimension(10, 'cm')); $this->assertEquals(359.99928, wc_get_dimension(10, 'in')); $this->assertEquals(10, wc_get_dimension(10, 'yd')); $this->assertEquals(9144, wc_get_dimension(10, 'mm')); $this->assertEquals(9.144, wc_get_dimension(10, 'm')); // negative $this->assertEquals(0, wc_get_dimension(-10, 'mm')); // restore default update_option('woocommerce_dimension_unit', $default_unit); }
/** * Extracts the weight and dimensions from the 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()) { $_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'); $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)); }
/** * @deprecated */ function woocommerce_get_dimension($dim, $to_unit) { return wc_get_dimension($dim, $to_unit); }
/** * 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; }
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; }
public function get_volume($dimensions) { return array_product(array_map(function ($dim) { $meters = wc_get_dimension($dim, 'm'); if (WOOTAN_DEBUG) { error_log("-> converting {$dim} to meters: {$meters}"); } return $meters; }, $dimensions)); }
/** * 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; }
/** * Retrieve a measurement for a product in inches. * * @param int $product_id The product ID to retrieve the measurement for. * @param string $dimension The dimension to retrieve. "length", "width" or "height" * @return float The requested dimension for the given product. */ private function get_shipping_dimension($product_id, $dimension) { $product = $this->load_product($product_id); switch ($dimension) { case 'length': case 'width': case 'height': $measurement = $product->{$dimension}; break; default: return null; } if (empty($measurement)) { return null; } $measurement = wc_get_dimension($measurement, 'in'); return $measurement; }
public function get_goods() { $goods = array(); $count = 0; foreach ($this->package['contents'] as $item_id => $values) { $product = $values['data']; $qty = $values['quantity']; if ($qty > 0 && $product->needs_shipping()) { $_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'); $goods[$count]['height'] = !empty($_height) && $_height > 0 ? $_height : $this->minimum_height; $goods[$count]['width'] = !empty($_width) && $_width > 0 ? $_width : $this->minimum_width; $goods[$count]['length'] = !empty($_length) && $_length > 0 ? $_length : $this->minimum_length; $goods[$count]['weight'] = !empty($_weight) && $_weight > 0 ? $_weight : $this->minimum_weight; if ($qty > 1) { $n = $count; for ($i = 0; $i < $qty; $i++) { $goods[$n]['height'] = !empty($_height) && $_height > 0 ? $_height : $this->minimum_height; $goods[$n]['width'] = !empty($_width) && $_width > 0 ? $_width : $this->minimum_width; $goods[$n]['length'] = !empty($_length) && $_length > 0 ? $_length : $this->minimum_length; $goods[$n]['weight'] = !empty($_weight) && $_weight > 0 ? $_weight : $this->minimum_weight; $n++; } $count = $n; } $count++; } } return $goods; }
/** * 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®'; } 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); } }
/** * wcj_product_height. * * @version 2.5.5 * @since 2.5.5 */ function wcj_product_height($atts) { $return = '' != $atts['to_unit'] ? wc_get_dimension($this->the_product->get_height(), $atts['to_unit']) : $this->the_product->get_height(); return 'yes' === $atts['round'] ? round($return, $atts['precision']) : $return; }