WooCommerce Bookings, by WooCommerce
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
add_filter( 'wpo_search_product_types', function ( $types ) { $types[] = 'booking'; return $types; }, 10, 1 ); add_filter('wpo_cart_item_is_price_readonly', function ($is_readonly, $cart_item_data) { return isset($cart_item_data['booking']) ? true : $is_readonly; }, 10, 2); add_filter('wpo_cart_item_is_qty_readonly', function ($is_readonly, $cart_item_data) { return isset($cart_item_data['booking']) ? true : $is_readonly; }, 10, 2); add_filter('wpo_update_cart_cart_item_meta', function ($cart_item_meta, $item) { foreach ($_POST as $key => $value) { if (preg_match('/^wc_bookings_field_(.*)/i', $key)) { unset($_POST[$key]); } } if (isset($_POST['add-to-cart'])) { unset($_POST['add-to-cart']); } if ( isset($item['booking']) ) { $persons = array(); if (isset($item['booking']['_persons'])) { if (is_array($item['booking']['_persons'])) { if (isset($item['booking']['_persons'][0])) { $persons['wc_bookings_field_persons'] = $item['booking']['_persons'][0]; } else { foreach ($item['booking']['_persons'] as $i => $v) { $persons['wc_bookings_field_persons_' . $i] = $v; } } } else { $persons['wc_bookings_field_persons'] = $item['booking']['_persons']; } } $_POST = array_merge($_POST, $persons, array( 'wc_bookings_field_start_date_month' => $item['booking']['_month'], 'wc_bookings_field_start_date_day' => $item['booking']['_day'], 'wc_bookings_field_start_date_year' => $item['booking']['_year'], 'wc_bookings_field_duration' => isset($item['booking']['_duration']) && $item['booking']['_duration'] > 0 ? $item['booking']['_duration'] : null, 'wc_bookings_field_resource' => isset($item['booking']['_resource_id']) ? $item['booking']['_resource_id'] : null, 'wc_bookings_field_start_date_time' => isset($item['booking']['_time']) && $item['booking']['_time'] !== '' ? date(\DateTime::ISO8601, strtotime("{$item['booking']['_year']}-{$item['booking']['_month']}-{$item['booking']['_day']} {$item['booking']['_time']}")) : null, 'wc_bookings_field_start_date_local_timezone' => isset($item['booking']['_local_timezone']) ? $item['booking']['_local_timezone'] : null, 'add-to-cart' => $item['product_id'], )); if ( isset( $item['booking']['_booking_id'] ) ) { wp_delete_post( $item['booking']['_booking_id'], true ); } } return array_merge($cart_item_meta, array( 'booking' => isset($item['booking']) ? $item['booking'] : null, )); }, 10, 2); add_filter('wpo_update_cart_loaded_product', function ($loaded_product, $item) { return array_merge($loaded_product, array( 'booking' => isset($item['booking']) ? $item['booking'] : null, 'readonly_price' => isset($item['booking']) ? $item['data']->get_price() : null, 'readonly_custom_meta_fields_html' => isset($item['booking']) ? wc_get_formatted_cart_item_data($item) : '', )); }, 10, 3); add_filter('wpo_get_item_by_product', function ($product, $cart_item_data) { return array_merge($product, array( 'booking' => isset($cart_item_data['booking']) ? $cart_item_data['booking'] : null, )); }, 10, 2); |