WC_Correios_Shipping_International

Default Correios international shipping method abstract class.

Defined (1)

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

/includes/abstracts/abstract-wc-correios-shipping-international.php  
  1. abstract class WC_Correios_Shipping_International extends WC_Correios_Shipping { 
  2.  
  3. /** 
  4. * Initialize the Correios shipping method. 
  5. * @param int $instance_id Shipping zone instance ID. 
  6. */ 
  7. public function __construct( $instance_id = 0 ) { 
  8. $this->instance_id = absint( $instance_id ); 
  9. $this->method_description = sprintf( __( '%s is a international shipping method from Correios.', 'woocommerce-correios' ), $this->method_title ); 
  10. $this->supports = array( 
  11. 'shipping-zones',  
  12. 'instance-settings',  
  13. ); 
  14.  
  15. // Load the form fields. 
  16. $this->init_form_fields(); 
  17.  
  18. // Define user set variables. 
  19. $this->enabled = $this->get_option( 'enabled' ); 
  20. $this->title = $this->get_option( 'title' ); 
  21. $this->origin_state = $this->get_option( 'origin_state' ); 
  22. $this->origin_location = $this->get_option( 'origin_location' ); 
  23. $this->shipping_class_id = (int) $this->get_option( 'shipping_class_id', '-1' ); 
  24. $this->show_delivery_time = $this->get_option( 'show_delivery_time' ); 
  25. $this->fee = $this->get_option( 'fee' ); 
  26. $this->debug = $this->get_option( 'debug' ); 
  27.  
  28. // Save admin options. 
  29. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  30.  
  31. /** 
  32. * Admin options fields. 
  33. */ 
  34. public function init_form_fields() { 
  35. $this->instance_form_fields = array( 
  36. 'enabled' => array( 
  37. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  38. 'type' => 'checkbox',  
  39. 'label' => __( 'Enable this shipping method', 'woocommerce-correios' ),  
  40. 'default' => 'yes',  
  41. ),  
  42. 'title' => array( 
  43. 'title' => __( 'Title', 'woocommerce-correios' ),  
  44. 'type' => 'text',  
  45. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-correios' ),  
  46. 'desc_tip' => true,  
  47. 'default' => $this->method_title,  
  48. ),  
  49. 'behavior_options' => array( 
  50. 'title' => __( 'Behavior Options', 'woocommerce-correios' ),  
  51. 'type' => 'title',  
  52. 'default' => '',  
  53. ),  
  54. 'origin_state' => array( 
  55. 'title' => __( 'Origin State', 'woocommerce-correios' ),  
  56. 'type' => 'select',  
  57. 'description' => __( 'The UF of the location your packages are delivered from.', 'woocommerce-correios' ),  
  58. 'desc_tip' => true,  
  59. 'default' => '',  
  60. 'class' => 'wc-enhanced-select',  
  61. 'options' => WC()->countries->get_states( 'BR' ),  
  62. ),  
  63. 'origin_location' => array( 
  64. 'title' => __( 'Origin Locale', 'woocommerce-correios' ),  
  65. 'type' => 'select',  
  66. 'description' => __( 'The location of your packages are delivered from.', 'woocommerce-correios' ),  
  67. 'desc_tip' => true,  
  68. 'default' => 'C',  
  69. 'class' => 'wc-enhanced-select',  
  70. 'options' => array( 
  71. 'C' => __( 'Capital', 'woocommerce-correios' ),  
  72. 'I' => __( 'Interior', 'woocommerce-correios' ),  
  73. ),  
  74. ),  
  75. 'shipping_class_id' => array( 
  76. 'title' => __( 'Shipping Class', 'woocommerce-correios' ),  
  77. 'type' => 'select',  
  78. 'description' => __( 'If necessary, select a shipping class to apply this method.', 'woocommerce-correios' ),  
  79. 'desc_tip' => true,  
  80. 'default' => '',  
  81. 'class' => 'wc-enhanced-select',  
  82. 'options' => $this->get_shipping_classes_options(),  
  83. ),  
  84. 'show_delivery_time' => array( 
  85. 'title' => __( 'Delivery Time', 'woocommerce-correios' ),  
  86. 'type' => 'checkbox',  
  87. 'label' => __( 'Show estimated delivery time', 'woocommerce-correios' ),  
  88. 'description' => __( 'Display the estimated delivery time in working days.', 'woocommerce-correios' ),  
  89. 'desc_tip' => true,  
  90. 'default' => 'no',  
  91. ),  
  92. 'fee' => array( 
  93. 'title' => __( 'Handling Fee', 'woocommerce-correios' ),  
  94. 'type' => 'price',  
  95. 'description' => __( 'Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce-correios' ),  
  96. 'desc_tip' => true,  
  97. 'placeholder' => '0.00',  
  98. 'default' => '',  
  99. ),  
  100. 'optional_services' => array( 
  101. 'title' => __( 'Optional Services', 'woocommerce-correios' ),  
  102. 'type' => 'title',  
  103. 'description' => __( 'Use these options to add the value of each service provided by the Correios.', 'woocommerce-correios' ),  
  104. 'default' => '',  
  105. ),  
  106. 'testing' => array( 
  107. 'title' => __( 'Testing', 'woocommerce-correios' ),  
  108. 'type' => 'title',  
  109. 'default' => '',  
  110. ),  
  111. 'debug' => array( 
  112. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  113. 'type' => 'checkbox',  
  114. 'label' => __( 'Enable logging', 'woocommerce-correios' ),  
  115. 'default' => 'no',  
  116. 'description' => sprintf( __( 'Log %s events, such as WebServices requests.', 'woocommerce-correios' ), $this->method_title ) . $this->get_log_link(),  
  117. ),  
  118. ); 
  119.  
  120. /** 
  121. * Get Correios service code. 
  122. * @return string 
  123. */ 
  124. public function get_code() { 
  125. return apply_filters( 'woocommerce_correios_shipping_method_code', $this->code, $this->id, $this->instance_id ); 
  126.  
  127. /** 
  128. * Get shipping rate. 
  129. * @param array $package Order package. 
  130. * @return SimpleXMLElement|null 
  131. */ 
  132. protected function get_rate( $package ) { 
  133. $api = new WC_Correios_Webservice_International( $this->id, $this->instance_id ); 
  134. $api->set_debug( $this->debug ); 
  135. $api->set_service( $this->get_code() ); 
  136. $api->set_package( $package ); 
  137. $api->set_destination_country( $package['destination']['country'] ); 
  138. $api->set_origin_state( $this->origin_state ); 
  139. $api->set_origin_location( $this->origin_location ); 
  140.  
  141. $shipping = $api->get_shipping(); 
  142.  
  143. return $shipping; 
  144.  
  145. /** 
  146. * Calculates the shipping rate. 
  147. * @param array $package Order package. 
  148. */ 
  149. public function calculate_shipping( $package = array() ) { 
  150. $api = new WC_Correios_Webservice_International( $this->id, $this->instance_id ); 
  151.  
  152. // Check if valid to be calculeted. 
  153. if ( ! in_array( $package['destination']['country'], $api->get_allowed_countries(), true ) ) { 
  154. return; 
  155.  
  156. // Check for shipping classes. 
  157. if ( ! $this->has_only_selected_shipping_class( $package ) ) { 
  158. return; 
  159.  
  160. $shipping = $this->get_rate( $package ); 
  161.  
  162. if ( empty( $shipping->dados_postais->preco_postal ) ) { 
  163. return; 
  164.  
  165. // Set the shipping rates. 
  166. $label = $this->title; 
  167. if ( 'yes' === $this->show_delivery_time ) { 
  168. $label .= ' (' . sanitize_text_field( (string) $shipping->dados_postais->prazo_entrega ) . ')'; 
  169. $cost = sanitize_text_field( (float) $shipping->dados_postais->preco_postal ); 
  170.  
  171. // Exit if don't have price. 
  172. if ( 0 === intval( $cost ) ) { 
  173. return; 
  174.  
  175. // Apply fees. 
  176. $fee = $this->get_fee( $this->fee, $cost ); 
  177.  
  178. // Create the rate and apply filters. 
  179. $rate = apply_filters( 'woocommerce_correios_' . $this->id . '_rate', array( 
  180. 'id' => $this->id . $this->instance_id,  
  181. 'label' => $label,  
  182. 'cost' => (float) $cost + (float) $fee,  
  183. ), $this->instance_id, $package ); 
  184.  
  185. // Deprecated filter. 
  186. $rates = apply_filters( 'woocommerce_correios_shipping_methods', array( $rate ), $package ); 
  187.  
  188. // Add rate to WooCommerce. 
  189. $this->add_rate( $rates[0] );