WC_Correios_Shipping_Carta

Default Correios Carta shipping method abstract class.

Defined (1)

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

/includes/abstracts/abstract-wc-correios-shipping-carta.php  
  1. abstract class WC_Correios_Shipping_Carta extends WC_Correios_Shipping { 
  2.  
  3. /** 
  4. * Initialize Carta Registrada. 
  5. * @param int $instance_id Shipping zone instance. 
  6. */ 
  7. public function __construct( $instance_id = 0 ) { 
  8. $this->instance_id = absint( $instance_id ); 
  9. $this->method_description = sprintf( __( '%s is a 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->shipping_class = $this->get_option( 'shipping_class' ); 
  22. $this->show_delivery_time = $this->get_option( 'show_delivery_time' ); 
  23. $this->additional_time = $this->get_option( 'additional_time' ); 
  24. $this->fee = $this->get_option( 'fee' ); 
  25. $this->receipt_notice = $this->get_option( 'receipt_notice' ); 
  26. $this->own_hands = $this->get_option( 'own_hands' ); 
  27. $this->debug = $this->get_option( 'debug' ); 
  28.  
  29. // Active logs. 
  30. if ( 'yes' === $this->debug ) { 
  31. $this->log = new WC_Logger(); 
  32.  
  33. // Save admin options. 
  34. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  35.  
  36. /** 
  37. * Get shipping classes options. 
  38. * @return array 
  39. */ 
  40. protected function get_shipping_classes_options() { 
  41. $shipping_classes = WC()->shipping->get_shipping_classes(); 
  42. $options = array( 
  43. '' => __( '-- Select a shipping class --', 'woocommerce-correios' ),  
  44. ); 
  45.  
  46. if ( ! empty( $shipping_classes ) ) { 
  47. $options += wp_list_pluck( $shipping_classes, 'name', 'slug' ); 
  48.  
  49. return $options; 
  50.  
  51. /** 
  52. * Admin options fields. 
  53. */ 
  54. public function init_form_fields() { 
  55. $this->instance_form_fields = array( 
  56. 'enabled' => array( 
  57. 'title' => __( 'Enable/Disable', 'woocommerce-correios' ),  
  58. 'type' => 'checkbox',  
  59. 'label' => __( 'Enable this shipping method', 'woocommerce-correios' ),  
  60. 'default' => 'yes',  
  61. ),  
  62. 'title' => array( 
  63. 'title' => __( 'Title', 'woocommerce-correios' ),  
  64. 'type' => 'text',  
  65. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce-correios' ),  
  66. 'desc_tip' => true,  
  67. 'default' => $this->method_title,  
  68. ),  
  69. 'behavior_options' => array( 
  70. 'title' => __( 'Behavior Options', 'woocommerce-correios' ),  
  71. 'type' => 'title',  
  72. 'default' => '',  
  73. ),  
  74. 'shipping_class' => array( 
  75. 'title' => __( 'Shipping Class', 'woocommerce-correios' ),  
  76. 'type' => 'select',  
  77. 'description' => __( 'Select for which shipping class this method will be applied.', 'woocommerce-correios' ),  
  78. 'desc_tip' => true,  
  79. 'default' => '',  
  80. 'class' => 'wc-enhanced-select',  
  81. 'options' => $this->get_shipping_classes_options(),  
  82. ),  
  83. 'show_delivery_time' => array( 
  84. 'title' => __( 'Delivery Time', 'woocommerce-correios' ),  
  85. 'type' => 'checkbox',  
  86. 'label' => __( 'Show estimated delivery time', 'woocommerce-correios' ),  
  87. 'description' => __( 'Display the estimated delivery time in working days.', 'woocommerce-correios' ),  
  88. 'desc_tip' => true,  
  89. 'default' => 'no',  
  90. ),  
  91. 'additional_time' => array( 
  92. 'title' => __( 'Delivery Days', 'woocommerce-correios' ),  
  93. 'type' => 'text',  
  94. 'description' => __( 'Working days to the estimated delivery.', 'woocommerce-correios' ),  
  95. 'desc_tip' => true,  
  96. 'default' => '0',  
  97. 'placeholder' => '0',  
  98. ),  
  99. 'fee' => array( 
  100. 'title' => __( 'Handling Fee', 'woocommerce-correios' ),  
  101. 'type' => 'price',  
  102. 'description' => __( 'Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce-correios' ),  
  103. 'desc_tip' => true,  
  104. 'placeholder' => '0.00',  
  105. 'default' => '',  
  106. ),  
  107. 'optional_services' => array( 
  108. 'title' => __( 'Optional Services', 'woocommerce-correios' ),  
  109. 'type' => 'title',  
  110. 'description' => __( 'Use these options to add the value of each service provided by the Correios.', 'woocommerce-correios' ),  
  111. 'default' => '',  
  112. ),  
  113. 'receipt_notice' => array( 
  114. 'title' => __( 'Receipt Notice', 'woocommerce-correios' ),  
  115. 'type' => 'checkbox',  
  116. 'label' => __( 'Enable receipt notice', 'woocommerce-correios' ),  
  117. 'description' => __( 'This controls whether to add costs of the receipt notice service.', 'woocommerce-correios' ),  
  118. 'desc_tip' => true,  
  119. 'default' => 'no',  
  120. ),  
  121. 'own_hands' => array( 
  122. 'title' => __( 'Own Hands', 'woocommerce-correios' ),  
  123. 'type' => 'checkbox',  
  124. 'label' => __( 'Enable own hands', 'woocommerce-correios' ),  
  125. 'description' => __( 'This controls whether to add costs of the own hands service', 'woocommerce-correios' ),  
  126. 'desc_tip' => true,  
  127. 'default' => 'no',  
  128. ),  
  129. 'testing' => array( 
  130. 'title' => __( 'Testing', 'woocommerce-correios' ),  
  131. 'type' => 'title',  
  132. 'default' => '',  
  133. ),  
  134. 'debug' => array( 
  135. 'title' => __( 'Debug Log', 'woocommerce-correios' ),  
  136. 'type' => 'checkbox',  
  137. 'label' => __( 'Enable logging', 'woocommerce-correios' ),  
  138. 'default' => 'no',  
  139. 'description' => sprintf( __( 'Log %s events, such as WebServices requests.', 'woocommerce-correios' ), $this->method_title ) . $this->get_log_link(),  
  140. ),  
  141. ); 
  142.  
  143. /** 
  144. * Get costs. 
  145. * @return array 
  146. */ 
  147. protected function get_costs() { 
  148. return array(); 
  149.  
  150. /** 
  151. * Get shpping cost. 
  152. * @param array $package Shipping package. 
  153. * @return float 
  154. */ 
  155. protected function get_shipping_cost( $package ) { 
  156. return 0; 
  157.  
  158. /** 
  159. * Get shpping time. 
  160. * @param array $package Shipping package. 
  161. * @return int 
  162. */ 
  163. protected function get_shipping_time( $package ) { 
  164. return 0; 
  165.  
  166. /** 
  167. * Get package weight. 
  168. * @param array $package Shipping package. 
  169. * @return float|bool 
  170. */ 
  171. protected function get_package_weight( $package ) { 
  172. $weight = 0; 
  173.  
  174. foreach ( $package['contents'] as $value ) { 
  175. $product = $value['data']; 
  176. $qty = $value['quantity']; 
  177. $product_weight = 0; 
  178.  
  179. // Check if all or some items in the cart don't supports this shipping method. 
  180. if ( $this->shipping_class !== $product->get_shipping_class() ) { 
  181. if ( 'yes' === $this->debug ) { 
  182. $this->log->add( $this->id, 'One or all items in the cart do not supports the configured shipping class' ); 
  183.  
  184. return false; 
  185.  
  186. if ( $qty > 0 && $product->needs_shipping() ) { 
  187. $product_weight = wc_get_weight( (float) $product->get_weight(), 'g' ); 
  188.  
  189. if ( $qty > 1 ) { 
  190. $product_weight *= $qty; 
  191.  
  192. $weight += $product_weight; 
  193.  
  194. return $weight; 
  195.  
  196. /** 
  197. * Calculates the shipping rate. 
  198. * @param array $package Order package. 
  199. */ 
  200. public function calculate_shipping( $package = array() ) { 
  201. // Check if valid to be calculeted. 
  202. if ( '' === $package['destination']['postcode'] || 'BR' !== $package['destination']['country'] ) { 
  203. return; 
  204.  
  205. // Cost. 
  206. $cost = $this->get_shipping_cost( $package ); 
  207.  
  208. if ( 0 === $cost ) { 
  209. return; 
  210.  
  211. // Apply fees. 
  212. $fee = $this->get_fee( $this->fee, $cost ); 
  213.  
  214. // Create the rate and apply filters. 
  215. $rate = apply_filters( 'woocommerce_correios_' . $this->id . '_rate', array( 
  216. 'id' => $this->id . $this->instance_id,  
  217. 'label' => $this->get_shipping_method_label( (int) $this->get_shipping_time( $package ), $package ),  
  218. 'cost' => (float) $cost + (float) $fee,  
  219. ), $this->instance_id, $package ); 
  220.  
  221. // Add rate to WooCommerce. 
  222. $this->add_rate( $rate );