id = 'az_ups'; $this->method_title = __( 'Configure UPS' ); // Title shown in admin $this->title = __( 'ups UPS' ); $this->method_description = __( 'Real Time Rates, Premium Supports Shipping Label.' ); // $this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled $this->init(); } /** * Init your settings * * @access public * @return void */ function init() { // Load the settings API $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings $this->init_settings(); // This is part of the settings API. Loads settings you previously init. // Save settings in admin if you have any defined add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); } /** * calculate_shipping function. * * @access public * @param mixed $package * @return void */ public function calculate_shipping( $package = array() ) { $pack_aft_hook = apply_filters('a2z_ups_rate_packages', $package); $general_settings = get_option('a2z_ups_main_settings'); $general_settings = empty($general_settings) ? array() : $general_settings; if(isset($general_settings['a2z_ups_rates']) && $general_settings['a2z_ups_rates'] == 'yes' && isset($pack_aft_hook['destination']['country']) && !empty($pack_aft_hook['destination']['country'])) { $des_coun = $pack_aft_hook['destination']['country']; if ( ( "US" == $des_coun ) && ( "PR" == $des_coun ) ) { $destination_country = "PR"; } $weight_unit = $dim_unit = ''; if(!empty($general_settings['a2z_ups_weight_unit']) && $general_settings['a2z_ups_weight_unit'] == 'KG_CM') { $weight_unit = 'KGS'; } else { $weight_unit = 'LBS'; } $total_weight_for_request = 0; if ($pack_aft_hook) { foreach ($pack_aft_hook['contents'] as $parcel) { $quantity = $parcel['quantity']; for($i=0;$i<$quantity; $i++) { $product = $parcel['data']->get_data(); $total_weight =(string) $product['weight']; $total_weight = str_replace(',','.',$total_weight); if($total_weight<0.001){ $total_weight = 0.001; }else{ $total_weight = round((float)$total_weight,3); } $total_weight_for_request += $total_weight; } } } $xmlRequest = file_get_contents(dirname(__FILE__).'/xml/rate.xml'); $xmlRequest = str_replace('{ac_hit}',$general_settings['a2z_ups_access_key'],$xmlRequest); $xmlRequest = str_replace('{usr}',$general_settings['a2z_ups_site_id'],$xmlRequest); $xmlRequest = str_replace('{pwd}',$general_settings['a2z_ups_site_pwd'],$xmlRequest); $xmlRequest = str_replace('{from_name}',$general_settings['a2z_ups_shipper_name'],$xmlRequest); $xmlRequest = str_replace('{from_acc}',$general_settings['a2z_ups_acc_no'],$xmlRequest); $xmlRequest = str_replace('{from_city}',$general_settings['a2z_ups_city'],$xmlRequest); $xmlRequest = str_replace('{from_state}',$general_settings['a2z_ups_state'],$xmlRequest); $xmlRequest = str_replace('{from_country}',$general_settings['a2z_ups_country'],$xmlRequest); $xmlRequest = str_replace('{from_postal}',$general_settings['a2z_ups_zip'],$xmlRequest); $xmlRequest = str_replace('{to_city}',$pack_aft_hook['destination']['city'],$xmlRequest); $xmlRequest = str_replace('{to_country}',$des_coun,$xmlRequest); $xmlRequest = str_replace('{to_postal}',$pack_aft_hook['destination']['postcode'],$xmlRequest); $xmlRequest = str_replace('{weight_code}',$weight_unit,$xmlRequest); $xmlRequest = str_replace('{weight}',$total_weight_for_request,$xmlRequest); $request_url = (isset($general_settings['a2z_ups_test']) && $general_settings['a2z_ups_test'] != 'yes') ? 'https://wwwcie.ups.com/ups.app/xml/Rate' : 'https://onlinetools.ups.com/ups.app/xml/Rate'; $result = wp_remote_post($request_url, array( 'method' => 'POST', 'timeout' => 70, 'sslverify' => 0, 'body' => $xmlRequest ) ); libxml_use_internal_errors(true); if(!empty($result)) { $xml = simplexml_load_string(utf8_encode($result['body'])); } if(isset($general_settings['a2z_ups_developer_rate']) && $general_settings['a2z_ups_developer_rate'] == 'yes') { echo "
"; echo "Request
"; print_r(htmlspecialchars($xmlRequest)); echo "Response
"; print_r($xml); die(); } if(isset($xml->Response->ResponseStatusCode) && $xml->Response->ResponseStatusCode != '1') { return false; } $_carriers = array( //"Public carrier name" => "technical name", 'ups_12' => '3 Day Select', 'ups_03' => 'Ground', 'ups_02' => '2nd Day Air', 'ups_59' => '2nd Day Air AM', 'ups_01' => 'Next Day Air', 'ups_13' => 'Next Day Air Saver', 'ups_14' => 'Next Day Air Early AM', 'ups_11' => 'UPS Standard', 'ups_07' => 'UPS Express', 'ups_08' => 'UPS Expedited', 'ups_54' => 'UPS Express Plus', 'ups_65' => 'UPS Saver', 'ups_92' => 'SurePost Less than 1 lb', 'ups_93' => 'SurePost 1 lb or Greater', 'ups_94' => 'SurePost BPM', 'ups_95' => 'SurePost Media', 'ups_08' => 'UPS ExpeditedSM', 'ups_82' => 'Today Standard', "ups_83" => "UPS Today Dedicated Courier", "ups_84" => "UPS Today Intercity", "ups_85" => "UPS Today Express", "ups_86" => "UPS Today Express Saver", 'ups_M2' => 'First Class Mail', 'ups_M3' => 'Priority Mail', 'ups_M4' => 'Expedited Mail Innovations', 'ups_M5' => 'Priority Mail Innovations', 'ups_M6' => 'EconomyMail Innovations', 'ups_70' => 'Access Point Economy', 'ups_96' => 'Worldwide Express Freight' ); $carriers_available = isset($general_settings['a2z_ups_carrier']) && is_array($general_settings['a2z_ups_carrier']) ? $general_settings['a2z_ups_carrier'] : array(); foreach($xml->RatedShipment as $indshipment) { $rate_code = 'ups_'.(string)$indshipment->Service->Code; $rate_cost = 0; if(array_key_exists($rate_code,$carriers_available)) { if ( $general_settings['a2z_ups_account_rates'] == 'yes' && isset( $indshipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue ) ) { $isoUPSCurrency = (string) $indshipment->NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode; $rate_cost = (float) $indshipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue; } else { $isoUPSCurrency = (string) $indshipment->TotalCharges->CurrencyCode; $rate_cost = (float) $indshipment->TotalCharges->MonetaryValue; } } else{ continue; } $rate_cost = apply_filters('a2z_ups_rate_cost',$rate_cost,$rate_code); if($rate_cost > 0) { $rate_name = $_carriers[$rate_code]; $name = isset($carriers_name_available[$rate_code]) && !empty($carriers_name_available[$rate_code]) ? $carriers_name_available[$rate_code] : $rate_name; $rate = array( 'id' => 'a2z'.$rate_code, 'label' => $name, 'cost' => $rate_cost, 'meta_data' => ''); $this->add_rate( $rate ); } } } } private function a2z_get_zipcode_or_city($country, $city, $postcode) { $no_postcode_country = array('AE', 'AF', 'AG', 'AI', 'AL', 'AN', 'AO', 'AW', 'BB', 'BF', 'BH', 'BI', 'BJ', 'BM', 'BO', 'BS', 'BT', 'BW', 'BZ', 'CD', 'CF', 'CG', 'CI', 'CK', 'CL', 'CM', 'CO', 'CR', 'CV', 'DJ', 'DM', 'DO', 'EC', 'EG', 'ER', 'ET', 'FJ', 'FK', 'GA', 'GD', 'GH', 'GI', 'GM', 'GN', 'GQ', 'GT', 'GW', 'GY', 'HK', 'HN', 'HT', 'IE', 'IQ', 'IR', 'JM', 'JO', 'KE', 'KH', 'KI', 'KM', 'KN', 'KP', 'KW', 'KY', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LY', 'ML', 'MM', 'MO', 'MR', 'MS', 'MT', 'MU', 'MW', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NP', 'NR', 'NU', 'OM', 'PA', 'PE', 'PF', 'PY', 'QA', 'RW', 'SA', 'SB', 'SC', 'SD', 'SL', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SY', 'TC', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'UY', 'VC', 'VE', 'VG', 'VN', 'VU', 'WS', 'XA', 'XB', 'XC', 'XE', 'XL', 'XM', 'XN', 'XS', 'YE', 'ZM', 'ZW'); $postcode_city = !in_array( $country, $no_postcode_country ) ? $postcode_city = "{$postcode} " : ''; if( !empty($city) ){ $postcode_city .= "{$city} "; } return $postcode_city; } /** * Initialise Gateway Settings Form Fields */ public function init_form_fields() { $this->form_fields = array('a2z_ups' => array('type'=>'a2z_ups')); } public function generate_a2z_ups_html() { include( 'views/a2z_ups_settings_view.php' ); } } }