/**
  * trigger function.
  */
 function trigger($booking_id)
 {
     if ($booking_id) {
         $this->object = get_wc_booking($booking_id);
         if ($this->object->has_status('in-cart')) {
             return;
         }
         $this->find[] = '{product_title}';
         $this->replace[] = $this->object->get_product()->get_title();
         if ($this->object->get_order()) {
             $this->find[] = '{order_date}';
             $this->replace[] = date_i18n(wc_date_format(), strtotime($this->object->get_order()->order_date));
             $this->find[] = '{order_number}';
             $this->replace[] = $this->object->get_order()->get_order_number();
         } else {
             $this->find[] = '{order_date}';
             $this->replace[] = date_i18n(wc_date_format(), strtotime($this->object->booking_date));
             $this->find[] = '{order_number}';
             $this->replace[] = __('N/A', 'woocommerce-bookings');
         }
         if (!$this->is_enabled() || !$this->get_recipient()) {
             return;
         }
         $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
     }
 }
 /**
  * Cancel a booking.
  */
 public static function cancel_booking()
 {
     if (isset($_GET['cancel_booking']) && isset($_GET['booking_id'])) {
         $booking_id = absint($_GET['booking_id']);
         $booking = get_wc_booking($booking_id);
         $booking_can_cancel = $booking->has_status(apply_filters('woocommerce_valid_booking_statuses_for_cancel', array('unpaid', 'pending-confirmation', 'confirmed', 'paid')));
         $redirect = $_GET['redirect'];
         if ($booking->has_status('cancelled')) {
             // Already cancelled - take no action
         } elseif ($booking_can_cancel && $booking->id == $booking_id && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-bookings-cancel_booking')) {
             // Cancel the booking
             $booking->update_status('cancelled');
             WC_Cache_Helper::get_transient_version('bookings', true);
             // Message
             wc_add_notice(apply_filters('woocommerce_booking_cancelled_notice', __('Your booking was cancelled.', 'woocommerce-bookings')), apply_filters('woocommerce_booking_cancelled_notice_type', 'notice'));
             do_action('woocommerce_bookings_cancelled_booking', $booking->id);
         } elseif (!$booking_can_cancel) {
             wc_add_notice(__('Your booking can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce-bookings'), 'error');
         } else {
             wc_add_notice(__('Invalid booking.', 'woocommerce-bookings'), 'error');
         }
         if ($redirect) {
             wp_safe_redirect($redirect);
             exit;
         }
     }
 }
 /**
  * trigger function.
  *
  * @access public
  * @return void
  */
 function trigger($booking_id)
 {
     if ($booking_id) {
         $this->object = get_wc_booking($booking_id);
         if ($this->object->get_product()) {
             $this->find[] = '{product_title}';
             $this->replace[] = $this->object->get_product()->get_title();
         }
         if ($this->object->get_order()) {
             $this->find[] = '{order_date}';
             $this->replace[] = date_i18n(wc_date_format(), strtotime($this->object->get_order()->order_date));
             $this->find[] = '{order_number}';
             $this->replace[] = $this->object->get_order()->get_order_number();
             $this->recipient = $this->object->get_order()->billing_email;
         } else {
             $this->find[] = '{order_date}';
             $this->replace[] = date_i18n(wc_date_format(), strtotime($this->object->booking_date));
             $this->find[] = '{order_number}';
             $this->replace[] = __('N/A', 'woocommerce-bookings');
             if ($this->object->customer_id && ($customer = get_user_by('id', $this->object->customer_id))) {
                 $this->recipient = $customer->user_email;
             }
         }
     }
     if (!$this->is_enabled() || !$this->get_recipient()) {
         return;
     }
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
 }
 /**
  * trigger function.
  *
  * @access public
  * @return void
  */
 function trigger($booking_id, $notification_subject, $notification_message, $attachments = array())
 {
     global $woocommerce;
     if ($booking_id) {
         $this->object = get_wc_booking($booking_id);
         $this->recipient = $this->object->get_order()->billing_email;
         $this->find[] = '{product_title}';
         $this->replace[] = $this->object->get_product()->get_title();
         $this->find[] = '{order_date}';
         $this->replace[] = date_i18n(woocommerce_date_format(), strtotime($this->object->get_order()->order_date));
         $this->find[] = '{order_number}';
         $this->replace[] = $this->object->get_order()->get_order_number();
         $this->find[] = '{customer_name}';
         $this->replace[] = $this->object->get_order()->billing_first_name . ' ' . $this->object->get_order()->billing_last_name;
         $this->find[] = '{customer_first_name}';
         $this->replace[] = $this->object->get_order()->billing_first_name;
         $this->find[] = '{customer_last_name}';
         $this->replace[] = $this->object->get_order()->billing_last_name;
     }
     if (!$this->is_enabled() || !$this->get_recipient()) {
         return;
     }
     $this->heading = str_replace($this->find, $this->replace, $notification_subject);
     $this->subject = str_replace($this->find, $this->replace, $notification_subject);
     $this->notification_message = str_replace($this->find, $this->replace, $notification_message);
     $attachments = apply_filters('woocommerce_email_attachments', $attachments, $this->id, $this->object);
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $attachments);
 }
/**
 * Validate and create a new booking manually.
 *
 * @see WC_Booking::new_booking() for available $new_booking_data args
 * @param  int $product_id you are booking
 * @param  array $new_booking_data
 * @param  string $status
 * @param  boolean $exact If false, the function will look for the next available block after your start date if the date is unavailable.
 * @return mixed WC_Booking object on success or false on fail
 */
function create_wc_booking($product_id, $new_booking_data = array(), $status = 'confirmed', $exact = false)
{
    // Merge booking data
    $defaults = array('product_id' => $product_id, 'start_date' => '', 'end_date' => '', 'resource_id' => '');
    $new_booking_data = wp_parse_args($new_booking_data, $defaults);
    $product = get_product($product_id);
    $start_date = $new_booking_data['start_date'];
    $end_date = $new_booking_data['end_date'];
    $max_date = $product->get_max_date();
    // If not set, use next available
    if (!$start_date) {
        $min_date = $product->get_min_date();
        $start_date = strtotime("+{$min_date['value']} {$min_date['unit']}", current_time('timestamp'));
    }
    // If not set, use next available + block duration
    if (!$end_date) {
        $end_date = strtotime("+{$product->wc_booking_duration} {$product->wc_booking_duration_unit}", $start_date);
    }
    $searching = true;
    $date_diff = $end_date - $start_date;
    while ($searching) {
        $available_bookings = $this->product->get_available_bookings($start_date, $end_date, $new_booking_data['resource_id'], $data['_qty']);
        if ($available_bookings && !is_wp_error($available_bookings)) {
            if (!$new_booking_data['resource_id'] && is_array($available_bookings)) {
                $new_booking_data['resource_id'] = current(array_keys($available_bookings));
            }
            $searching = false;
        } else {
            if ($exact) {
                return false;
            }
            $start_date += $date_diff;
            $end_date += $date_diff;
            if ($end_date > strtotime("+{$max_date['value']} {$max_date['unit']}")) {
                return false;
            }
        }
    }
    // Set dates
    $new_booking_data['start_date'] = $start_date;
    $new_booking_data['end_date'] = $end_date;
    // Create it
    $new_booking = get_wc_booking($new_booking_data);
    $new_booking->create($status);
    return $new_booking;
}
 /**
  * Mark a booking confirmed
  */
 public function mark_booking_confirmed()
 {
     if (!current_user_can('manage_bookings')) {
         wp_die(__('You do not have sufficient permissions to access this page.', 'woocommerce-bookings'));
     }
     if (!check_admin_referer('wc-booking-confirm')) {
         wp_die(__('You have taken too long. Please go back and retry.', 'woocommerce-bookings'));
     }
     $booking_id = isset($_GET['booking_id']) && (int) $_GET['booking_id'] ? (int) $_GET['booking_id'] : '';
     if (!$booking_id) {
         die;
     }
     $booking = get_wc_booking($booking_id);
     if ($booking->get_status() !== 'confirmed') {
         $booking->update_status('confirmed');
     }
     wp_safe_redirect(wp_get_referer());
 }
 /**
  * trigger function.
  *
  * @access public
  * @return void
  */
 function trigger($booking_id)
 {
     global $woocommerce;
     if ($booking_id) {
         $this->object = get_wc_booking($booking_id);
         $this->recipient = $this->object->get_order()->billing_email;
         $this->find[] = '{product_title}';
         $this->replace[] = $this->object->get_product()->get_title();
         $this->find[] = '{order_date}';
         $this->replace[] = date_i18n(woocommerce_date_format(), strtotime($this->object->get_order()->order_date));
         $this->find[] = '{order_number}';
         $this->replace[] = $this->object->get_order()->get_order_number();
     }
     if (!$this->is_enabled() || !$this->get_recipient()) {
         return;
     }
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
 }
 /**
  * Gets bookings for a user by ID
  *
  * @param int $user_id The id of the user that we want bookings for
  * @return array of WC_Booking objects
  */
 public static function get_bookings_for_user($user_id)
 {
     $booking_statuses = apply_filters('woocommerce_bookings_for_user_statuses', array('unpaid', 'pending-confirmation', 'confirmed', 'paid', 'cancelled', 'complete'));
     $booking_ids = get_posts(array('numberposts' => -1, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'wc_booking', 'post_status' => $booking_statuses, 'fields' => 'ids', 'no_found_rows' => true, 'meta_query' => array(array('key' => '_booking_customer_id', 'value' => absint($user_id), 'compare' => 'IN'))));
     $bookings = array();
     foreach ($booking_ids as $booking_id) {
         $bookings[] = get_wc_booking($booking_id);
     }
     return $bookings;
 }
 /**
  * order_item_meta function.
  *
  * @param mixed $item_id
  * @param mixed $values
  */
 public function order_item_meta($item_id, $values)
 {
     global $wpdb;
     if (!empty($values['booking'])) {
         $product = $values['data'];
         $booking_id = $values['booking']['_booking_id'];
         $booking = get_wc_booking($booking_id);
         $booking_status = 'unpaid';
         $order_id = $wpdb->get_var($wpdb->prepare("SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id));
         // Set as pending when the booking requires confirmation
         if (wc_booking_requires_confirmation($values['product_id'])) {
             $booking_status = 'pending-confirmation';
         }
         if (!$booking) {
             $booking = $this->create_booking_from_cart_data($cart_item_meta, $product->id);
         }
         $booking->set_order_id($order_id, $item_id);
         // Add summary of details to line item
         foreach ($values['booking'] as $key => $value) {
             if (strpos($key, '_') !== 0) {
                 wc_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
             }
         }
         wc_add_order_item_meta($item_id, __('Booking ID', 'woocommerce-bookings'), $values['booking']['_booking_id']);
         // Update status
         $booking->update_status($booking_status);
     }
 }
 /**
  * Output the form
  */
 public function output()
 {
     $this->errors = array();
     $step = 1;
     try {
         if (!empty($_POST) && !check_admin_referer('create_booking_notification')) {
             throw new Exception(__('Error - please try again', 'woocommerce-bookings'));
         }
         if (!empty($_POST['create_booking'])) {
             $customer_id = absint($_POST['customer_id']);
             $bookable_product_id = absint($_POST['bookable_product_id']);
             $booking_order = wc_clean($_POST['booking_order']);
             if (!$bookable_product_id) {
                 throw new Exception(__('Please choose a bookable product', 'woocommerce-bookings'));
             }
             if ($booking_order === 'existing') {
                 $order_id = absint($_POST['booking_order_id']);
                 $booking_order = $order_id;
                 if (!$booking_order || get_post_type($booking_order) !== 'shop_order') {
                     throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
                 }
             }
             $step++;
             $product = get_product($bookable_product_id);
             $booking_form = new WC_Booking_Form($product);
         } elseif (!empty($_POST['create_booking_2'])) {
             $customer_id = absint($_POST['customer_id']);
             $bookable_product_id = absint($_POST['bookable_product_id']);
             $booking_order = wc_clean($_POST['booking_order']);
             $product = get_product($bookable_product_id);
             $booking_form = new WC_Booking_Form($product);
             $booking_data = $booking_form->get_posted_data($_POST);
             $booking_cost = ($cost = $booking_form->calculate_booking_cost($_POST)) && !is_wp_error($cost) ? number_format($cost, 2, '.', '') : 0;
             $create_order = false;
             if ('yes' === get_option('woocommerce_prices_include_tax')) {
                 if (version_compare(WOOCOMMERCE_VERSION, '2.3', '<')) {
                     $base_tax_rates = WC_Tax::get_shop_base_rate($product->tax_class);
                 } else {
                     $base_tax_rates = WC_Tax::get_base_tax_rates($product->tax_class);
                 }
                 $base_taxes = WC_Tax::calc_tax($booking_cost, $base_tax_rates, true);
                 $booking_cost = round($booking_cost - array_sum($base_taxes), absint(get_option('woocommerce_price_num_decimals')));
             }
             // Data to go into the booking
             $new_booking_data = array('user_id' => $customer_id, 'product_id' => $product->id, 'resource_id' => isset($booking_data['_resource_id']) ? $booking_data['_resource_id'] : '', 'persons' => $booking_data['_persons'], 'cost' => $booking_cost, 'start_date' => $booking_data['_start_date'], 'end_date' => $booking_data['_end_date'], 'all_day' => $booking_data['_all_day'] ? 1 : 0);
             // Create order
             if ($booking_order === 'new') {
                 $create_order = true;
                 $order_id = $this->create_order($booking_cost, $customer_id);
                 if (!$order_id) {
                     throw new Exception(__('Error: Could not create order', 'woocommerce-bookings'));
                 }
             } elseif ($booking_order > 0) {
                 $order_id = absint($booking_order);
                 if (!$order_id || get_post_type($order_id) !== 'shop_order') {
                     throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
                 }
                 $order = new WC_Order($order_id);
                 update_post_meta($order_id, '_order_total', $order->get_total() + $booking_cost);
                 update_post_meta($order_id, '_booking_order', '1');
             } else {
                 $order_id = 0;
             }
             if ($order_id) {
                 $item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product->get_title(), 'order_item_type' => 'line_item'));
                 if (!$item_id) {
                     throw new Exception(__('Error: Could not create item', 'woocommerce-bookings'));
                 }
                 // Add line item meta
                 woocommerce_add_order_item_meta($item_id, '_qty', 1);
                 woocommerce_add_order_item_meta($item_id, '_tax_class', $product->get_tax_class());
                 woocommerce_add_order_item_meta($item_id, '_product_id', $product->id);
                 woocommerce_add_order_item_meta($item_id, '_variation_id', '');
                 woocommerce_add_order_item_meta($item_id, '_line_subtotal', $booking_cost);
                 woocommerce_add_order_item_meta($item_id, '_line_total', $booking_cost);
                 woocommerce_add_order_item_meta($item_id, '_line_tax', 0);
                 woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', 0);
                 // We have an item id
                 $new_booking_data['order_item_id'] = $item_id;
                 // Add line item data
                 foreach ($booking_data as $key => $value) {
                     if (strpos($key, '_') !== 0) {
                         woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
                     }
                 }
             }
             // Create the booking itself
             $new_booking = get_wc_booking($new_booking_data);
             $new_booking->create($create_order ? 'unpaid' : 'pending-confirmation');
             wp_safe_redirect(admin_url('post.php?post=' . ($create_order ? $order_id : $new_booking->id) . '&action=edit'));
             exit;
         }
     } catch (Exception $e) {
         $this->errors[] = $e->getMessage();
     }
     switch ($step) {
         case 1:
             include 'views/html-create-booking-page.php';
             break;
         case 2:
             include 'views/html-create-booking-page-2.php';
             break;
     }
 }
 /**
  * Remove/cancel the booking in Google Calendar
  *
  * @param  int $booking_id Booking ID
  *
  * @return void
  */
 public function remove_booking($booking_id)
 {
     $event_id = get_post_meta($booking_id, '_wc_bookings_gcalendar_event_id', true);
     if ($event_id) {
         $booking = get_wc_booking($booking_id);
         $api_url = $this->calendars_uri . $this->calendar_id . '/events/' . $event_id;
         $access_token = $this->get_access_token();
         $params = array('method' => 'DELETE', 'sslverify' => false, 'timeout' => 60, 'headers' => array('Authorization' => 'Bearer ' . $access_token));
         if ('yes' == $this->debug) {
             $this->log->add($this->id, 'Removing booking #' . $booking->id . ' with Google Calendar...');
         }
         $response = wp_remote_post($api_url, $params);
         if (!is_wp_error($response) && 204 == $response['response']['code']) {
             if ('yes' == $this->debug) {
                 $this->log->add($this->id, 'Booking removed successfully!');
             }
             // Remove event ID
             delete_post_meta($booking->id, '_wc_bookings_gcalendar_event_id');
         } else {
             if ('yes' == $this->debug) {
                 $this->log->add($this->id, 'Error while removing the booking #' . $booking->id . ': ' . print_r($response, true));
             }
         }
     }
 }
 /**
  * Send emails that matches the provided triggers to the queue
  * @param int $booking_id
  * @param array $triggers
  */
 private function create_email_order($booking_id, $triggers = array())
 {
     /**
      * @var $booking WC_Booking
      * @var $order WC_Order
      */
     $booking = get_wc_booking($booking_id);
     $last_status = get_post_meta($booking_id, '_last_status', true);
     $order = WC_FUE_Compatibility::wc_get_order($booking->order_id);
     $emails = fue_get_emails('wc_bookings', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN'))));
     foreach ($emails as $email) {
         if (!empty($email->meta['bookings_last_status']) && $email->meta['bookings_last_status'] != $last_status) {
             continue;
         }
         if ($this->is_category_excluded($booking, $email)) {
             continue;
         }
         // A booking can have no order linked to it
         if ($order) {
             $customer = fue_get_customer_from_order($order);
             if (Follow_Up_Emails::instance()->fue_wc->wc_scheduler->exclude_customer_based_on_purchase_history($customer, $email)) {
                 continue;
             }
         }
         if ($email->interval_type == 'before_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_start', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start - $time;
         } elseif ($email->interval_type == 'after_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_end', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start + $time;
         } else {
             $send_on = $email->get_send_timestamp();
         }
         $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $booking->product_id, 'order_id' => $booking->order_id, 'meta' => array('booking_id' => $booking_id));
         if ($order) {
             $user_id = WC_FUE_Compatibility::get_order_user_id($order);
             if ($user_id) {
                 $user = new WP_User($user_id);
                 $insert['user_id'] = $user_id;
                 $insert['user_email'] = $user->user_email;
             }
         }
         // Remove the nonce to avoid infinite loop because doing a
         // remove_action on WC_Bookings_Details_Meta_Box doesnt work
         unset($_POST['wc_bookings_details_meta_box_nonce']);
         if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
             // Tell FUE that an email order has been created
             // to stop it from sending storewide emails
             if (!defined('FUE_ORDER_CREATED')) {
                 define('FUE_ORDER_CREATED', true);
             }
         }
     }
 }
 /**
  * Remove inactive booking
  */
 public function remove_inactive_booking_from_cart($booking_id)
 {
     if ($booking_id && ($booking = get_wc_booking($booking_id)) && $booking->has_status('in-cart')) {
         wp_delete_post($booking_id);
     }
 }
 /**
  * Output the form
  */
 public function output()
 {
     global $woocommerce;
     $this->errors = array();
     $step = 1;
     try {
         if (!empty($_POST) && !check_admin_referer('create_booking_notification')) {
             throw new Exception(__('Error - please try again', 'woocommerce-bookings'));
         }
         if (!empty($_POST['create_booking'])) {
             $customer_id = absint($_POST['customer_id']);
             $bookable_product_id = absint($_POST['bookable_product_id']);
             $create_order = isset($_POST['create_order']) ? 1 : 0;
             if (!$bookable_product_id) {
                 throw new Exception(__('Please choose a bookable product', 'woocommerce-bookings'));
             }
             $step++;
             $product = get_product($bookable_product_id);
             $booking_form = new WC_Booking_Form($product);
         } elseif (!empty($_POST['create_booking_2'])) {
             $customer_id = absint($_POST['customer_id']);
             $bookable_product_id = absint($_POST['bookable_product_id']);
             $create_order = absint($_POST['create_order']);
             $product = get_product($bookable_product_id);
             $booking_form = new WC_Booking_Form($product);
             $booking_data = $booking_form->get_posted_data($_POST);
             $booking_cost = number_format($booking_form->calculate_booking_cost($_POST), 2, '.', '');
             // Data to go into the booking
             $new_booking_data = array('product_id' => $product->id, 'resource_id' => isset($booking_data['_resource_id']) ? $booking_data['_resource_id'] : '', 'persons' => $booking_data['_persons'], 'cost' => $booking_cost, 'start_date' => $booking_data['_start_date'], 'end_date' => $booking_data['_end_date'], 'all_day' => $booking_data['_all_day'] ? 1 : 0);
             // Create order
             if ($create_order) {
                 $order_id = $this->create_order($booking_cost, $customer_id);
                 if (!$order_id) {
                     throw new Exception(__('Error: Could not create order', 'woocommerce-bookings'));
                 }
                 $item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product->get_title(), 'order_item_type' => 'line_item'));
                 if (!$item_id) {
                     throw new Exception(__('Error: Could not create item', 'woocommerce-bookings'));
                 }
                 // Add line item meta
                 woocommerce_add_order_item_meta($item_id, '_qty', 1);
                 woocommerce_add_order_item_meta($item_id, '_tax_class', $product->get_tax_class());
                 woocommerce_add_order_item_meta($item_id, '_product_id', $product->id);
                 woocommerce_add_order_item_meta($item_id, '_variation_id', '');
                 woocommerce_add_order_item_meta($item_id, '_line_subtotal', $booking_cost);
                 woocommerce_add_order_item_meta($item_id, '_line_total', $booking_cost);
                 woocommerce_add_order_item_meta($item_id, '_line_tax', 0);
                 woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', 0);
                 // We have an item id
                 $new_booking_data['order_item_id'] = $item_id;
                 // Add line item data
                 foreach ($booking_data as $key => $value) {
                     if (strpos($key, '_') !== 0) {
                         woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
                     }
                 }
             }
             // Create the booking itself
             $new_booking = get_wc_booking($new_booking_data);
             $new_booking->create($create_order ? 'unpaid' : 'pending');
             wp_safe_redirect(admin_url('post.php?post=' . ($create_order ? $order_id : $new_booking->id) . '&action=edit'));
             exit;
         }
     } catch (Exception $e) {
         $this->errors[] = $e->getMessage();
     }
     switch ($step) {
         case 1:
             include 'views/html-create-booking-page.php';
             break;
         case 2:
             include 'views/html-create-booking-page-2.php';
             break;
     }
 }
 /**
  * Removes the booking from an order
  * when the order includes only bookings which require confirmation
  *
  * @param int $booking_id
  */
 public function remove_cancelled_booking($booking_id)
 {
     global $wpdb;
     $booking = get_wc_booking($booking_id);
     $order = $booking->get_order();
     $bookings = array();
     foreach ($order->get_items() as $order_item_id => $item) {
         if ($item[__('Booking ID', 'woocommerce-bookings')] == $booking_id) {
             wc_delete_order_item($order_item_id);
             $order->calculate_totals();
             $order->add_order_note(sprintf(__('The product %s has been removed from the order because the booking #%d cannot be confirmed.', 'woocommerce-bookings'), $item['name'], $booking_id), true);
         }
     }
 }
 /**
  * Change the booking status
  */
 public function mark_booking_complete($booking_id)
 {
     $booking = get_wc_booking($booking_id);
     $booking->update_status('complete');
 }
 /**
  * Cancel bookings with order
  * @param  int $order_id
  */
 public function cancel_bookings($order_id)
 {
     global $wpdb;
     $order = new WC_Order($order_id);
     $bookings = array();
     // Prevents infinite loop during synchronization
     update_post_meta($order_id, '_booking_status_sync', true);
     foreach ($order->get_items() as $order_item_id => $item) {
         if ('line_item' == $item['type']) {
             $bookings = array_merge($bookings, $wpdb->get_col($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_booking_order_item_id' AND meta_value = %d", $order_item_id)));
         }
     }
     foreach ($bookings as $booking_id) {
         if (get_post_meta($booking_id, '_booking_status_sync', true)) {
             continue;
         }
         $booking = get_wc_booking($booking_id);
         $booking->update_status('cancelled');
     }
     delete_post_meta($order_id, '_booking_status_sync');
 }
 /**
  * Define our custom columns shown in admin.
  * @param  string $column
  */
 public function custom_columns($column)
 {
     global $post, $booking;
     if (empty($booking) || $booking->id != $post->ID) {
         $booking = get_wc_booking($post->ID);
     }
     switch ($column) {
         case 'booking_status':
             echo $booking->get_status(false);
             break;
         case 'booking_id':
             printf('<a href="%s">' . __('Booking #%d', 'woocommerce-bookings') . '</a>', admin_url('post.php?post=' . $post->ID . '&action=edit'), $post->ID);
             break;
         case 'customer':
             $customer = $booking->get_customer();
             if ($customer) {
                 echo '<a href="mailto:' . $customer->email . '">' . $customer->name . '</a>';
             } else {
                 echo '-';
             }
             break;
         case 'booked_product':
             $product = $booking->get_product();
             $resource = $booking->get_resource();
             if ($product) {
                 echo '<a href="' . admin_url('post.php?post=' . $product->id . '&action=edit') . '">' . $product->post->post_title . '</a>';
                 if ($resource) {
                     echo ' (<a href="' . admin_url('post.php?post=' . $resource->ID . '&action=edit') . '">' . $resource->post_title . '</a>)';
                 }
             } else {
                 echo '-';
             }
             break;
         case 'order':
             $order = $booking->get_order();
             if ($order) {
                 echo '<a href="' . admin_url('post.php?post=' . $order->id . '&action=edit') . '">#' . $order->get_order_number() . '</a> - ' . esc_html($order->status);
             } else {
                 echo '-';
             }
             break;
         case 'start_date':
             echo $booking->get_start_date();
             break;
         case 'end_date':
             echo $booking->get_end_date();
             break;
         case 'booking_actions':
             echo '<p>';
             $actions = array();
             $actions['view'] = array('url' => admin_url('post.php?post=' . $post->ID . '&action=edit'), 'name' => __('View', 'woocommerce-bookings'), 'action' => "view");
             if (in_array($booking->get_status(), array('pending-confirmation'))) {
                 $actions['confirm'] = array('url' => wp_nonce_url(admin_url('admin-ajax.php?action=wc-booking-confirm&booking_id=' . $post->ID), 'wc-booking-confirm'), 'name' => __('Confirm', 'woocommerce-bookings'), 'action' => "confirm");
             }
             $actions = apply_filters('woocommerce_admin_booking_actions', $actions, $booking);
             foreach ($actions as $action) {
                 printf('<a class="button tips %s" href="%s" data-tip="%s">%s</a>', esc_attr($action['action']), esc_url($action['url']), esc_attr($action['name']), esc_attr($action['name']));
             }
             echo '</p>';
             break;
     }
 }
 /**
  * order_item_meta function.
  *
  * @param mixed $item_id
  * @param mixed $values
  */
 public function order_item_meta($item_id, $values)
 {
     if (!empty($values['booking'])) {
         $product = $values['data'];
         // Create the new booking
         $new_booking_data = array('order_item_id' => $item_id, 'product_id' => $values['product_id'], 'cost' => $values['booking']['_cost'], 'start_date' => $values['booking']['_start_date'], 'end_date' => $values['booking']['_end_date'], 'all_day' => $values['booking']['_all_day']);
         // Check if the booking has resources
         if (isset($values['booking']['_resource_id'])) {
             $new_booking_data['resource_id'] = $values['booking']['_resource_id'];
             // ID of the resource
         }
         // Checks if the booking allows persons
         if (isset($values['booking']['_persons'])) {
             $new_booking_data['persons'] = $values['booking']['_persons'];
             // Count of persons making booking
         }
         $booking_status = 'unpaid';
         // Set as pending when the booking requires confirmation
         if (wc_booking_requires_confirmation($values['product_id'])) {
             $booking_status = 'pending';
         }
         $new_booking = get_wc_booking($new_booking_data);
         $new_booking->create($booking_status);
         // Add summary of details to line item
         foreach ($values['booking'] as $key => $value) {
             if (strpos($key, '_') !== 0) {
                 woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
             }
         }
     }
 }
示例#20
0
function print_orders_pedal()
{
    global $post;
    $args = array('post_type' => 'shop_order', 'post_status' => 'publish', 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => array('unpaid'))));
    $orders = get_posts($args);
    $i = 0;
    $out = array();
    foreach ($orders as $o) {
        if ($i > 10) {
            break;
        }
        $order_id = $o->ID;
        $order = new WC_Order($order_id);
        $list = $order->get_items();
        $booking = get_wc_booking('86');
        print_r($booking);
        foreach ($order->get_items() as $item) {
            if ($i > 10) {
                break;
            }
            $i++;
            $date = $item['Booking Date'];
            $time = $item['Booking Time'];
            $unix_start = $item['product_id'];
            $child_age = $item['Child Details - Child\'s Age'];
            $child = $item['Child Details - Child\'s Name'];
            // use for date and time filters
            //$myString = "Hello, world!";
            //echo strstr( $myString, "wor" );                    // Displays 'world!'
            //echo ( strstr( $myString, "xyz" ) ? "Yes" : "No" ); // Displays 'No'
            if (strstr($time, '9:30')) {
                $out[] = '  <div class="wc-upcoming-booking">
            <div class="wc-upcoming-time">
                <span class="upcoming-hour">' . $time . '</span>
                <span class="upcoming-date">' . $date . '</span>
                <span class="upcoming-date">' . strtotime($date) . '</span>
            </div>
            <div class="wc-upcoming-details">
                ' . $child . ', Age: ' . $child_age . '
            </div>
        </div>';
            }
        }
    }
    wp_reset_postdata();
    echo implode($out);
}