Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1.  
  2. /**
  3. * Add student data to cart item.
  4. *
  5. * @param array $cart_item_data
  6. * @param int $product_id
  7. * @param int $variation_id
  8. *
  9. * @return array
  10. */
  11. function bsc_add_student_data_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
  12. $booking_student = filter_input( INPUT_POST, 'booking-student' );
  13.  
  14. if ( empty( $booking_student ) ) {
  15. return $cart_item_data;
  16. }
  17.  
  18. $cart_item_data['booking-student'] = $booking_student;
  19.  
  20. return $cart_item_data;
  21. }
  22.  
  23. add_filter( 'woocommerce_add_cart_item_data', 'bsc_add_student_data_to_cart_item', 10, 3 );
  24.  
  25. /**
  26. * Display student data text in the cart.
  27. *
  28. * @param array $item_data
  29. * @param array $cart_item
  30. *
  31. * @return array
  32. */
  33. function bsc_display_student_booking_data_in_cart( $item_data, $cart_item ) {
  34. if ( empty( $cart_item['booking-student'] ) ) {
  35. return $item_data;
  36. }
  37. $student_object = get_post( $cart_item['booking-student'] );
  38. $item_data[] = array(
  39. 'key' => __( 'Student', 'bsc-translation' ),
  40. 'value' => wc_clean($student_object->post_title),
  41. 'display' => '',
  42. );
  43.  
  44. return $item_data;
  45. }
  46.  
  47. add_filter( 'woocommerce_get_item_data', 'bsc_display_student_booking_data_in_cart', 10, 2 );
  48.  
  49. /**
  50. * Add engraving text to order.
  51. *
  52. * @param WC_Order_Item_Product $item
  53. * @param string $cart_item_key
  54. * @param array $values
  55. * @param WC_Order $order
  56. */
  57. function bsc_add_student_date_to_order_items( $item, $cart_item_key, $values, $order ) {
  58. if ( empty( $values['booking-student'] ) ) {
  59. return;
  60. }
  61.  
  62. $item->add_meta_data( __( 'booking-student', 'bsc-translation' ), $values['booking-student'] );
  63. }
  64.  
  65. add_action( 'woocommerce_checkout_create_order_line_item', 'bsc_add_student_date_to_order_items', 10, 4 );
  66.  
  67. /*
  68. * OUTPUT STUDENT NAME INSTEADOF ID IN ITEM DETAILS
  69. */
  70. function filter_woocommerce_order_item_display_meta_value( $meta_value ) {
  71. // make filter magic happen here...
  72. if ( get_post_status ( $meta_value ) ) {
  73. //return 'test';
  74. $student = get_post($meta_value);
  75. if($student->post_type == 'students'){
  76. $meta_value = $student->post_title;
  77. return $meta_value;
  78. } else {
  79. return $meta_value;
  80. }
  81. } else {
  82. return $meta_value;
  83. }
  84. };
  85. // add the filter
  86. add_filter( 'woocommerce_order_item_display_meta_value', 'filter_woocommerce_order_item_display_meta_value', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement