Skip to content

Commit

Permalink
Show check-in/check-out info on cart/purchases pages.
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
justinshreve committed Dec 14, 2015
1 parent 8f70717 commit b2ca32a
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions includes/class-wc-accommodation-booking-cart-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 );
}

/**
Expand All @@ -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();

0 comments on commit b2ca32a

Please sign in to comment.