Advertisement
nshelper

Untitled

Jul 24th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. /**
  2. * Expands an order item to get its data.
  3. *
  4. * @param WC_Order_item $item Order item data.
  5. * @return array
  6. */
  7. protected function get_order_item_data( $item ) {
  8.  
  9. do_action( 'woocommerce/cart_loop/start', $item );
  10.  
  11. $data = $item->get_data();
  12. $format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' );
  13.  
  14. // Format decimal values.
  15. foreach ( $format_decimal as $key ) {
  16. if ( isset( $data[ $key ] ) ) {
  17. $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
  18. }
  19. }
  20.  
  21. // Add SKU, PRICE, and IMAGE to products.
  22. if ( is_callable( array( $item, 'get_product' ) ) ) {
  23. $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null;
  24. $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0;
  25.  
  26. $image_id = $item->get_product() ? $item->get_product()->get_image_id() : 0;
  27. $data['image'] = array(
  28. 'id' => $image_id,
  29. 'src' => $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '',
  30. );
  31. }
  32.  
  33. // Add parent_name if the product is a variation.
  34. if ( is_callable( array( $item, 'get_product' ) ) ) {
  35. $product = $item->get_product();
  36.  
  37. if ( is_callable( array( $product, 'get_parent_data' ) ) ) {
  38. $data['parent_name'] = $product->get_title();
  39. } else {
  40. $data['parent_name'] = null;
  41. }
  42. }
  43.  
  44. // Format taxes.
  45. if ( ! empty( $data['taxes']['total'] ) ) {
  46. $taxes = array();
  47.  
  48. foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) {
  49. $taxes[] = array(
  50. 'id' => $tax_rate_id,
  51. 'total' => $tax,
  52. 'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '',
  53. );
  54. }
  55. $data['taxes'] = $taxes;
  56. } elseif ( isset( $data['taxes'] ) ) {
  57. $data['taxes'] = array();
  58. }
  59.  
  60. // Remove names for coupons, taxes and shipping.
  61. if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) {
  62. unset( $data['name'] );
  63. }
  64.  
  65. // Remove props we don't want to expose.
  66. unset( $data['order_id'] );
  67. unset( $data['type'] );
  68.  
  69. // Expand meta_data to include user-friendly values.
  70. $formatted_meta_data = $item->get_all_formatted_meta_data( null );
  71.  
  72. // Filter out product variations.
  73. if ( isset( $product ) && 'true' === $this->request['order_item_display_meta'] ) {
  74. $order_item_name = $data['name'];
  75. $data['meta_data'] = array_filter(
  76. $data['meta_data'],
  77. function( $meta ) use ( $product, $order_item_name ) {
  78. $display_value = wp_kses_post( rawurldecode( (string) $meta->value ) );
  79.  
  80. // Skip items with values already in the product details area of the product name.
  81. if ( $product && $product->is_type( 'variation' ) && wc_is_attribute_in_product_name( $display_value, $order_item_name ) ) {
  82. return false;
  83. }
  84.  
  85. return true;
  86. }
  87. );
  88. }
  89.  
  90. $data['meta_data'] = array_map(
  91. array( $this, 'merge_meta_item_with_formatted_meta_display_attributes' ),
  92. $data['meta_data'],
  93. array_fill( 0, count( $data['meta_data'] ), $formatted_meta_data )
  94. );
  95.  
  96. do_action( 'woocommerce/cart_loop/end', $item );
  97.  
  98. return $data;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement