WC_Correios_Tracking_History

Correios tracking history class.

Defined (1)

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

/includes/class-wc-correios-tracking-history.php  
  1. class WC_Correios_Tracking_History { 
  2.  
  3. /** 
  4. * Tracking webservice URL. 
  5. * @var string 
  6. */ 
  7. private $_webservice_url = 'https://webservice.correios.com.br/service/rastro/Rastro.wsdl'; 
  8.  
  9. /** 
  10. * Initialize actions. 
  11. */ 
  12. public function __construct() { 
  13. add_action( 'woocommerce_order_details_after_order_table', array( $this, 'view' ), 1 ); 
  14.  
  15. /** 
  16. * Get the tracking history webservice URL. 
  17. * @return string 
  18. */ 
  19. protected function get_tracking_history_webservice_url() { 
  20. return apply_filters( 'woocommerce_correios_tracking_webservice_url', $this->_webservice_url ); 
  21.  
  22. /** 
  23. * Get user data. 
  24. * @return array 
  25. */ 
  26. protected function get_user_data() { 
  27. $user_data = apply_filters( 'woocommerce_correios_tracking_user_data', array( 'login' => 'ECT', 'password' => 'SRO' ) ); 
  28.  
  29. return $user_data; 
  30.  
  31. /** 
  32. * Logger. 
  33. * @param string $data Data to log. 
  34. */ 
  35. protected function logger( $data ) { 
  36. if ( apply_filters( 'woocommerce_correios_enable_tracking_debug', false ) ) { 
  37. $logger = new WC_Logger(); 
  38. $logger->add( 'correios-tracking-history', $data ); 
  39.  
  40. /** 
  41. * Access API Correios. 
  42. * @param array $tracking_code Tracking code. 
  43. * @return array 
  44. */ 
  45. protected function get_tracking_history( $tracking_codes ) { 
  46. include_once dirname( __FILE__ ) . '/class-wc-correios-soap-client.php'; 
  47.  
  48. $this->logger( sprintf( 'Fetching tracking history for "%s" on Correios Webservices...', implode( ', ', $tracking_codes ) ) ); 
  49.  
  50. $objects = null; 
  51. $user_data = $this->get_user_data(); 
  52. $args = apply_filters( 'woocommerce_correios_tracking_history_webservice_args', array( 
  53. 'usuario' => $user_data['login'],  
  54. 'senha' => $user_data['password'],  
  55. 'tipo' => 'L', // L - List of objects, F - Object Range. 
  56. 'resultado' => 'T', // T - Returns all the object's events, U - Returns only last event object. 
  57. 'lingua' => '101',  
  58. 'objetos' => implode( '', $tracking_codes ),  
  59. ) ); 
  60.  
  61. try { 
  62. $soap = new WC_Correios_Soap_Client( $this->get_tracking_history_webservice_url() ); 
  63. $response = $soap->buscaEventos( $args ); 
  64.  
  65. // Handle Correios multiple formats response. 
  66. if ( ! empty( $response->return->objeto ) ) { 
  67. // Handle multiple objects. 
  68. if ( is_array( $response->return->objeto ) ) { 
  69. $objects = (array) $response->return->objeto; 
  70.  
  71. // Fix when return only last event for each object. 
  72. if ( is_object( $objects[0]->evento ) ) { 
  73. $new_objects = array(); 
  74. foreach ( $objects as $key => $object ) { 
  75. $new_objects[ $key ] = $object; 
  76. $new_objects[ $key ]->evento = array( $new_objects[ $key ]->evento ); 
  77. $objects = $new_objects; 
  78.  
  79. // Handle single object. 
  80. } elseif ( is_object( $response->return->objeto ) ) { 
  81. $objects = array( $response->return->objeto ); 
  82.  
  83. // Fix when return only last event. 
  84. if ( is_object( $objects[0]->evento ) ) { 
  85. $objects[0]->evento = array( $objects[0]->evento ); 
  86. } catch ( Exception $e ) { 
  87. $this->logger( sprintf( 'An error occurred while trying to fetch the tracking history for "%s": %s', implode( ', ', $tracking_codes ), $e->getMessage() ) ); 
  88.  
  89. if ( ! is_null( $objects ) ) { 
  90. $this->logger( sprintf( 'Tracking history found successfully: %s', print_r( $objects, true ) ) ); 
  91.  
  92. return apply_filters( 'woocommerce_correios_tracking_objects', $objects, $tracking_codes ); 
  93.  
  94. /** 
  95. * Display the order tracking code in order details and the tracking history. 
  96. * @param WC_Order $order Order data. 
  97. */ 
  98. public function view( $order ) { 
  99. $objects = array(); 
  100.  
  101. $tracking_codes = wc_correios_get_tracking_codes( $order ); 
  102.  
  103. // Check if exist a tracking code for the order. 
  104. if ( empty( $tracking_codes ) ) { 
  105. return; 
  106.  
  107. // Try to connect to Correios Webservices and get the tracking history. 
  108. if ( apply_filters( 'woocommerce_correios_enable_tracking_history', false ) ) { 
  109. $objects = $this->get_tracking_history( $tracking_codes ); 
  110.  
  111. wc_get_template( 
  112. 'myaccount/tracking-title.php',  
  113. array(),  
  114. '',  
  115. WC_Correios::get_templates_path() 
  116. ); 
  117.  
  118. // Display the right template for show the tracking code or tracking history. 
  119. if ( ! empty( $objects ) ) { 
  120. foreach ( $objects as $object ) { 
  121. wc_get_template( 
  122. 'myaccount/tracking-history-table.php',  
  123. array( 
  124. 'events' => (array) $object->evento,  
  125. 'code' => (string) $object->numero,  
  126. ),  
  127. '',  
  128. WC_Correios::get_templates_path() 
  129. ); 
  130. } else { 
  131. wc_get_template( 
  132. 'myaccount/tracking-codes.php',  
  133. array( 
  134. 'codes' => $tracking_codes,  
  135. ),  
  136. '',  
  137. WC_Correios::get_templates_path() 
  138. );