/includes/class-wc-correios-tracking-history.php

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