wc_correios_update_tracking_code

Update tracking code.

Description

wc_correios_update_tracking_code( (WC_Order|int) $order, (string) $tracking_code, (bool) $remove = false ); 

Parameters (3)

0. $order (WC_Order|int)
Order ID or order data.
1. $tracking_code (string)
The tracking code.
2. $remove — Optional. (bool) => false
Remove If should remove the tracking code.

Usage

  1. if ( !function_exists( 'wc_correios_update_tracking_code' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-correios/includes/wc-correios-functions.php'; 
  3.  
  4. // Order ID or order data. 
  5. $order = null; 
  6.  
  7. // The tracking code. 
  8. $tracking_code = ''; 
  9.  
  10. // remove If should remove the tracking code. 
  11. $remove = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_correios_update_tracking_code($order, $tracking_code, $remove); 
  15.  

Defined (1)

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

/includes/wc-correios-functions.php  
  1. function wc_correios_update_tracking_code( $order, $tracking_code, $remove = false ) { 
  2. $tracking_code = sanitize_text_field( $tracking_code ); 
  3.  
  4. // Get order instance. 
  5. if ( is_numeric( $order ) ) { 
  6. $order = wc_get_order( $order ); 
  7.  
  8. if ( method_exists( $order, 'get_meta' ) ) { 
  9. $tracking_codes = $order->get_meta( '_correios_tracking_code' ); 
  10. } else { 
  11. $tracking_codes = $order->correios_tracking_code; 
  12.  
  13. $tracking_codes = array_filter( explode( ', ', $tracking_codes ) ); 
  14.  
  15. if ( '' === $tracking_code ) { 
  16. if ( method_exists( $order, 'delete_meta_data' ) ) { 
  17. $order->delete_meta_data( '_correios_tracking_code' ); 
  18. $order->save(); 
  19. } else { 
  20. delete_post_meta( $order->id, '_correios_tracking_code' ); 
  21.  
  22. return true; 
  23. } elseif ( ! $remove && ! in_array( $tracking_code, $tracking_codes, true ) ) { 
  24. $tracking_codes[] = $tracking_code; 
  25.  
  26. if ( method_exists( $order, 'update_meta_data' ) ) { 
  27. $order->update_meta_data( '_correios_tracking_code', implode( ', ', $tracking_codes ) ); 
  28. $order->save(); 
  29. } else { 
  30. update_post_meta( $order->id, '_correios_tracking_code', implode( ', ', $tracking_codes ) ); 
  31.  
  32. // Add order note. 
  33. $order->add_order_note( sprintf( __( 'Added a Correios tracking code: %s', 'woocommerce-correios' ), $tracking_code ) ); 
  34.  
  35. // Send email notification. 
  36. wc_correios_trigger_tracking_code_email( $order, $tracking_code ); 
  37.  
  38. return true; 
  39. } elseif ( $remove && in_array( $tracking_code, $tracking_codes, true ) ) { 
  40. if ( false !== ( $key = array_search( $tracking_code, $tracking_codes ) ) ) { 
  41. unset( $tracking_codes[ $key ] ); 
  42.  
  43. if ( method_exists( $order, 'update_meta_data' ) ) { 
  44. $order->update_meta_data( '_correios_tracking_code', implode( ', ', $tracking_codes ) ); 
  45. $order->save(); 
  46. } else { 
  47. update_post_meta( $order->id, '_correios_tracking_code', implode( ', ', $tracking_codes ) ); 
  48.  
  49. // Add order note. 
  50. $order->add_order_note( sprintf( __( 'Removed a Correios tracking code: %s', 'woocommerce-correios' ), $tracking_code ) ); 
  51.  
  52. return true; 
  53.  
  54. return false;