/** * 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); } }
/** * 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); } } } }
/** * 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; } }
/** * 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; } }