wc_update_240_refunds

The WooCommerce wc update 240 refunds function.

Description

wc_update_240_refunds(); 

Usage

  1. if ( !function_exists( 'wc_update_240_refunds' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-update-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_update_240_refunds(); 
  7.  

Defined (1)

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

/includes/wc-update-functions.php  
  1. function wc_update_240_refunds() { 
  2. global $wpdb; 
  3. /** 
  4. * Refunds for full refunded orders. 
  5. * Update fully refunded orders to ensure they have a refund line item so reports add up. 
  6. */ 
  7. $refunded_orders = get_posts( array( 
  8. 'posts_per_page' => -1,  
  9. 'post_type' => 'shop_order',  
  10. 'post_status' => array( 'wc-refunded' ),  
  11. ) ); 
  12.  
  13. // Ensure emails are disabled during this update routine 
  14. remove_all_actions( 'woocommerce_order_status_refunded_notification' ); 
  15. remove_all_actions( 'woocommerce_order_partially_refunded_notification' ); 
  16. remove_action( 'woocommerce_order_status_refunded', array( 'WC_Emails', 'send_transactional_email' ) ); 
  17. remove_action( 'woocommerce_order_partially_refunded', array( 'WC_Emails', 'send_transactional_email' ) ); 
  18.  
  19. foreach ( $refunded_orders as $refunded_order ) { 
  20. $order_total = get_post_meta( $refunded_order->ID, '_order_total', true ); 
  21. $refunded_total = $wpdb->get_var( $wpdb->prepare( " 
  22. SELECT SUM( postmeta.meta_value ) 
  23. FROM $wpdb->postmeta AS postmeta 
  24. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d ) 
  25. WHERE postmeta.meta_key = '_refund_amount' 
  26. AND postmeta.post_id = posts.ID 
  27. ", $refunded_order->ID ) ); 
  28.  
  29. if ( $order_total > $refunded_total ) { 
  30. wc_create_refund( array( 
  31. 'amount' => $order_total - $refunded_total,  
  32. 'reason' => __( 'Order fully refunded', woocommerce ),  
  33. 'order_id' => $refunded_order->ID,  
  34. 'line_items' => array(),  
  35. 'date' => $refunded_order->post_modified,  
  36. ) ); 
  37.