wc_update_200_line_items

The WooCommerce wc update 200 line items function.

Description

wc_update_200_line_items(); 

Usage

  1. if ( !function_exists( 'wc_update_200_line_items' ) ) { 
  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_200_line_items(); 
  7.  

Defined (1)

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

/includes/wc-update-functions.php  
  1. function wc_update_200_line_items() { 
  2. global $wpdb; 
  3.  
  4. // Now its time for the massive update to line items - move them to the new DB tables 
  5. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old' 
  6. $order_item_rows = $wpdb->get_results( " 
  7. SELECT meta_value, post_id FROM {$wpdb->postmeta} 
  8. WHERE meta_key = '_order_items' 
  9. " ); 
  10.  
  11. foreach ( $order_item_rows as $order_item_row ) { 
  12.  
  13. $order_items = (array) maybe_unserialize( $order_item_row->meta_value ); 
  14.  
  15. foreach ( $order_items as $order_item ) { 
  16.  
  17. if ( ! isset( $order_item['line_total'] ) && isset( $order_item['taxrate'] ) && isset( $order_item['cost'] ) ) { 
  18. $order_item['line_tax'] = number_format( ( $order_item['cost'] * $order_item['qty'] ) * ( $order_item['taxrate'] / 100 ), 2, '.', '' ); 
  19. $order_item['line_total'] = $order_item['cost'] * $order_item['qty']; 
  20. $order_item['line_subtotal_tax'] = $order_item['line_tax']; 
  21. $order_item['line_subtotal'] = $order_item['line_total']; 
  22.  
  23. $order_item['line_tax'] = isset( $order_item['line_tax'] ) ? $order_item['line_tax'] : 0; 
  24. $order_item['line_total'] = isset( $order_item['line_total'] ) ? $order_item['line_total'] : 0; 
  25. $order_item['line_subtotal_tax'] = isset( $order_item['line_subtotal_tax'] ) ? $order_item['line_subtotal_tax'] : 0; 
  26. $order_item['line_subtotal'] = isset( $order_item['line_subtotal'] ) ? $order_item['line_subtotal'] : 0; 
  27.  
  28. $item_id = wc_add_order_item( $order_item_row->post_id, array( 
  29. 'order_item_name' => $order_item['name'],  
  30. 'order_item_type' => 'line_item',  
  31. ) ); 
  32.  
  33. // Add line item meta 
  34. if ( $item_id ) { 
  35. wc_add_order_item_meta( $item_id, '_qty', absint( $order_item['qty'] ) ); 
  36. wc_add_order_item_meta( $item_id, '_tax_class', $order_item['tax_class'] ); 
  37. wc_add_order_item_meta( $item_id, '_product_id', $order_item['id'] ); 
  38. wc_add_order_item_meta( $item_id, '_variation_id', $order_item['variation_id'] ); 
  39. wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $order_item['line_subtotal'] ) ); 
  40. wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $order_item['line_subtotal_tax'] ) ); 
  41. wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $order_item['line_total'] ) ); 
  42. wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $order_item['line_tax'] ) ); 
  43.  
  44. $meta_rows = array(); 
  45.  
  46. // Insert meta 
  47. if ( ! empty( $order_item['item_meta'] ) ) { 
  48. foreach ( $order_item['item_meta'] as $key => $meta ) { 
  49. // Backwards compatibility 
  50. if ( is_array( $meta ) && isset( $meta['meta_name'] ) ) { 
  51. $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $meta['meta_name'] ) . '", "' . esc_sql( $meta['meta_value'] ) . '")'; 
  52. } else { 
  53. $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $key ) . '", "' . esc_sql( $meta ) . '")'; 
  54.  
  55. // Insert meta rows at once 
  56. if ( sizeof( $meta_rows ) > 0 ) { 
  57. $wpdb->query( $wpdb->prepare( " 
  58. INSERT INTO {$wpdb->prefix}woocommerce_order_itemmeta ( order_item_id, meta_key, meta_value ) 
  59. VALUES " . implode( ', ', $meta_rows ) . "; 
  60. ", $order_item_row->post_id ) ); 
  61.  
  62. // Delete from DB (rename) 
  63. $wpdb->query( $wpdb->prepare( " 
  64. UPDATE {$wpdb->postmeta} 
  65. SET meta_key = '_order_items_old' 
  66. WHERE meta_key = '_order_items' 
  67. AND post_id = %d 
  68. ", $order_item_row->post_id ) ); 
  69.  
  70. unset( $meta_rows, $item_id, $order_item ); 
  71.  
  72. // Do the same kind of update for order_taxes - move to lines 
  73. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old' 
  74. $order_tax_rows = $wpdb->get_results( " 
  75. SELECT meta_value, post_id FROM {$wpdb->postmeta} 
  76. WHERE meta_key = '_order_taxes' 
  77. " ); 
  78.  
  79. foreach ( $order_tax_rows as $order_tax_row ) { 
  80.  
  81. $order_taxes = (array) maybe_unserialize( $order_tax_row->meta_value ); 
  82.  
  83. if ( ! empty( $order_taxes ) ) { 
  84. foreach ( $order_taxes as $order_tax ) { 
  85.  
  86. if ( ! isset( $order_tax['label'] ) || ! isset( $order_tax['cart_tax'] ) || ! isset( $order_tax['shipping_tax'] ) ) 
  87. continue; 
  88.  
  89. $item_id = wc_add_order_item( $order_tax_row->post_id, array( 
  90. 'order_item_name' => $order_tax['label'],  
  91. 'order_item_type' => 'tax',  
  92. ) ); 
  93.  
  94. // Add line item meta 
  95. if ( $item_id ) { 
  96. wc_add_order_item_meta( $item_id, 'compound', absint( isset( $order_tax['compound'] ) ? $order_tax['compound'] : 0 ) ); 
  97. wc_add_order_item_meta( $item_id, 'tax_amount', wc_clean( $order_tax['cart_tax'] ) ); 
  98. wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_clean( $order_tax['shipping_tax'] ) ); 
  99.  
  100. // Delete from DB (rename) 
  101. $wpdb->query( $wpdb->prepare( " 
  102. UPDATE {$wpdb->postmeta} 
  103. SET meta_key = '_order_taxes_old' 
  104. WHERE meta_key = '_order_taxes' 
  105. AND post_id = %d 
  106. ", $order_tax_row->post_id ) ); 
  107.  
  108. unset( $tax_amount );