class-wc-order-item.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. class Booked_WC_Order_Item_Hooks {
  3. // changes order item meta label and value
  4. // replaces the appointmenr ID with it's date label
  5. public static function woocommerce_attribute_label($label, $name, $product=null) {
  6. if ( preg_match('~cfield_\d+$~', strtolower($label)) ) {
  7. $label = __('Form Field', 'booked-woocommerce-payments');
  8. add_filter('woocommerce_order_item_display_meta_value', array('Booked_WC_Order_Item_Hooks', 'woocommerce_order_item_display_custom_field_meta_value'), 10, 1);
  9. } else if ( BOOKED_WC_PLUGIN_PREFIX . 'appointment_cal_name' === strtolower($label) ) {
  10. $label = __('Calendar', 'booked-woocommerce-payments');
  11. } else if ( BOOKED_WC_PLUGIN_PREFIX . 'appointment_assignee_name' === strtolower($label) ) {
  12. $label = __('Booking Agent', 'booked-woocommerce-payments');
  13. }
  14. if ( $label && !strstr( BOOKED_WC_PLUGIN_PREFIX . 'appointment_id', strtolower($label) ) ) {
  15. return $label;
  16. }
  17. add_filter('woocommerce_order_item_display_meta_value', array('Booked_WC_Order_Item_Hooks', 'woocommerce_order_item_display_meta_value'), 10, 1);
  18. if ( is_admin() ) {
  19. add_filter('pre_kses', array('Booked_WC_Order_Item_Hooks', 'pre_kses'), 10, 3);
  20. }
  21. $label = __('Appointment', 'booked-woocommerce-payments');
  22. return $label;
  23. }
  24. public static function woocommerce_order_item_display_meta_value( $meta_value ) {
  25. remove_filter('woocommerce_order_item_display_meta_value', array('Booked_WC_Order_Item_Hooks', 'woocommerce_order_item_display_meta_value'), 10, 1);
  26. try {
  27. $appointment = Booked_WC_Appointment::get(intval($meta_value));
  28. } catch (Exception $e) {
  29. $appointment = false;
  30. }
  31. if ( $appointment ) {
  32. $meta_value = $appointment->timeslot_text;
  33. }
  34. return $meta_value;
  35. }
  36. public static function woocommerce_order_item_display_custom_field_meta_value( $meta_value ) {
  37. remove_filter('woocommerce_order_item_display_meta_value', array('Booked_WC_Order_Item_Hooks', 'woocommerce_order_item_display_custom_field_meta_value'), 10, 1);
  38. $separator = '--SEP--';
  39. $parts = explode($separator, $meta_value);
  40. $meta_value = $parts[0] . ':' . $parts[1];
  41. return $meta_value;
  42. }
  43. // changes the Order Item app_id to text in Back End
  44. public static function pre_kses($string, $allowed_html, $allowed_protocols) {
  45. if ( $string===__('Appointment', 'booked-woocommerce-payments') ) {
  46. return $string;
  47. }
  48. remove_filter('pre_kses', array('Booked_WC_Order_Item_Hooks', 'pre_kses'), 10, 3);
  49. $app_id = intval(strip_tags($string));
  50. if ( !$app_id ) {
  51. return $string;
  52. }
  53. try {
  54. $appointment = Booked_WC_Appointment::get($app_id);
  55. } catch (Exception $e) {
  56. $appointment = false;
  57. }
  58. if ( $appointment ) {
  59. $string = $appointment->timeslot_text;
  60. }
  61. return $string;
  62. }
  63. // change the product title on the order details page if it is assigned to a appointment
  64. public static function woocommerce_order_item_name($title, $item) {
  65. if ( isset($item['booked_wc_appointment_id']) ) {
  66. // remove product title link, so the visitors can't acess the product details page
  67. $title = preg_replace('~<a[^>]+>([^<]+)</a>~i', '$1', $title);
  68. }
  69. return $title;
  70. }
  71. }