WC_Correios_Webservice_International

Correios Webservice International integration class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-correios-webservice-international.php  
  1. class WC_Correios_Webservice_International { 
  2.  
  3. /** 
  4. * Webservice URL. 
  5. * @var string 
  6. */ 
  7. private $_webservice = 'http://www2.correios.com.br/sistemas/efi/bb/Consulta.cfm?'; 
  8.  
  9. /** 
  10. * Shipping method ID. 
  11. * @var string 
  12. */ 
  13. protected $id = ''; 
  14.  
  15. /** 
  16. * Shipping zone instance ID. 
  17. * @var int 
  18. */ 
  19. protected $instance_id = 0; 
  20.  
  21. /** 
  22. * IDs from Correios services. 
  23. * 110 - Mercadoria Expressa (EMS). 
  24. * 128 - Mercadoria Econmica. 
  25. * 209 - Leve Internacional. 
  26. * @var string 
  27. */ 
  28. protected $service = ''; 
  29.  
  30. /** 
  31. * WooCommerce package containing the products. 
  32. * @var array 
  33. */ 
  34. protected $package = null; 
  35.  
  36. /** 
  37. * Destination country. 
  38. * @var string 
  39. */ 
  40. protected $destination_country = ''; 
  41.  
  42. /** 
  43. * Origin location. 
  44. * @var string 
  45. */ 
  46. protected $origin_location = ''; 
  47.  
  48. /** 
  49. * Origin state. 
  50. * @var string 
  51. */ 
  52. protected $origin_state = ''; 
  53.  
  54. /** 
  55. * Package height. 
  56. * @var float 
  57. */ 
  58. protected $height = 0; 
  59.  
  60. /** 
  61. * Package width. 
  62. * @var float 
  63. */ 
  64. protected $width = 0; 
  65.  
  66. /** 
  67. * Package length. 
  68. * @var float 
  69. */ 
  70. protected $length = 0; 
  71.  
  72. /** 
  73. * Package weight. 
  74. * @var float 
  75. */ 
  76. protected $weight = 0; 
  77.  
  78. /** 
  79. * Declared value. 
  80. * @var string 
  81. */ 
  82. protected $declared_value = '0'; 
  83.  
  84. /** 
  85. * Debug mode. 
  86. * @var string 
  87. */ 
  88. protected $debug = 'no'; 
  89.  
  90. /** 
  91. * Initialize webservice. 
  92. * @param string $id Method ID. 
  93. * @param int $instance_id Instance ID. 
  94. */ 
  95. public function __construct( $id = 'correios', $instance_id = 0 ) { 
  96. $this->id = $id; 
  97. $this->instance_id = $instance_id; 
  98. $this->log = new WC_Logger(); 
  99.  
  100. /** 
  101. * Set the service. 
  102. * @param string $service Correios international service. 
  103. */ 
  104. public function set_service( $service = '' ) { 
  105. $this->service = $service; 
  106.  
  107. /** 
  108. * Set shipping package. 
  109. * @param array $package Shipping package. 
  110. */ 
  111. public function set_package( $package = array() ) { 
  112. $this->package = $package; 
  113. $correios_package = new WC_Correios_Package( $package ); 
  114.  
  115. if ( ! is_null( $correios_package ) ) { 
  116. $data = $correios_package->get_data(); 
  117.  
  118. $this->set_height( wc_get_dimension( $data['height'], 'mm', 'cm' ) ); 
  119. $this->set_width( wc_get_dimension( $data['width'], 'mm', 'cm' ) ); 
  120. $this->set_length( wc_get_dimension( $data['length'], 'mm', 'cm' ) ); 
  121. $this->set_weight( wc_get_weight( $data['weight'], 'g', 'kg' ) ); 
  122.  
  123. if ( 'yes' === $this->debug ) { 
  124. if ( ! empty( $data ) ) { 
  125. $data = array( 
  126. 'weight' => $this->get_weight(),  
  127. 'height' => $this->get_height(),  
  128. 'width' => $this->get_width(),  
  129. 'length' => $this->get_length(),  
  130. ); 
  131.  
  132. $this->log->add( $this->id, 'Weight and cubage of the order: ' . print_r( $data, true ) ); 
  133.  
  134. /** 
  135. * Set destination country. 
  136. * @param string $country Destination country. 
  137. */ 
  138. public function set_destination_country( $country = '' ) { 
  139. $this->destination_country = $country; 
  140.  
  141. /** 
  142. * Set origin location. 
  143. * @param string $location Origin location. 
  144. */ 
  145. public function set_origin_location( $location = '' ) { 
  146. $this->origin_location = $location; 
  147.  
  148. /** 
  149. * Set origin state. 
  150. * @param string $state Origin state. 
  151. */ 
  152. public function set_origin_state( $state = '' ) { 
  153. $this->origin_state = $state; 
  154.  
  155. /** 
  156. * Set shipping package height. 
  157. * @param float $height Shipping package height. 
  158. */ 
  159. public function set_height( $height = 0 ) { 
  160. $this->height = (float) $height; 
  161.  
  162. /** 
  163. * Set shipping package width. 
  164. * @param float $width Shipping package width. 
  165. */ 
  166. public function set_width( $width = 0 ) { 
  167. $this->width = (float) $width; 
  168.  
  169. /** 
  170. * Set shipping package length. 
  171. * @param float $length Shipping package length. 
  172. */ 
  173. public function set_length( $length = 0 ) { 
  174. $this->length = (float) $length; 
  175.  
  176. /** 
  177. * Set shipping package weight. 
  178. * @param float $weight Shipping package weight. 
  179. */ 
  180. public function set_weight( $weight = 0 ) { 
  181. $this->weight = (float) $weight; 
  182.  
  183. /** 
  184. * Set the debug mode. 
  185. * @param string $debug Yes or no. 
  186. */ 
  187. public function set_debug( $debug = 'no' ) { 
  188. $this->debug = $debug; 
  189.  
  190. /** 
  191. * Get webservice URL. 
  192. * @return string 
  193. */ 
  194. public function get_webservice_url() { 
  195. return apply_filters( 'woocommerce_correios_webservice_international_url', $this->_webservice, $this->id, $this->instance_id, $this->package ); 
  196.  
  197. /** 
  198. * Get allowed countries. 
  199. * @return string 
  200. */ 
  201. public function get_allowed_countries() { 
  202. return apply_filters( 'woocommerce_correios_international_allowed_countries', array( 
  203. 'AD',  
  204. 'AE',  
  205. 'AF',  
  206. 'AG',  
  207. 'AI',  
  208. 'AL',  
  209. 'AM',  
  210. 'AN',  
  211. 'AO',  
  212. 'AR',  
  213. 'AS',  
  214. 'AT',  
  215. 'AU',  
  216. 'AW',  
  217. 'AZ',  
  218. 'BA',  
  219. 'BB',  
  220. 'BD',  
  221. 'BE',  
  222. 'BF',  
  223. 'BG',  
  224. 'BH',  
  225. 'BI',  
  226. 'BJ',  
  227. 'BM',  
  228. 'BN',  
  229. 'BO',  
  230. 'BS',  
  231. 'BT',  
  232. 'BW',  
  233. 'BY',  
  234. 'BZ',  
  235. 'CA',  
  236. 'CC',  
  237. 'CD',  
  238. 'CF',  
  239. 'CG',  
  240. 'CH',  
  241. 'CI',  
  242. 'CK',  
  243. 'CL',  
  244. 'CM',  
  245. 'CN',  
  246. 'CO',  
  247. 'CR',  
  248. 'CU',  
  249. 'CV',  
  250. 'CX',  
  251. 'CY',  
  252. 'CZ',  
  253. 'DE',  
  254. 'DJ',  
  255. 'DK',  
  256. 'DM',  
  257. 'DO',  
  258. 'DZ',  
  259. 'EC',  
  260. 'EE',  
  261. 'EG',  
  262. 'EH',  
  263. 'ER',  
  264. 'ES',  
  265. 'ET',  
  266. 'FI',  
  267. 'FJ',  
  268. 'FK',  
  269. 'FM',  
  270. 'FO',  
  271. 'FR',  
  272. 'GA',  
  273. 'GB',  
  274. 'GD',  
  275. 'GE',  
  276. 'GF',  
  277. 'GG',  
  278. 'GH',  
  279. 'GI',  
  280. 'GL',  
  281. 'GM',  
  282. 'GN',  
  283. 'GP',  
  284. 'GQ',  
  285. 'GR',  
  286. 'GS',  
  287. 'GT',  
  288. 'GU',  
  289. 'GW',  
  290. 'GY',  
  291. 'HK',  
  292. 'HN',  
  293. 'HR',  
  294. 'HT',  
  295. 'HU',  
  296. 'ID',  
  297. 'IE',  
  298. 'IL',  
  299. 'IM',  
  300. 'IN',  
  301. 'IQ',  
  302. 'IR',  
  303. 'IS',  
  304. 'IT',  
  305. 'JE',  
  306. 'JM',  
  307. 'JO',  
  308. 'JP',  
  309. 'KE',  
  310. 'KG',  
  311. 'KH',  
  312. 'KI',  
  313. 'KM',  
  314. 'KN',  
  315. 'KP',  
  316. 'KR',  
  317. 'KW',  
  318. 'KY',  
  319. 'KZ',  
  320. 'LA',  
  321. 'LB',  
  322. 'LC',  
  323. 'LI',  
  324. 'LK',  
  325. 'LR',  
  326. 'LS',  
  327. 'LT',  
  328. 'LU',  
  329. 'LV',  
  330. 'LY',  
  331. 'MA',  
  332. 'MC',  
  333. 'MD',  
  334. 'ME',  
  335. 'MG',  
  336. 'MH',  
  337. 'MK',  
  338. 'ML',  
  339. 'MM',  
  340. 'MN',  
  341. 'MO',  
  342. 'MP',  
  343. 'MQ',  
  344. 'MR',  
  345. 'MS',  
  346. 'MT',  
  347. 'MU',  
  348. 'MV',  
  349. 'MW',  
  350. 'MX',  
  351. 'MY',  
  352. 'MZ',  
  353. 'NA',  
  354. 'NC',  
  355. 'NE',  
  356. 'NF',  
  357. 'NG',  
  358. 'NI',  
  359. 'NL',  
  360. 'NO',  
  361. 'NP',  
  362. 'NR',  
  363. 'NU',  
  364. 'NZ',  
  365. 'OM',  
  366. 'PA',  
  367. 'PE',  
  368. 'PF',  
  369. 'PG',  
  370. 'PH',  
  371. 'PK',  
  372. 'PL',  
  373. 'PM',  
  374. 'PN',  
  375. 'PR',  
  376. 'PS',  
  377. 'PT',  
  378. 'PW',  
  379. 'PY',  
  380. 'QA',  
  381. 'RE',  
  382. 'RO',  
  383. 'RS',  
  384. 'RU',  
  385. 'RW',  
  386. 'SA',  
  387. 'SB',  
  388. 'SC',  
  389. 'SD',  
  390. 'SE',  
  391. 'SG',  
  392. 'SH',  
  393. 'SI',  
  394. 'SK',  
  395. 'SL',  
  396. 'SM',  
  397. 'SN',  
  398. 'SO',  
  399. 'SR',  
  400. 'ST',  
  401. 'SV',  
  402. 'SY',  
  403. 'SZ',  
  404. 'TC',  
  405. 'TD',  
  406. 'TF',  
  407. 'TG',  
  408. 'TH',  
  409. 'TJ',  
  410. 'TK',  
  411. 'TM',  
  412. 'TN',  
  413. 'TO',  
  414. 'TP',  
  415. 'TR',  
  416. 'TT',  
  417. 'TV',  
  418. 'TW',  
  419. 'TZ',  
  420. 'UA',  
  421. 'UG',  
  422. 'US',  
  423. 'UY',  
  424. 'UZ',  
  425. 'VA',  
  426. 'VC',  
  427. 'VE',  
  428. 'VG',  
  429. 'VI',  
  430. 'VN',  
  431. 'VU',  
  432. 'WF',  
  433. 'WS',  
  434. 'XA',  
  435. 'XB',  
  436. 'XC',  
  437. 'XD',  
  438. 'XE',  
  439. 'XF',  
  440. 'XG',  
  441. 'YE',  
  442. 'YT',  
  443. 'ZA',  
  444. 'ZM',  
  445. 'ZW',  
  446. ) ); 
  447.  
  448. /** 
  449. * Get destination country. 
  450. * @return string 
  451. */ 
  452. public function get_destination_country() { 
  453. return $this->destination_country; 
  454.  
  455. /** 
  456. * Get origin location. 
  457. * @return string 
  458. */ 
  459. public function get_origin_location() { 
  460. $location = 'C' === $this->origin_location ? 'C' : 'I'; 
  461.  
  462. return apply_filters( 'woocommerce_correios_international_origin_location', $this->origin_location, $this->id, $this->instance_id, $this->package ); 
  463.  
  464. /** 
  465. * Get origin state. 
  466. * @return string 
  467. */ 
  468. public function get_origin_state() { 
  469. return apply_filters( 'woocommerce_correios_international_origin_state', $this->origin_state, $this->id, $this->instance_id, $this->package ); 
  470.  
  471. /** 
  472. * Get height. 
  473. * @return float 
  474. */ 
  475. public function get_height() { 
  476. return $this->float_to_string( $this->height ); 
  477.  
  478. /** 
  479. * Get width. 
  480. * @return float 
  481. */ 
  482. public function get_width() { 
  483. return $this->float_to_string( $this->width ); 
  484.  
  485. /** 
  486. * Get length. 
  487. * @return float 
  488. */ 
  489. public function get_length() { 
  490. return $this->float_to_string( $this->length ); 
  491.  
  492. /** 
  493. * Get weight. 
  494. * @return float 
  495. */ 
  496. public function get_weight() { 
  497. return $this->float_to_string( $this->weight ); 
  498.  
  499. /** 
  500. * Fix number format for XML. 
  501. * @param float $value Value with dot. 
  502. * @return string Value with comma. 
  503. */ 
  504. protected function float_to_string( $value ) { 
  505. $value = str_replace( '.', ', ', $value ); 
  506.  
  507. return $value; 
  508.  
  509. /** 
  510. * Check if is setted. 
  511. * @return bool 
  512. */ 
  513. protected function is_setted() { 
  514. $state = $this->get_origin_state(); 
  515.  
  516. return ! empty( $this->service ) || ! empty( $this->destination_country ) || ! in_array( $this->destination_country, $this->get_allowed_countries() ) || ! empty( $state ) || 0 === $this->get_height(); 
  517.  
  518. /** 
  519. * Get shipping prices. 
  520. * @return SimpleXMLElement 
  521. */ 
  522. public function get_shipping() { 
  523. $shipping = null; 
  524.  
  525. // Checks if services and postcode is empty. 
  526. if ( ! $this->is_setted() ) { 
  527. return $values; 
  528.  
  529. $args = apply_filters( 'woocommerce_correios_international_shipping_args', array( 
  530. 'tipoConsulta' => 'Geral',  
  531. 'especif' => $this->service,  
  532. 'uforigem' => $this->get_origin_state(),  
  533. 'localidade' => $this->get_origin_location(),  
  534. 'pais' => $this->get_destination_country(),  
  535. 'altura' => $this->get_height(),  
  536. 'largura' => $this->get_width(),  
  537. 'profundidade' => $this->get_length(),  
  538. 'peso' => $this->get_weight(),  
  539. 'reset' => 'true',  
  540. ), $this->id, $this->package ); 
  541.  
  542. $url = add_query_arg( $args, $this->get_webservice_url() ); 
  543.  
  544. if ( 'yes' === $this->debug ) { 
  545. $this->log->add( $this->id, 'Requesting Correios WebServices: ' . $url ); 
  546.  
  547. // Gets the WebServices response. 
  548. $response = wp_safe_remote_get( $url, array( 'timeout' => 30 ) ); 
  549.  
  550. if ( is_wp_error( $response ) ) { 
  551. if ( 'yes' === $this->debug ) { 
  552. $this->log->add( $this->id, 'WP_Error: ' . $response->get_error_message() ); 
  553. } elseif ( $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) { 
  554. try { 
  555. $result = wc_correios_safe_load_xml( $response['body'], LIBXML_NOCDATA ); 
  556. } catch ( Exception $e ) { 
  557. if ( 'yes' === $this->debug ) { 
  558. $this->log->add( $this->id, 'Correios WebServices invalid XML: ' . $e->getMessage() ); 
  559.  
  560. if ( isset( $result->tipo_servico ) ) { 
  561. if ( 'yes' === $this->debug ) { 
  562. $this->log->add( $this->id, 'Correios WebServices response: ' . print_r( $result, true ) ); 
  563.  
  564. $shipping = $result->tipo_servico; 
  565. } else { 
  566. if ( 'yes' === $this->debug ) { 
  567. $this->log->add( $this->id, 'Error accessing the Correios WebServices: ' . print_r( $response, true ) ); 
  568.  
  569. return $shipping;