本文整理匯總了PHP中J2Store::weight方法的典型用法代碼示例。如果您正苦於以下問題:PHP J2Store::weight方法的具體用法?PHP J2Store::weight怎麽用?PHP J2Store::weight使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類J2Store
的用法示例。
在下文中一共展示了J2Store::weight方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getRates
/**
* Method to get rates from the USPS shipping API
*
* @param array $address
* @return array rates array
*/
private function getRates($address, $order)
{
$rates = array();
$status = true;
$shipping_status = false;
//first check if shippable items are in cart
$products = $order->getItems();
if ($order->isShippingEnabled() === false) {
return $rates;
}
$currencyObject = J2Store::currency();
$store_address = J2Store::config();
$domestic_services = $this->params->get('domestic_services');
$inernational_services = $this->params->get('international_services');
$quote_data = array();
$method_data = array();
$cart_weight_total = $order->getTotalShippingWeight();
$weightObject = J2Store::weight();
$weight = $weightObject->convert($cart_weight_total, $store_address->get('config_weight_class_id'), $this->params->get('usps_weight_class_id'));
$weight = $weight < 0.1 ? 0.1 : $weight;
$pounds = floor($weight);
$ounces = round(16 * ($weight - $pounds), 2);
// max 5 digits
$postcode = str_replace(' ', '', $address->postal_code);
//get country data
if ($address->country_isocode_2 == 'US') {
$xml = '<RateV4Request USERID="' . $this->usps_username . '">';
$xml .= ' <Package ID="1">';
$xml .= ' <Service>ALL</Service>';
$xml .= ' <ZipOrigination>' . substr($this->params->get('usps_postcode'), 0, 5) . '</ZipOrigination>';
$xml .= ' <ZipDestination>' . substr($postcode, 0, 5) . '</ZipDestination>';
$xml .= ' <Pounds>' . $pounds . '</Pounds>';
$xml .= ' <Ounces>' . $ounces . '</Ounces>';
// Prevent common size mismatch error from USPS (Size cannot be Regular if Container is Rectangular for some reason)
if ($this->params->get('usps_container') == 'RECTANGULAR' && $this->params->get('usps_size') == 'REGULAR') {
$this->params->set('usps_container', 'VARIABLE');
}
$xml .= ' <Container>' . $this->params->get('usps_container') . '</Container>';
$xml .= ' <Size>' . $this->params->get('usps_size') . '</Size>';
$xml .= ' <Width>' . $this->params->get('usps_width') . '</Width>';
$xml .= ' <Length>' . $this->params->get('usps_length') . '</Length>';
$xml .= ' <Height>' . $this->params->get('usps_height') . '</Height>';
// Calculate girth based on usps calculation
$xml .= ' <Girth>' . round((double) $this->params->get('usps_length') + (double) $this->params->get('usps_width') * 2 + (double) $this->params->get('usps_height') * 2, 1) . '</Girth>';
$xml .= ' <Machinable>' . ($this->params->get('usps_machinable') ? 'true' : 'false') . '</Machinable>';
$xml .= ' </Package>';
$xml .= '</RateV4Request>';
$request = 'API=RateV4&XML=' . urlencode($xml);
} else {
$countries = $this->getCountries();
if (isset($countries[$address->country_isocode_2])) {
$xml = '<IntlRateV2Request USERID="' . $this->usps_username . '">';
$xml .= ' <Package ID="1">';
$xml .= ' <Pounds>' . $pounds . '</Pounds>';
$xml .= ' <Ounces>' . $ounces . '</Ounces>';
$xml .= ' <MailType>All</MailType>';
$xml .= ' <GXG>';
$xml .= ' <POBoxFlag>N</POBoxFlag>';
$xml .= ' <GiftFlag>N</GiftFlag>';
$xml .= ' </GXG>';
$xml .= ' <ValueOfContents>' . $order->order_subtotal . '</ValueOfContents>';
$xml .= ' <Country>' . $countries[$address->country_isocode_2] . '</Country>';
// Intl only supports RECT and NONRECT
if ($this->params->get('usps_container') == 'VARIABLE') {
$this->params->set('usps_container', 'NONRECTANGULAR');
}
$xml .= ' <Container>' . $this->params->get('usps_container') . '</Container>';
$xml .= ' <Size>' . $this->params->get('usps_size') . '</Size>';
$xml .= ' <Width>' . $this->params->get('usps_width') . '</Width>';
$xml .= ' <Length>' . $this->params->get('usps_length') . '</Length>';
$xml .= ' <Height>' . $this->params->get('usps_height') . '</Height>';
$xml .= ' <Girth>' . $this->params->get('usps_girth') . '</Girth>';
$xml .= ' <CommercialFlag>N</CommercialFlag>';
$xml .= ' </Package>';
$xml .= '</IntlRateV2Request>';
$request = 'API=IntlRateV2&XML=' . urlencode($xml);
} else {
$status = false;
}
}
if ($status) {
$result = $this->_sendRequest($request);
$handling = $this->params->get('handling', '0');
if ($result) {
if ($this->params->get('show_debug')) {
$this->_log("USPS DATA SENT: " . urldecode($request));
$this->_log("USPS DATA RECV: " . $result);
}
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXml($result);
$rate_response = $dom->getElementsByTagName('RateV4Response')->item(0);
$intl_rate_response = $dom->getElementsByTagName('IntlRateV2Response')->item(0);
$error = $dom->getElementsByTagName('Error')->item(0);
$firstclasses = array('First-Class Mail Parcel', 'First-Class Mail Large Envelope', 'First-Class Mail Letter', 'First-Class Mail Postcards');
//.........這裏部分代碼省略.........