Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. // SAVE the product meta to CART item (does not yet display it though)
  2. add_filter( 'woocommerce_add_cart_item_data', 'fs_save_my_custom_product_field', 10, 2 );
  3. function fs_save_my_custom_product_field( $cart_item_data, $product_id ) {
  4. $port = get_field( 'port', $product_id, true );
  5. $fs_duration_monday = get_field( 'custom_duration_monday', $product_id, true );
  6. $fs_duration_tuesday = get_field( 'custom_duration_tuesday', $product_id, true );
  7. $fs_duration_wednesday = get_field( 'custom_duration_wednesday', $product_id, true );
  8. $fs_duration_thursday = get_field( 'custom_duration_thursday', $product_id, true );
  9. $fs_duration_friday = get_field( 'custom_duration_friday', $product_id, true );
  10. $fs_duration_saturday = get_field( 'custom_duration_saturday', $product_id, true );
  11. $fs_duration_sunday = get_field( 'custom_duration_sunday', $product_id, true );
  12.  
  13. if( !empty( $port ) ) {
  14. $cart_item_data['port'] = $port;
  15.  
  16. // below statement make sure every add to cart action as unique line item
  17. $cart_item_data['unique_key'] = md5( microtime().rand() );
  18. }
  19. if( !empty( $fs_duration_monday ) ) {
  20. $cart_item_data['custom_duration_monday'] = $fs_duration_monday;
  21.  
  22. // below statement make sure every add to cart action as unique line item
  23. $cart_item_data['unique_key'] = md5( microtime().rand() );
  24. }
  25. if( !empty( $fs_duration_tuesday ) ) {
  26. $cart_item_data['custom_duration_tuesday'] = $fs_duration_tuesday;
  27.  
  28. // below statement make sure every add to cart action as unique line item
  29. $cart_item_data['unique_key'] = md5( microtime().rand() );
  30. }
  31. if( !empty( $fs_duration_wednesday ) ) {
  32. $cart_item_data['custom_duration_wednesday'] = $fs_duration_wednesday;
  33.  
  34. // below statement make sure every add to cart action as unique line item
  35. $cart_item_data['unique_key'] = md5( microtime().rand() );
  36. }
  37. if( !empty( $fs_duration_thursday ) ) {
  38. $cart_item_data['custom_duration_thursday'] = $fs_duration_thursday;
  39.  
  40. // below statement make sure every add to cart action as unique line item
  41. $cart_item_data['unique_key'] = md5( microtime().rand() );
  42. }
  43. if( !empty( $fs_duration_friday ) ) {
  44. $cart_item_data['custom_duration_friday'] = $fs_duration_friday;
  45.  
  46. // below statement make sure every add to cart action as unique line item
  47. $cart_item_data['unique_key'] = md5( microtime().rand() );
  48. }
  49. if( !empty( $fs_duration_saturday ) ) {
  50. $cart_item_data['custom_duration_saturday'] = $fs_duration_saturday;
  51.  
  52. // below statement make sure every add to cart action as unique line item
  53. $cart_item_data['unique_key'] = md5( microtime().rand() );
  54. }
  55. if( !empty( $fs_duration_sunday ) ) {
  56. $cart_item_data['custom_duration_sunday'] = $fs_duration_sunday;
  57.  
  58. // below statement make sure every add to cart action as unique line item
  59. $cart_item_data['unique_key'] = md5( microtime().rand() );
  60. }
  61. return $cart_item_data;
  62. }
  63.  
  64. add_filter( 'woocommerce_get_item_data', 'fs_woocommerce_get_item_data', 11, 2 );
  65. function fs_woocommerce_get_item_data( $item_data, $cart_item ) {
  66. $product_id = isset( $cart_item[ 'product_id' ] ) ? $cart_item[ 'product_id' ] : 0;
  67. if ( YITH_WCBK_Product_Post_Type_Admin::is_booking( $product_id ) ) {
  68. /** @var WC_Product_Booking $product */
  69. $product = wc_get_product( $product_id );
  70.  
  71. $fs_duration_monday = $cart_item['custom_duration_monday'];
  72. $fs_duration_tuesday = $cart_item['custom_duration_tuesday'];
  73. $fs_duration_wednesday = $cart_item['custom_duration_wednesday'];
  74. $fs_duration_thursday = $cart_item['custom_duration_thursday'];
  75. $fs_duration_friday = $cart_item['custom_duration_friday'];
  76. $fs_duration_saturday = $cart_item['custom_duration_saturday'];
  77. $fs_duration_sunday = $cart_item['custom_duration_sunday'];
  78.  
  79. $booking_data = $cart_item[ 'yith_booking_data' ];
  80.  
  81. $from = $booking_data[ 'from' ];
  82. $to = $booking_data[ 'to' ];
  83. $duration = $booking_data[ 'duration' ];
  84. $has_time = in_array( $product->get_duration_unit(), array( 'hour', 'minute' ) );
  85. $date_format = wc_date_format();
  86. $date_format .= $has_time ? ' ' . wc_time_format() : '';
  87.  
  88. // check current day of the Booking Date and see if it matches a particular day of week
  89. $fs_date_day = date_i18n( 'l', $from );
  90. // if it does, then force change the Duration display to match the length that was set in the settings
  91. if( $fs_date_day === 'Monday' && $fs_duration_monday) {
  92. $duration = $fs_duration_monday;
  93. $duration_display = $duration . ' hours';
  94. }
  95. elseif( $fs_date_day === 'Tuesday' && $fs_duration_tuesday) {
  96. $duration = $fs_duration_tuesday;
  97. $duration_display = $duration . ' hours';
  98. }
  99. elseif( $fs_date_day === 'Wednesday' && $fs_duration_wednesday) {
  100. $duration = $fs_duration_wednesday;
  101. $duration_display = $duration . ' hours';
  102. }
  103. elseif( $fs_date_day === 'Thursday' && $fs_duration_thursday) {
  104. $duration = $fs_duration_thursday;
  105. $duration_display = $duration . ' hours';
  106. }
  107. elseif( $fs_date_day === 'Friday' && $fs_duration_friday ) {
  108. $duration = $fs_duration_friday;
  109. $duration_display = $duration . ' hours';
  110. }
  111. elseif( $fs_date_day === 'Saturday' && $fs_duration_saturday) {
  112. $duration = $fs_duration_saturday;
  113. $duration_display = $duration . ' hours';
  114. }
  115. elseif( $fs_date_day === 'Sunday' && $fs_duration_sunday) {
  116. $duration = $fs_duration_sunday;
  117. $duration_display = $duration . ' hours';
  118. }
  119. // otherwise fallback to the default setting from YITH
  120. else {
  121. $duration_display = yith_wcbk_format_duration( $duration, $product->get_duration_unit() );
  122. }
  123.  
  124. // get the original from date again
  125. $fs_from = $booking_data[ 'from' ];
  126. $fs_duration_minutes = $duration * 60;
  127. // get the custom end date by adding new duration in minutes
  128. $fs_until_new = strtotime('+'. $fs_duration_minutes .' minutes', $fs_from);
  129.  
  130. $booking_item_data = array(
  131. 'yith_booking_from' => array(
  132. //'key' => yith_wcbk_get_booking_meta_label( 'from' ),
  133. 'key' => 'Cruising on',
  134. 'value' => $from,
  135. 'display' => date_i18n( 'l, F, j Y', $from ) .' from '. date_i18n( wc_time_format(), $from ) .' to '. date_i18n( wc_time_format(), $fs_until_new )
  136. ),
  137. 'yith_booking_to' => array(
  138. 'key' => yith_wcbk_get_booking_meta_label( 'to' ),
  139. 'value' => $to,
  140. 'display' => date_i18n( $date_format, $to )
  141. ),
  142. 'yith_booking_duration' => array(
  143. 'key' => yith_wcbk_get_booking_meta_label( 'duration' ),
  144. 'value' => $duration,
  145. 'display' => $duration_display
  146. )
  147. );
  148.  
  149. $item_data = array_merge( $item_data, $booking_item_data );
  150. }
  151.  
  152. return $item_data;
  153. }
  154.  
  155. /**
  156. * get booking details from booking data
  157. *
  158. * @param array $booking_data
  159. * @param int $product_id
  160. *
  161. * @return array
  162. *
  163. * @since 2.0.6
  164. */
  165. public function get_booking_order_item_details( $booking_data, $product_id = 0 ) {
  166. $details = array();
  167. foreach ( $booking_data as $booking_data_key => $booking_data_value ) {
  168. $this_title = yith_wcbk_get_booking_meta_label( $booking_data_key );
  169.  
  170. switch ( $booking_data_key ) {
  171. case 'person_types':
  172. if ( is_array( $booking_data_value ) ) {
  173. foreach ( $booking_data_value as $person_type ) {
  174. if ( !empty( $person_type[ 'number' ] ) ) {
  175. $details[] = array(
  176. 'key' => $person_type[ 'title' ],
  177. 'value' => $person_type[ 'number' ],
  178. );
  179. }
  180.  
  181. }
  182. }
  183. break;
  184. case 'booking_services':
  185. if ( is_array( $booking_data_value ) ) {
  186. $booking_services = array();
  187. $hidden_booking_services = array();
  188. foreach ( $booking_data_value as $service ) {
  189. if ( !$service[ 'hidden' ] ) {
  190. $booking_services[] = $service[ 'title' ];
  191. } else {
  192. $hidden_booking_services[] = $service[ 'title' ];
  193. }
  194. }
  195. if ( !!$booking_services ) {
  196. $details[] = array(
  197. 'key' => yith_wcbk_get_label( 'booking-services' ),
  198. 'value' => yith_wcbk_booking_services_html( $booking_services ),
  199. );
  200. }
  201. if ( !!$hidden_booking_services ) {
  202. $details[] = array(
  203. 'key' => '_hidden_booking_services',
  204. 'value' => yith_wcbk_booking_services_html( $hidden_booking_services ),
  205. );
  206. }
  207. }
  208. break;
  209.  
  210. case 'from':
  211. case 'to':
  212. /** @var WC_Product_Booking $product */
  213. $product = wc_get_product( $product_id );
  214. $date_format = wc_date_format();
  215. if ( $product && $product->is_type( 'booking' ) && $product->has_time() ) {
  216. $date_format .= ' ' . wc_time_format();
  217. }
  218. $this_value = date_i18n( $date_format, $booking_data_value );
  219. $details[] = array(
  220. 'key' => $this_title,
  221. 'value' => $this_value,
  222. );
  223. break;
  224. case 'duration':
  225. $this_value = $booking_data_value;
  226. $product = wc_get_product( $product_id );
  227. if ( $product && $product instanceof WC_Product_Booking ) {
  228. $duration_unit = $product->get_duration_unit();
  229. $duration_unit_label = yith_wcbk_get_duration_unit_label( $duration_unit, absint( $booking_data_value ) );
  230. $this_value .= ' ' . $duration_unit_label;
  231. }
  232. $details[] = array(
  233. 'key' => $this_title,
  234. 'value' => $this_value,
  235. );
  236. break;
  237. default:
  238. $details[] = array(
  239. 'key' => $this_title,
  240. 'value' => $booking_data_value,
  241. );
  242. break;
  243. }
  244. }
  245.  
  246. return apply_filters( 'yith_wcbk_order_get_booking_order_item_details', $details, $booking_data, $product_id );
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement