WC_Correios_Shipping_Legacy

Legacy Correios shipping method class.

Defined (1)

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

/includes/shipping/class-wc-correios-shipping-legacy.php  
  1. class WC_Correios_Shipping_Legacy extends WC_Shipping_Method { 
  2.  
  3. /** 
  4. * Initialize the Correios shipping method. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'correios'; 
  8. $this->method_title = __( 'Correios', 'woocommerce-correios' ); 
  9. $this->method_description = __( 'Correios is a brazilian delivery method.', 'woocommerce-correios' ); 
  10.  
  11. // Load the form fields. 
  12. $this->init_form_fields(); 
  13.  
  14. // Load the settings. 
  15. $this->init_settings(); 
  16.  
  17. // Define user set variables. 
  18. $this->enabled = $this->get_option( 'enabled' ); 
  19. $this->title = $this->get_option( 'title' ); 
  20. $this->declare_value = $this->get_option( 'declare_value' ); 
  21. $this->display_date = $this->get_option( 'display_date' ); 
  22. $this->additional_time = $this->get_option( 'additional_time' ); 
  23. $this->fee = $this->get_option( 'fee' ); 
  24. $this->zip_origin = $this->get_option( 'zip_origin' ); 
  25. $this->corporate_service = $this->get_option( 'corporate_service' ); 
  26. $this->login = $this->get_option( 'login' ); 
  27. $this->password = $this->get_option( 'password' ); 
  28. $this->service_pac = $this->get_option( 'service_pac' ); 
  29. $this->service_sedex = $this->get_option( 'service_sedex' ); 
  30. $this->service_sedex_10 = $this->get_option( 'service_sedex_10' ); 
  31. $this->service_sedex_hoje = $this->get_option( 'service_sedex_hoje' ); 
  32. $this->service_esedex = $this->get_option( 'service_esedex' ); 
  33. $this->minimum_height = $this->get_option( 'minimum_height' ); 
  34. $this->minimum_width = $this->get_option( 'minimum_width' ); 
  35. $this->minimum_length = $this->get_option( 'minimum_length' ); 
  36. $this->debug = $this->get_option( 'debug' ); 
  37.  
  38. // Method variables. 
  39. $this->availability = 'specific'; 
  40. $this->countries = array( 'BR' ); 
  41.  
  42. // Actions. 
  43. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  44.  
  45. // Active logs. 
  46. if ( 'yes' == $this->debug ) { 
  47. $this->log = new WC_Logger(); 
  48.  
  49. /** 
  50. * Get log. 
  51. * @return string 
  52. */ 
  53. protected function get_log_view() { 
  54. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.2', '>=' ) ) { 
  55. return '<a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs&log_file=' . esc_attr( $this->id ) . '-' . sanitize_file_name( wp_hash( $this->id ) ) . '.log' ) ) . '">' . __( 'System Status > Logs', 'woocommerce-correios' ) . '</a>'; 
  56.  
  57. return '<code>woocommerce/logs/' . esc_attr( $this->id ) . '-' . sanitize_file_name( wp_hash( $this->id ) ) . '.txt</code>'; 
  58.  
  59. /** 
  60. * Admin options fields. 
  61. */ 
  62. public function init_form_fields() { 
  63. $this->form_fields = array( 
  64. 'enabled' => array( 
  65. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  66. 'type' => 'checkbox',  
  67. 'label' => __( 'Enable this shipping method', 'woocommerce-correios' ),  
  68. 'default' => 'no',  
  69. ),  
  70. 'title' => array( 
  71. 'title' => __( 'Title', 'woocommerce-correios' ),  
  72. 'type' => 'text',  
  73. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-correios' ),  
  74. 'desc_tip' => true,  
  75. 'default' => __( 'Correios', 'woocommerce-correios' ),  
  76. ),  
  77. 'zip_origin' => array( 
  78. 'title' => __( 'Origin Zip Code', 'woocommerce-correios' ),  
  79. 'type' => 'text',  
  80. 'description' => __( 'Zip Code from where the requests are sent.', 'woocommerce-correios' ),  
  81. 'desc_tip' => true,  
  82. ),  
  83. 'declare_value' => array( 
  84. 'title' => __( 'Declare value', 'woocommerce-correios' ),  
  85. 'type' => 'select',  
  86. 'default' => 'none',  
  87. 'options' => array( 
  88. 'declare' => __( 'Declare', 'woocommerce-correios' ),  
  89. 'none' => __( 'No', 'woocommerce-correios' ),  
  90. ),  
  91. ),  
  92. 'display_date' => array( 
  93. 'title' => __( 'Estimated delivery', 'woocommerce-correios' ),  
  94. 'type' => 'checkbox',  
  95. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  96. 'description' => __( 'Display date of estimated delivery.', 'woocommerce-correios' ),  
  97. 'desc_tip' => true,  
  98. 'default' => 'no',  
  99. ),  
  100. 'additional_time' => array( 
  101. 'title' => __( 'Additional days', 'woocommerce-correios' ),  
  102. 'type' => 'text',  
  103. 'description' => __( 'Additional days to the estimated delivery.', 'woocommerce-correios' ),  
  104. 'desc_tip' => true,  
  105. 'default' => '0',  
  106. 'placeholder' => '0',  
  107. ),  
  108. 'fee' => array( 
  109. 'title' => __( 'Handling Fee', 'woocommerce-correios' ),  
  110. 'type' => 'text',  
  111. 'description' => __( 'Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce-correios' ),  
  112. 'desc_tip' => true,  
  113. 'placeholder' => '0.00',  
  114. ),  
  115. 'services' => array( 
  116. 'title' => __( 'Correios Services', 'woocommerce-correios' ),  
  117. 'type' => 'title',  
  118. ),  
  119. 'corporate_service' => array( 
  120. 'title' => __( 'Corporate Service', 'woocommerce-correios' ),  
  121. 'type' => 'select',  
  122. 'description' => __( 'Choose between conventional or corporate service.', 'woocommerce-correios' ),  
  123. 'desc_tip' => true,  
  124. 'default' => 'conventional',  
  125. 'options' => array( 
  126. 'conventional' => __( 'Conventional', 'woocommerce-correios' ),  
  127. 'corporate' => __( 'Corporate', 'woocommerce-correios' ),  
  128. ),  
  129. ),  
  130. 'login' => array( 
  131. 'title' => __( 'Administrative Code', 'woocommerce-correios' ),  
  132. 'type' => 'text',  
  133. 'description' => __( 'Your Correios login.', 'woocommerce-correios' ),  
  134. 'desc_tip' => true,  
  135. ),  
  136. 'password' => array( 
  137. 'title' => __( 'Administrative Password', 'woocommerce-correios' ),  
  138. 'type' => 'password',  
  139. 'description' => __( 'Your Correios password.', 'woocommerce-correios' ),  
  140. 'desc_tip' => true,  
  141. ),  
  142. 'service_pac' => array( 
  143. 'title' => __( 'PAC', 'woocommerce-correios' ),  
  144. 'type' => 'checkbox',  
  145. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  146. 'description' => __( 'Shipping via PAC.', 'woocommerce-correios' ),  
  147. 'desc_tip' => true,  
  148. 'default' => 'no',  
  149. ),  
  150. 'service_sedex' => array( 
  151. 'title' => __( 'SEDEX', 'woocommerce-correios' ),  
  152. 'type' => 'checkbox',  
  153. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  154. 'description' => __( 'Shipping via SEDEX.', 'woocommerce-correios' ),  
  155. 'desc_tip' => true,  
  156. 'default' => 'no',  
  157. ),  
  158. 'service_sedex_10' => array( 
  159. 'title' => __( 'SEDEX 10', 'woocommerce-correios' ),  
  160. 'type' => 'checkbox',  
  161. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  162. 'description' => __( 'Shipping via SEDEX 10.', 'woocommerce-correios' ),  
  163. 'desc_tip' => true,  
  164. 'default' => 'no',  
  165. ),  
  166. 'service_sedex_hoje' => array( 
  167. 'title' => __( 'SEDEX Hoje', 'woocommerce-correios' ),  
  168. 'type' => 'checkbox',  
  169. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  170. 'description' => __( 'Shipping via SEDEX Hoje.', 'woocommerce-correios' ),  
  171. 'desc_tip' => true,  
  172. 'default' => 'no',  
  173. ),  
  174. 'service_esedex' => array( 
  175. 'title' => __( 'e-SEDEX', 'woocommerce-correios' ),  
  176. 'type' => 'checkbox',  
  177. 'label' => __( 'Enable', 'woocommerce-correios' ),  
  178. 'description' => __( 'Shipping via e-SEDEX.', 'woocommerce-correios' ),  
  179. 'desc_tip' => true,  
  180. 'default' => 'no',  
  181. ),  
  182. 'package_standard' => array( 
  183. 'title' => __( 'Package Standard', 'woocommerce-correios' ),  
  184. 'type' => 'title',  
  185. 'description' => __( 'Sets a minimum measure for the package.', 'woocommerce-correios' ),  
  186. 'desc_tip' => true,  
  187. ),  
  188. 'minimum_height' => array( 
  189. 'title' => __( 'Minimum Height', 'woocommerce-correios' ),  
  190. 'type' => 'text',  
  191. 'description' => __( 'Minimum height of the package. Correios needs at least 2 cm.', 'woocommerce-correios' ),  
  192. 'desc_tip' => true,  
  193. 'default' => '2',  
  194. ),  
  195. 'minimum_width' => array( 
  196. 'title' => __( 'Minimum Width', 'woocommerce-correios' ),  
  197. 'type' => 'text',  
  198. 'description' => __( 'Minimum width of the package. Correios needs at least 11 cm.', 'woocommerce-correios' ),  
  199. 'desc_tip' => true,  
  200. 'default' => '11',  
  201. ),  
  202. 'minimum_length' => array( 
  203. 'title' => __( 'Minimum Length', 'woocommerce-correios' ),  
  204. 'type' => 'text',  
  205. 'description' => __( 'Minimum length of the package. Correios needs at least 16 cm.', 'woocommerce-correios' ),  
  206. 'desc_tip' => true,  
  207. 'default' => '16',  
  208. ),  
  209. 'testing' => array( 
  210. 'title' => __( 'Testing', 'woocommerce-correios' ),  
  211. 'type' => 'title',  
  212. ),  
  213. 'debug' => array( 
  214. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  215. 'type' => 'checkbox',  
  216. 'label' => __( 'Enable logging', 'woocommerce-correios' ),  
  217. 'default' => 'no',  
  218. 'description' => sprintf( __( 'Log Correios events, such as WebServices requests, inside %s.', 'woocommerce-correios' ), $this->get_log_view() ),  
  219. ),  
  220. ); 
  221.  
  222. /** 
  223. * Correios options page. 
  224. */ 
  225. public function admin_options() { 
  226. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  227.  
  228. // Call the admin scripts. 
  229. wp_enqueue_script( 'wc-correios', plugins_url( 'assets/js/admin/shipping-methods' . $suffix . '.js', WC_Correios::get_main_file() ), array( 'jquery' ), '', true ); 
  230.  
  231. echo '<h3>' . esc_html( $this->method_title ) . '</h3>'; 
  232.  
  233. echo '<p>' . esc_html( $this->method_description ) . '</p>'; 
  234.  
  235. if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '2.6.0', '>=' ) ) { 
  236. echo '<div class="error inline">'; 
  237. echo '<p><strong>' . sprintf( __( 'This shipping method this depreciated since %s, and you should configure shipping methods in the new Shipping Zones screen.', 'woocommerce-correios' ), 'WooCommerce 2.6' ) . ' <a href="https://youtu.be/3z9j0NPU_O0">' . __( 'See how to configure.', 'woocommerce-correios' ) . '</a></strong></p>'; 
  238. echo '</div>'; 
  239. } else { 
  240. include WC_Correios::get_plugin_path() . 'includes/admin/views/html-admin-help-message.php'; 
  241.  
  242. echo '<table class="form-table">'; 
  243. $this->generate_settings_html(); 
  244. echo '</table>'; 
  245.  
  246. /** 
  247. * Gets the services IDs. 
  248. * @return array 
  249. */ 
  250. protected function correios_services() { 
  251. $services = array(); 
  252.  
  253. $services['PAC'] = ( 'yes' == $this->service_pac ) ? '04510' : ''; 
  254. $services['SEDEX'] = ( 'yes' == $this->service_sedex ) ? '04014' : ''; 
  255. $services['SEDEX 10'] = ( 'yes' == $this->service_sedex_10 ) ? '40215' : ''; 
  256. $services['SEDEX Hoje'] = ( 'yes' == $this->service_sedex_hoje ) ? '40290' : ''; 
  257.  
  258. if ( 'corporate' == $this->corporate_service ) { 
  259. $services['PAC'] = ( 'yes' == $this->service_pac ) ? '04669' : ''; 
  260. $services['SEDEX'] = ( 'yes' == $this->service_sedex ) ? '04162' : ''; 
  261. $services['e-SEDEX'] = ( 'yes' == $this->service_esedex ) ? '81019' : ''; 
  262.  
  263. return array_filter( $services ); 
  264.  
  265. /** 
  266. * Gets the price of shipping. 
  267. * @param array $package Order package. 
  268. * @return array Correios Quotes. 
  269. */ 
  270. protected function correios_calculate( $package ) { 
  271. $services = array_values( $this->correios_services() ); 
  272. $connect = new WC_Correios_Webservice( $this->id ); 
  273. $connect->set_debug( $this->debug ); 
  274. $connect->set_service( $services ); 
  275. $connect->set_package( $package ); 
  276. $connect->set_origin_postcode( $this->zip_origin ); 
  277. $connect->set_destination_postcode( $package['destination']['postcode'] ); 
  278.  
  279. if ( 'declare' == $this->declare_value ) { 
  280. $connect->set_declared_value( $package['contents_cost'] ); 
  281.  
  282. if ( 'corporate' == $this->corporate_service ) { 
  283. $connect->set_login( $this->login ); 
  284. $connect->set_password( $this->password ); 
  285.  
  286. $connect->set_minimum_height( $this->minimum_height ); 
  287. $connect->set_minimum_width( $this->minimum_width ); 
  288. $connect->set_minimum_length( $this->minimum_length ); 
  289.  
  290. $shipping = $connect->get_shipping(); 
  291.  
  292. if ( ! empty( $shipping ) ) { 
  293. return $shipping; 
  294. } else { 
  295. // Cart only with virtual products. 
  296. if ( 'yes' == $this->debug ) { 
  297. $this->log->add( 'correios', 'Cart only with virtual products.' ); 
  298.  
  299. return array(); 
  300.  
  301. /** 
  302. * Gets the service name. 
  303. * @param int $code Correios service ID. 
  304. * @return array Correios service name. 
  305. */ 
  306. protected function get_service_name( $code ) { 
  307. $name = array( 
  308. '04510' => 'PAC',  
  309. '04014' => 'SEDEX',  
  310. '40215' => 'SEDEX 10',  
  311. '40290' => 'SEDEX Hoje',  
  312. '04669' => 'PAC',  
  313. '04162' => 'SEDEX',  
  314. '81019' => 'e-SEDEX',  
  315. ); 
  316.  
  317. return isset( $name[ $code ] ) ? $name[ $code ] : ''; 
  318.  
  319. /** 
  320. * Get additional time. 
  321. * @param array $package Package data. 
  322. * @return array 
  323. */ 
  324. protected function get_additional_time( $package = array() ) { 
  325. return apply_filters( 'woocommerce_correios_shipping_additional_time', $this->additional_time, $package ); 
  326.  
  327. /** 
  328. * Get accepted error codes. 
  329. * @return array 
  330. */ 
  331. protected function get_accepted_error_codes() { 
  332. $codes = apply_filters( 'woocommerce_correios_accepted_error_codes', array( '-33', '-3', '010' ) ); 
  333. $codes[] = '0'; 
  334.  
  335. return $codes; 
  336.  
  337. /** 
  338. * Calculates the shipping rate. 
  339. * @param array $package Order package. 
  340. */ 
  341. public function calculate_shipping( $package = array() ) { 
  342. $rates = array(); 
  343. $errors = array(); 
  344. $shipping_values = $this->correios_calculate( $package ); 
  345.  
  346. if ( ! empty( $shipping_values ) ) { 
  347. foreach ( $shipping_values as $shipping ) { 
  348. if ( ! isset( $shipping->Erro ) ) { 
  349. continue; 
  350.  
  351. $name = $this->get_service_name( (string) $shipping->Codigo ); 
  352. $error_number = (string) $shipping->Erro; 
  353. $error_message = wc_correios_get_error_message( $error_number ); 
  354. $errors[ $error_number ] = array( 
  355. 'error' => $error_message,  
  356. 'number' => $error_number,  
  357. ); 
  358.  
  359. if ( ! in_array( $error_number, $this->get_accepted_error_codes() ) ) { 
  360. continue; 
  361.  
  362. // Set the shipping rates. 
  363. $label = ( 'yes' == $this->display_date ) ? wc_correios_get_estimating_delivery( $name, (int) $shipping->PrazoEntrega, $this->get_additional_time( $package ) ) : $name; 
  364. $cost = wc_correios_normalize_price( esc_attr( (string) $shipping->Valor ) ); 
  365.  
  366. // Exit if don't have price. 
  367. if ( 0 === intval( $cost ) ) { 
  368. return; 
  369.  
  370. $fee = $this->get_fee( str_replace( ', ', '.', $this->fee ), $cost ); 
  371.  
  372. array_push( 
  373. $rates,  
  374. array( 
  375. 'id' => $name,  
  376. 'label' => $label,  
  377. 'cost' => $cost + $fee,  
  378. ); 
  379.  
  380. // Display correios errors. 
  381. if ( ! empty( $errors ) && is_cart() ) { 
  382. foreach ( $errors as $error ) { 
  383. if ( '' != $error['error'] ) { 
  384. $type = ( '010' == $error['number'] ) ? 'notice' : 'error'; 
  385. $message = '<strong>' . __( 'Correios', 'woocommerce-correios' ) . ':</strong> ' . esc_attr( $error['error'] ); 
  386. wc_add_notice( $message, $type ); 
  387.  
  388. $rates = apply_filters( 'woocommerce_correios_shipping_methods', $rates, $package ); 
  389.  
  390. // Add rates. 
  391. foreach ( $rates as $rate ) { 
  392. $this->add_rate( $rate );