WC_Correios_Shipping

Default Correios shipping method abstract class.

Defined (1)

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

/includes/abstracts/abstract-wc-correios-shipping.php  
  1. abstract class WC_Correios_Shipping extends WC_Shipping_Method { 
  2.  
  3. /** 
  4. * Service code. 
  5. * @var string 
  6. */ 
  7. protected $code = ''; 
  8.  
  9. /** 
  10. * Corporate code. 
  11. * @var string 
  12. */ 
  13. protected $corporate_code = ''; 
  14.  
  15. /** 
  16. * Initialize the Correios shipping method. 
  17. * @param int $instance_id Shipping zone instance ID. 
  18. */ 
  19. public function __construct( $instance_id = 0 ) { 
  20. $this->instance_id = absint( $instance_id ); 
  21. $this->method_description = sprintf( __( '%s is a shipping method from Correios.', 'woocommerce-correios' ), $this->method_title ); 
  22. $this->supports = array( 
  23. 'shipping-zones',  
  24. 'instance-settings',  
  25. ); 
  26.  
  27. // Load the form fields. 
  28. $this->init_form_fields(); 
  29.  
  30. // Define user set variables. 
  31. $this->enabled = $this->get_option( 'enabled' ); 
  32. $this->title = $this->get_option( 'title' ); 
  33. $this->origin_postcode = $this->get_option( 'origin_postcode' ); 
  34. $this->shipping_class_id = (int) $this->get_option( 'shipping_class_id', '-1' ); 
  35. $this->show_delivery_time = $this->get_option( 'show_delivery_time' ); 
  36. $this->additional_time = $this->get_option( 'additional_time' ); 
  37. $this->fee = $this->get_option( 'fee' ); 
  38. $this->receipt_notice = $this->get_option( 'receipt_notice' ); 
  39. $this->own_hands = $this->get_option( 'own_hands' ); 
  40. $this->declare_value = $this->get_option( 'declare_value' ); 
  41. $this->custom_code = $this->get_option( 'custom_code' ); 
  42. $this->service_type = $this->get_option( 'service_type' ); 
  43. $this->login = $this->get_option( 'login' ); 
  44. $this->password = $this->get_option( 'password' ); 
  45. $this->minimum_height = $this->get_option( 'minimum_height' ); 
  46. $this->minimum_width = $this->get_option( 'minimum_width' ); 
  47. $this->minimum_length = $this->get_option( 'minimum_length' ); 
  48. $this->debug = $this->get_option( 'debug' ); 
  49.  
  50. // Save admin options. 
  51. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  52.  
  53. /** 
  54. * Get log. 
  55. * @return string 
  56. */ 
  57. protected function get_log_link() { 
  58. 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' ) ) . '">' . __( 'View logs.', 'woocommerce-correios' ) . '</a>'; 
  59.  
  60. /** 
  61. * Get shipping classes options. 
  62. * @return array 
  63. */ 
  64. protected function get_shipping_classes_options() { 
  65. $shipping_classes = WC()->shipping->get_shipping_classes(); 
  66. $options = array( 
  67. '-1' => __( 'Any Shipping Class', 'woocommerce-correios' ),  
  68. '0' => __( 'No Shipping Class', 'woocommerce-correios' ),  
  69. ); 
  70.  
  71. if ( ! empty( $shipping_classes ) ) { 
  72. $options += wp_list_pluck( $shipping_classes, 'name', 'term_id' ); 
  73.  
  74. return $options; 
  75.  
  76. /** 
  77. * Admin options fields. 
  78. */ 
  79. public function init_form_fields() { 
  80. $this->instance_form_fields = array( 
  81. 'enabled' => array( 
  82. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  83. 'type' => 'checkbox',  
  84. 'label' => __( 'Enable this shipping method', 'woocommerce-correios' ),  
  85. 'default' => 'yes',  
  86. ),  
  87. 'title' => array( 
  88. 'title' => __( 'Title', 'woocommerce-correios' ),  
  89. 'type' => 'text',  
  90. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-correios' ),  
  91. 'desc_tip' => true,  
  92. 'default' => $this->method_title,  
  93. ),  
  94. 'behavior_options' => array( 
  95. 'title' => __( 'Behavior Options', 'woocommerce-correios' ),  
  96. 'type' => 'title',  
  97. 'default' => '',  
  98. ),  
  99. 'origin_postcode' => array( 
  100. 'title' => __( 'Origin Postcode', 'woocommerce-correios' ),  
  101. 'type' => 'text',  
  102. 'description' => __( 'The postcode of the location your packages are delivered from.', 'woocommerce-correios' ),  
  103. 'desc_tip' => true,  
  104. 'placeholder' => '00000-000',  
  105. 'default' => '',  
  106. ),  
  107. 'shipping_class_id' => array( 
  108. 'title' => __( 'Shipping Class', 'woocommerce-correios' ),  
  109. 'type' => 'select',  
  110. 'description' => __( 'If necessary, select a shipping class to apply this method.', 'woocommerce-correios' ),  
  111. 'desc_tip' => true,  
  112. 'default' => '',  
  113. 'class' => 'wc-enhanced-select',  
  114. 'options' => $this->get_shipping_classes_options(),  
  115. ),  
  116. 'show_delivery_time' => array( 
  117. 'title' => __( 'Delivery Time', 'woocommerce-correios' ),  
  118. 'type' => 'checkbox',  
  119. 'label' => __( 'Show estimated delivery time', 'woocommerce-correios' ),  
  120. 'description' => __( 'Display the estimated delivery time in working days.', 'woocommerce-correios' ),  
  121. 'desc_tip' => true,  
  122. 'default' => 'no',  
  123. ),  
  124. 'additional_time' => array( 
  125. 'title' => __( 'Additional Days', 'woocommerce-correios' ),  
  126. 'type' => 'text',  
  127. 'description' => __( 'Additional working days to the estimated delivery.', 'woocommerce-correios' ),  
  128. 'desc_tip' => true,  
  129. 'default' => '0',  
  130. 'placeholder' => '0',  
  131. ),  
  132. 'fee' => array( 
  133. 'title' => __( 'Handling Fee', 'woocommerce-correios' ),  
  134. 'type' => 'price',  
  135. 'description' => __( 'Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce-correios' ),  
  136. 'desc_tip' => true,  
  137. 'placeholder' => '0.00',  
  138. 'default' => '',  
  139. ),  
  140. 'optional_services' => array( 
  141. 'title' => __( 'Optional Services', 'woocommerce-correios' ),  
  142. 'type' => 'title',  
  143. 'description' => __( 'Use these options to add the value of each service provided by the Correios.', 'woocommerce-correios' ),  
  144. 'default' => '',  
  145. ),  
  146. 'receipt_notice' => array( 
  147. 'title' => __( 'Receipt Notice', 'woocommerce-correios' ),  
  148. 'type' => 'checkbox',  
  149. 'label' => __( 'Enable receipt notice', 'woocommerce-correios' ),  
  150. 'description' => __( 'This controls whether to add costs of the receipt notice service.', 'woocommerce-correios' ),  
  151. 'desc_tip' => true,  
  152. 'default' => 'no',  
  153. ),  
  154. 'own_hands' => array( 
  155. 'title' => __( 'Own Hands', 'woocommerce-correios' ),  
  156. 'type' => 'checkbox',  
  157. 'label' => __( 'Enable own hands', 'woocommerce-correios' ),  
  158. 'description' => __( 'This controls whether to add costs of the own hands service', 'woocommerce-correios' ),  
  159. 'desc_tip' => true,  
  160. 'default' => 'no',  
  161. ),  
  162. 'declare_value' => array( 
  163. 'title' => __( 'Declare Value for Insurance', 'woocommerce-correios' ),  
  164. 'type' => 'checkbox',  
  165. 'label' => __( 'Enable declared value', 'woocommerce-correios' ),  
  166. 'description' => __( 'This controls if the price of the package must be declared for insurance purposes.', 'woocommerce-correios' ),  
  167. 'desc_tip' => true,  
  168. 'default' => 'yes',  
  169. ),  
  170. 'service_options' => array( 
  171. 'title' => __( 'Service Options', 'woocommerce-correios' ),  
  172. 'type' => 'title',  
  173. 'default' => '',  
  174. ),  
  175. 'custom_code' => array( 
  176. 'title' => __( 'Service Code', 'woocommerce-correios' ),  
  177. 'type' => 'text',  
  178. 'description' => __( 'Service code, use this for custom codes.', 'woocommerce-correios' ),  
  179. 'desc_tip' => true,  
  180. 'placeholder' => $this->code,  
  181. 'default' => '',  
  182. ),  
  183. 'service_type' => array( 
  184. 'title' => __( 'Service Type', 'woocommerce-correios' ),  
  185. 'type' => 'select',  
  186. 'description' => __( 'Choose between conventional or corporate service.', 'woocommerce-correios' ),  
  187. 'desc_tip' => true,  
  188. 'default' => 'conventional',  
  189. 'class' => 'wc-enhanced-select',  
  190. 'options' => array( 
  191. 'conventional' => __( 'Conventional', 'woocommerce-correios' ),  
  192. 'corporate' => __( 'Corporate', 'woocommerce-correios' ),  
  193. ),  
  194. ),  
  195. 'login' => array( 
  196. 'title' => __( 'Administrative Code', 'woocommerce-correios' ),  
  197. 'type' => 'text',  
  198. 'description' => __( 'Your Correios login. It\'s usually your CNPJ.', 'woocommerce-correios' ),  
  199. 'desc_tip' => true,  
  200. 'default' => '',  
  201. ),  
  202. 'password' => array( 
  203. 'title' => __( 'Administrative Password', 'woocommerce-correios' ),  
  204. 'type' => 'text',  
  205. 'description' => __( 'Your Correios password.', 'woocommerce-correios' ),  
  206. 'desc_tip' => true,  
  207. 'default' => '',  
  208. ),  
  209. 'package_standard' => array( 
  210. 'title' => __( 'Package Standard', 'woocommerce-correios' ),  
  211. 'type' => 'title',  
  212. 'description' => __( 'Minimum measure for your shipping packages.', 'woocommerce-correios' ),  
  213. 'default' => '',  
  214. ),  
  215. 'minimum_height' => array( 
  216. 'title' => __( 'Minimum Height', 'woocommerce-correios' ),  
  217. 'type' => 'text',  
  218. 'description' => __( 'Minimum height of your shipping packages. Correios needs at least 2cm.', 'woocommerce-correios' ),  
  219. 'desc_tip' => true,  
  220. 'default' => '2',  
  221. ),  
  222. 'minimum_width' => array( 
  223. 'title' => __( 'Minimum Width', 'woocommerce-correios' ),  
  224. 'type' => 'text',  
  225. 'description' => __( 'Minimum width of your shipping packages. Correios needs at least 11cm.', 'woocommerce-correios' ),  
  226. 'desc_tip' => true,  
  227. 'default' => '11',  
  228. ),  
  229. 'minimum_length' => array( 
  230. 'title' => __( 'Minimum Length', 'woocommerce-correios' ),  
  231. 'type' => 'text',  
  232. 'description' => __( 'Minimum length of your shipping packages. Correios needs at least 16cm.', 'woocommerce-correios' ),  
  233. 'desc_tip' => true,  
  234. 'default' => '16',  
  235. ),  
  236. 'testing' => array( 
  237. 'title' => __( 'Testing', 'woocommerce-correios' ),  
  238. 'type' => 'title',  
  239. 'default' => '',  
  240. ),  
  241. 'debug' => array( 
  242. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  243. 'type' => 'checkbox',  
  244. 'label' => __( 'Enable logging', 'woocommerce-correios' ),  
  245. 'default' => 'no',  
  246. 'description' => sprintf( __( 'Log %s events, such as WebServices requests.', 'woocommerce-correios' ), $this->method_title ) . $this->get_log_link(),  
  247. ),  
  248. ); 
  249.  
  250. /** 
  251. * Correios options page. 
  252. */ 
  253. public function admin_options() { 
  254. include WC_Correios::get_plugin_path() . 'includes/admin/views/html-admin-shipping-method-settings.php'; 
  255.  
  256. /** 
  257. * Get Correios service code. 
  258. * @return string 
  259. */ 
  260. public function get_code() { 
  261. if ( ! empty( $this->custom_code ) ) { 
  262. $code = $this->custom_code; 
  263. } elseif ( $this->is_corporate() && ! empty( $this->corporate_code ) ) { 
  264. $code = $this->corporate_code; 
  265. } else { 
  266. $code = $this->code; 
  267.  
  268. return apply_filters( 'woocommerce_correios_shipping_method_code', $code, $this->id, $this->instance_id ); 
  269.  
  270. /** 
  271. * Check if need to use corporate services. 
  272. * @return bool 
  273. */ 
  274. protected function is_corporate() { 
  275. return 'corporate' === $this->service_type; 
  276.  
  277. /** 
  278. * Get login. 
  279. * @return string 
  280. */ 
  281. public function get_login() { 
  282. return $this->is_corporate() ? $this->login : ''; 
  283.  
  284. /** 
  285. * Get password. 
  286. * @return string 
  287. */ 
  288. public function get_password() { 
  289. return $this->is_corporate() ? $this->password : ''; 
  290.  
  291. /** 
  292. * Get the declared value from the package. 
  293. * @param array $package Cart package. 
  294. * @return float 
  295. */ 
  296. protected function get_declared_value( $package ) { 
  297. return $package['contents_cost']; 
  298.  
  299. /** 
  300. * Get shipping rate. 
  301. * @param array $package Cart package. 
  302. * @return SimpleXMLElement|null 
  303. */ 
  304. protected function get_rate( $package ) { 
  305. $api = new WC_Correios_Webservice( $this->id, $this->instance_id ); 
  306. $api->set_debug( $this->debug ); 
  307. $api->set_service( $this->get_code() ); 
  308. $api->set_package( $package ); 
  309. $api->set_origin_postcode( $this->origin_postcode ); 
  310. $api->set_destination_postcode( $package['destination']['postcode'] ); 
  311.  
  312. if ( 'yes' === $this->declare_value ) { 
  313. $api->set_declared_value( $this->get_declared_value( $package ) ); 
  314.  
  315. $api->set_own_hands( 'yes' === $this->own_hands ? 'S' : 'N' ); 
  316. $api->set_receipt_notice( 'yes' === $this->receipt_notice ? 'S' : 'N' ); 
  317.  
  318. $api->set_login( $this->get_login() ); 
  319. $api->set_password( $this->get_password() ); 
  320.  
  321. $api->set_minimum_height( $this->minimum_height ); 
  322. $api->set_minimum_width( $this->minimum_width ); 
  323. $api->set_minimum_length( $this->minimum_length ); 
  324.  
  325. $shipping = $api->get_shipping(); 
  326.  
  327. return $shipping; 
  328.  
  329. /** 
  330. * Get additional time. 
  331. * @param array $package Package data. 
  332. * @return array 
  333. */ 
  334. protected function get_additional_time( $package = array() ) { 
  335. return apply_filters( 'woocommerce_correios_shipping_additional_time', $this->additional_time, $package ); 
  336.  
  337. /** 
  338. * Get accepted error codes. 
  339. * @return array 
  340. */ 
  341. protected function get_accepted_error_codes() { 
  342. $codes = apply_filters( 'woocommerce_correios_accepted_error_codes', array( '-33', '-3', '010' ) ); 
  343. $codes[] = '0'; 
  344.  
  345. return $codes; 
  346.  
  347. /** 
  348. * Get shipping method label. 
  349. * @param int $days Days to deliver. 
  350. * @param array $package Package data. 
  351. * @return string 
  352. */ 
  353. protected function get_shipping_method_label( $days, $package ) { 
  354. if ( 'yes' === $this->show_delivery_time ) { 
  355. return wc_correios_get_estimating_delivery( $this->title, $days, $this->get_additional_time( $package ) ); 
  356.  
  357. return $this->title; 
  358.  
  359. /** 
  360. * Check if package uses only the selected shipping class. 
  361. * @param array $package Cart package. 
  362. * @return bool 
  363. */ 
  364. protected function has_only_selected_shipping_class( $package ) { 
  365. $only_selected = true; 
  366.  
  367. if ( -1 === $this->shipping_class_id ) { 
  368. return $only_selected; 
  369.  
  370. foreach ( $package['contents'] as $item_id => $values ) { 
  371. $product = $values['data']; 
  372. $qty = $values['quantity']; 
  373.  
  374. if ( $qty > 0 && $product->needs_shipping() ) { 
  375. if ( $this->shipping_class_id !== $product->get_shipping_class_id() ) { 
  376. $only_selected = false; 
  377. break; 
  378.  
  379. return $only_selected; 
  380.  
  381. /** 
  382. * Calculates the shipping rate. 
  383. * @param array $package Order package. 
  384. */ 
  385. public function calculate_shipping( $package = array() ) { 
  386. // Check if valid to be calculeted. 
  387. if ( '' === $package['destination']['postcode'] || 'BR' !== $package['destination']['country'] ) { 
  388. return; 
  389.  
  390. // Check for shipping classes. 
  391. if ( ! $this->has_only_selected_shipping_class( $package ) ) { 
  392. return; 
  393.  
  394. $shipping = $this->get_rate( $package ); 
  395.  
  396. if ( ! isset( $shipping->Erro ) ) { 
  397. return; 
  398.  
  399. $error_number = (string) $shipping->Erro; 
  400.  
  401. // Exit if have errors. 
  402. if ( ! in_array( $error_number, $this->get_accepted_error_codes(), true ) ) { 
  403. return; 
  404.  
  405. // Display Correios errors. 
  406. $error_message = wc_correios_get_error_message( $error_number ); 
  407. if ( '' !== $error_message && is_cart() ) { 
  408. $notice_type = ( '010' === $error_number ) ? 'notice' : 'error'; 
  409. $notice = '<strong>' . $this->title . ':</strong> ' . esc_html( $error_message ); 
  410. wc_add_notice( $notice, $notice_type ); 
  411.  
  412. // Set the shipping rates. 
  413. $label = $this->get_shipping_method_label( (int) $shipping->PrazoEntrega, $package ); 
  414. $cost = wc_correios_normalize_price( esc_attr( (string) $shipping->Valor ) ); 
  415.  
  416. // Exit if don't have price. 
  417. if ( 0 === intval( $cost ) ) { 
  418. return; 
  419.  
  420. // Apply fees. 
  421. $fee = $this->get_fee( $this->fee, $cost ); 
  422.  
  423. // Create the rate and apply filters. 
  424. $rate = apply_filters( 'woocommerce_correios_' . $this->id . '_rate', array( 
  425. 'id' => $this->id . $this->instance_id,  
  426. 'label' => $label,  
  427. 'cost' => (float) $cost + (float) $fee,  
  428. ), $this->instance_id, $package ); 
  429.  
  430. // Deprecated filter. 
  431. $rates = apply_filters( 'woocommerce_correios_shipping_methods', array( $rate ), $package ); 
  432.  
  433. // Add rate to WooCommerce. 
  434. $this->add_rate( $rates[0] );