From b2ca32a35a0b75f838a24fa44f81f410159eb2f4 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Mon, 14 Dec 2015 14:07:45 -0800 Subject: [PATCH] Show check-in/check-out info on cart/purchases pages. Fixes https://github.com/woothemes/woocommerce-accommodation-bookings/issues/3 --- ...-wc-accommodation-booking-cart-manager.php | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-accommodation-booking-cart-manager.php b/includes/class-wc-accommodation-booking-cart-manager.php index 0c118d87..f963e84d 100644 --- a/includes/class-wc-accommodation-booking-cart-manager.php +++ b/includes/class-wc-accommodation-booking-cart-manager.php @@ -3,8 +3,9 @@ exit; /** - * We need the normal WooCommerce Booking "add to cart" method to fire when our - * accommodation method does. + * We need to change a couple things about how the cart manager works: + * - The add-to-cart action for accommodation bookings should just call the booking action + * - We should display check-in/check-out times on the cart */ class WC_Accommodation_Booking_Cart_Manager { @@ -13,6 +14,7 @@ class WC_Accommodation_Booking_Cart_Manager { */ public function __construct() { add_action( 'woocommerce_accommodation-booking_add_to_cart', array( $this, 'add_to_cart' ), 30 ); + add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 20, 2 ); } /** @@ -22,6 +24,39 @@ function add_to_cart() { do_action( 'woocommerce_booking_add_to_cart' ); } + /** + * Display our check-in/check-out info + * + * @param mixed $other_data + * @param mixed $cart_item + * @return array + */ + public function get_item_data( $other_data, $cart_item ) { + if ( 'accommodation-booking' === $cart_item['data']->product_type ) { + $check_in = get_option( 'woocommerce_accommodation_bookings_check_in', '' ); + $check_out = get_option( 'woocommerce_accommodation_bookings_check_out', '' ); + $end_date = date_i18n( get_option( 'date_format'), strtotime( "+{$cart_item['booking']['_duration']} day", strtotime( $cart_item['booking']['date'] ) ) ); + + if ( ! empty( $check_in ) ) { + $other_data[] = array( + 'name' => __( 'Check-in', 'woocommerce-accommodation-bookings' ), + 'value' => esc_html( $cart_item['booking']['date'] . __( ' at ', 'woocommerce-accommodation-bookings' ) . date_i18n( get_option( 'time_format' ), strtotime( "Today " . $check_in ) ) ), + 'display' => '' + ); + } + + if ( ! empty( $check_out ) ) { + $other_data[] = array( + 'name' => __( 'Check-out', 'woocommerce-accommodation-bookings' ), + 'value' => $check_out, + 'display' => esc_html( $end_date . __( ' at ', 'woocommerce-accommodation-bookings' ) . date_i18n( get_option( 'time_format' ), strtotime( "Today " . $check_out ) ) ), + ); + } + } + + return $other_data; + } + } new WC_Accommodation_Booking_Cart_Manager();