'', 'pickup_location' => '', 'pickup_date' => '', 'calc_pickup_date' => '', 'return_date' => '', 'calc_return_date' => '', 'return_location_id' => '', 'return_location' => '', 'return_same' => '', 'order_days' => 0, 'order_hours' => 0, 'ceil_days' => 0, 'format' => stm_get_clear_date_format(), 'moment_format' => stm_get_clear_date_format( $date_time_format ), ); } function stm_get_cart_items() { return array( 'has_car' => false, 'option_total' => 0, 'options_list' => array(), 'car_class' => array(), 'options' => array(), 'total' => 0, 'option_ids' => array(), 'oldData' => 0, ); } function stm_rental_billing_info() { return array( 'first_name' => '', 'last_name' => '', 'email' => '', 'payment' => '', 'total' => '', ); } return; } else { require_once 'rental/PricePerHour.php'; require_once 'rental/PriceForDatePeriod.php'; if ( apply_filters( 'stm_me_get_nuxy_mod', false, 'enable_fixed_price_for_days' ) ) { require_once 'rental/PriceForQuantityDays.php'; } else { require_once 'rental/DiscountByDays.php'; } /** * Register the custom product type after init */ function stm_register_car_option_product_type() { /** * This should be in its own separate file. */ class WC_Product_Car_Option extends WC_Product_Simple { /** * Get internal type. * * @return string */ public function get_type() { return 'car_option'; } } } add_action( 'init', 'stm_register_car_option_product_type' ); function stm_save_selected_offices( $post_id ) { if ( $post_id != apply_filters( 'stm_get_wpml_product_parent_id', $post_id ) ) { $selected_offices = get_post_meta( apply_filters( 'stm_get_wpml_product_parent_id', $post_id ), 'stm_rental_office', true ); update_post_meta( $post_id, 'stm_rental_office', $selected_offices ); } } add_action( 'motors_butterbean_save_post', 'stm_save_selected_offices' ); function stm_add_car_option_product( $types ) { // Key should be exactly the same as in the class product_type parameter $types['car_option'] = __( 'Car Option', 'motors' ); return $types; } add_filter( 'product_type_selector', 'stm_add_car_option_product' ); /** * Show pricing fields for simple_rental product. */ function stm_car_option_custom_js() { if ( 'product' !== get_post_type() ) : return; endif; ?> cart ) && ! empty( WC()->cart->get_total() ) ) { return apply_filters( 'stm_rent_current_total', WC()->cart->get_total() ); } return 0; } function stm_get_cart_items() { $total_sum = stm_get_cart_current_total(); $fields = stm_get_rental_order_fields_values(); $cart = ( ! empty( WC()->cart ) && ! empty( WC()->cart->get_cart() ) ) ? WC()->cart->get_cart() : ''; $coupons = WC()->cart ? WC()->cart->get_applied_coupons() : array(); $total_discount_amount = 0; if ( ! empty( $coupons ) ) { foreach ( $coupons as $coupon ) { $coupon = new WC_Coupon( $coupon ); $discount_type = $coupon->get_discount_type(); $coupon_amount = $coupon->get_amount(); if ( 'percent' === $discount_type ) { $discount = ( floatval( $coupon_amount ) / 100 ) * WC()->cart->subtotal; } else { $discount = floatval( $coupon_amount ); } $total_discount_amount += $discount; } } $cart_items = array( 'has_car' => false, 'option_total' => 0, 'options_list' => array(), 'car_class' => array(), 'options' => array(), 'total' => $total_sum, 'option_ids' => array(), 'oldData' => 0, 'coupon_code' => $coupons, 'coupon' => $total_discount_amount, ); if ( ! empty( $cart ) ) { $cart_old_data = ( isset( $_GET['order_old_days'] ) && ! empty( intval( $_GET['order_old_days'] ) ) ) ? intval( $_GET['order_old_days'] ) : 0; foreach ( $cart as $cart_item ) { $id = apply_filters( 'stm_get_wpml_product_parent_id', $cart_item['product_id'] ); $post = $cart_item['data']; $buy_type = ( 'WC_Product_Car_Option' === get_class( $cart_item['data'] ) ) ? 'options' : 'car_class'; if ( 'options' === $buy_type ) { $cart_item_quantity = $cart_item['quantity']; if ( $cart_old_data > 0 ) { if ( 1 !== $cart_item['quantity'] ) { $cart_item_quantity = ( $cart_item['quantity'] / $cart_old_data ); } else { $cart_item_quantity = 1; } } $price_data = $cart_item['data']->get_data(); $price = $price_data['price']; if ( empty( $price ) ) { $price = 0; } if ( empty( get_post_meta( $cart_item['product_id'], '_car_option', true ) ) ) { $total = $cart_item_quantity * $price * $fields['ceil_days']; } else { $total = $cart_item_quantity * $price; } $cart_items['option_total'] += $total; $cart_items['option_ids'][] = $id; $cart_items[ $buy_type ][] = array( 'id' => $id, 'quantity' => $cart_item_quantity, 'name' => $post->get_title(), 'price' => $price, 'total' => $total, 'opt_days' => $fields['ceil_days'], 'subname' => get_post_meta( apply_filters( 'stm_get_wpml_product_child_id', $id ), 'cars_info', true ), ); $cart_items['options_list'][ $id ] = $post->get_title(); $cart_items['option_quantity'][ $id ] = $cart_item_quantity; } else { $variation_id = 0; if ( ! empty( $cart_item['variation_id'] ) ) { $variation_id = apply_filters( 'stm_get_wpml_product_parent_id', $cart_item['variation_id'] ); } if ( isset( $_GET['pickup_location'] ) ) { $pickup_location_meta = get_post_meta( $id, 'stm_rental_office' ); if ( ! in_array( $_GET['pickup_location'], explode( ',', $pickup_location_meta[0] ), true ) ) { WC()->cart->empty_cart(); } } $price_string = $cart_item['data']->get_data(); $price = $price_string['price']; if ( empty( $price ) ) { $price = 0; } $cart_items[ $buy_type ][] = array( 'id' => $id, 'variation_id' => $variation_id, 'quantity' => $cart_item['quantity'], 'name' => $post->get_title(), 'price' => $price, 'total' => (int) $fields['order_days'] * $price, // this may be changed in hook below. 'subtotal' => (int) $fields['order_days'] * $price, 'subname' => get_post_meta( apply_filters( 'stm_get_wpml_product_child_id', $id ), 'cars_info', true ), 'payment_method' => get_post_meta( $variation_id, '_stm_payment_method', true ), 'days' => (int) $fields['order_days'], 'ceil_days' => (int) $fields['ceil_days'], 'oldData' => $cart_old_data, ); $cart_items['has_car'] = true; } } /*Get only last element*/ if ( count( $cart_items['car_class'] ) > 1 ) { $rent = array_pop( $cart_items['car_class'] ); $cart_items['delete_items'] = $cart_items['car_class']; $cart_items['car_class'] = $rent; } else { if ( ! empty( $cart_items['car_class'] ) ) { $cart_items['car_class'] = $cart_items['car_class'][0]; } } } return apply_filters( 'stm_cart_items_content', $cart_items ); } /*Remove last car everytime another one added*/ add_action( 'template_redirect', 'stm_rental_remove_car_from_cart' ); function stm_rental_remove_car_from_cart() { /*This code is only for car reservation. Redirect on date reservation if not selected yet. BEGIN*/ $rental_datepick = apply_filters( 'stm_me_get_nuxy_mod', false, 'rental_datepick' ); if ( ! empty( $rental_datepick ) && is_checkout() && ! stm_check_rental_date_validation() ) { wp_safe_redirect( get_permalink( $rental_datepick ) ); exit; } /*This code is only for car reservation. Redirect on date reservation if not selected yet. END*/ $items = stm_get_cart_items(); $ids = array(); if ( ! empty( $_GET['remove-from-cart'] ) ) { $items['delete_items'][] = array( 'id' => intval( $_GET['remove-from-cart'] ), ); } if ( ! empty( $items['delete_items'] ) ) { foreach ( $items['delete_items'] as $product ) { $ids[] = $product['id']; } $woo_commerce = WC(); foreach ( $woo_commerce->cart->get_cart() as $cart_item_key => $cart_item ) { // Check to see if IDs match if ( in_array( $cart_item['product_id'], $ids, true ) ) { $woo_commerce->cart->set_quantity( $cart_item_key, 0, true ); break; } } } } /*Add quantity equal to days*/ add_action( 'template_redirect', 'stm_rental_add_quantity_to_cart' ); function stm_rental_add_quantity_to_cart() { $items = stm_get_cart_items(); $items = $items['car_class']; if ( ! empty( $items ) ) { $days = 1; if ( ! empty( $items['ceil_days'] ) ) { $days = $items['ceil_days']; } $woo_commerce = WC(); $cart = $woo_commerce->cart->get_cart(); $keys = array_keys( $cart ); $keys_count = count( $keys ); for ( $q = 0; $q < $keys_count; $q++ ) { $quant = $cart[ $keys[ $q ] ]['quantity']; if ( ( ! empty( $_GET['add-to-cart'] ) && $_GET['add-to-cart'] === $cart[ $keys[ $q ] ]['product_id'] || isset( $items['oldData'] ) && $items['oldData'] > 0 ) && 'variation' !== $cart[ $keys[ $q ] ]['data']->get_type() ) { if ( ! empty( $items['options'] ) ) { if ( $items['oldData'] > 0 ) { $quant = ( $cart[ $keys[ $q ] ]['quantity'] / $items['oldData'] ) * $days; } else { $quant = $cart[ $keys[ $q ] ]['quantity'] * $days; } } unset( $_GET['order_old_days'] ); } if ( 'car_option' === $cart[ $keys[ $q ] ]['data']->get_type() ) { $single_pay = get_post_meta( $cart[ $keys[ $q ] ]['data']->get_ID(), '_car_option', true ); if ( empty( $single_pay ) ) { $quant = $cart[ $keys[ $q ] ]['quantity']; } } if ( 'variation' === $cart[ $keys[ $q ] ]['data']->get_type() || 'simple' === $cart[ $keys[ $q ] ]['data']->get_type() ) { $quant = 1; } $woo_commerce->cart->set_quantity( $keys[ $q ], $quant, true ); } } } // Remove Car Options from main shop function stm_remove_car_options_from_query( $query ) { if ( ! is_admin() && $query->is_main_query() ) { $tax_query = array( array( 'taxonomy' => 'product_type', 'field' => 'slug', 'terms' => 'car_option', 'operator' => 'NOT IN', ), ); $query->set( 'tax_query', $tax_query ); if ( apply_filters( 'stm_is_rental_two', false ) ) { if ( ! empty( $_GET ) ) { $tax_query = array( 'relation' => 'AND', array( 'taxonomy' => 'product_type', 'field' => 'slug', 'terms' => 'car_option', 'operator' => 'NOT IN', ), ); foreach ( $_GET as $k => $val ) { if ( strpos( $k, 'filter_' ) !== false ) { $tax_query[ count( $tax_query ) ] = array( 'taxonomy' => str_replace( 'filter_', 'pa_', $k ), 'field' => 'slug', 'terms' => $val, ); } } $query->set( 'tax_query', $tax_query ); } } } $pl = 'stm_pickup_location_' . get_current_blog_id(); if ( ! is_admin() && $query->is_main_query() && isset( $_GET['pickup_location'] ) && ! stm_is_checkout( $query ) || ! is_admin() && $query->is_main_query() && ! empty( $_COOKIE[ $pl ] ) && ! stm_is_checkout( $query ) && stm_is_shop( $query ) ) { $location_id = ( isset( $_GET['pickup_location'] ) ) ? sanitize_text_field( $_GET['pickup_location'] ) : intval( $_COOKIE[ $pl ] ); $meta_query = array( array( 'key' => 'stm_rental_office', 'value' => stm_get_wpml_office_parent_id( $location_id ), 'compare' => 'LIKE', ), ); $query->set( 'meta_query', $meta_query ); } return $query; } add_action( 'pre_get_posts', 'stm_remove_car_options_from_query' ); function stm_is_shop( $query ) { $front_page_id = get_option( 'page_on_front' ); $current_page_id = $query->get( 'page_id' ); $shop_page_id = apply_filters( 'woocommerce_get_shop_page_id', get_option( 'woocommerce_shop_page_id' ) ); $is_static_front_page = 'page' === get_option( 'show_on_front' ); if ( $is_static_front_page && $front_page_id === $current_page_id ) { $is_shop_page = ( $current_page_id === $shop_page_id ); } else { $is_shop_page = is_shop(); } return $is_shop_page; } function stm_is_checkout( $query ) { $front_page_id = get_option( 'page_on_front' ); $current_page_id = $query->get( 'page_id' ); $checkout_page_id = apply_filters( 'woocommerce_checkout_page_id', get_option( 'woocommerce_checkout_page_id' ) ); $is_static_front_page = 'page' === get_option( 'show_on_front' ); if ( $is_static_front_page && $front_page_id === $current_page_id ) { $is_checkout_page = ( $current_page_id === $checkout_page_id ); } else { $is_checkout_page = is_checkout(); } return $is_checkout_page; } /*Checkout fields styling*/ add_filter( 'woocommerce_checkout_fields', 'stm_custom_override_checkout_fields' ); // Our hooked in function - $fields is passed via the filter! function stm_custom_override_checkout_fields( $fields ) { unset( $fields['billing']['billing_address_2'] ); $billing = $fields['billing']; $spliced = array_splice( $billing, 0, 2 ); $spliced['billing_driver_license'] = array( 'label' => esc_html__( 'Driver license', 'motors' ), 'required' => false, 'class' => array( 'form-row-first' ), 'autocomplete' => 'driver_license', 'priority' => '25', ); $fields['billing'] = array_merge( $spliced, $billing ); $unvalidated_fields = array( 'first_name', 'last_name', 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone', ); if ( ! empty( $fields['billing']['billing_company'] ) ) { $fields['billing']['billing_company']['class'] = array( 'form-row-last', ); } unset( $fields['billing']['billing_postcode'] ); foreach ( $fields['billing'] as $key => $field ) { $field['label_class'] = 'heading-font'; if ( ! in_array( $key, $unvalidated_fields, true ) ) { $fields['billing'][ $key ]['required'] = false; } else { $fields['billing'][ $key ]['required'] = true; } } if ( ! empty( $fields['billing']['billing_state'] ) ) { $fields['billing']['billing_state']['class'] = array( 'address-field', ); } return $fields; } add_filter( 'woocommerce_default_address_fields', 'stm_custom_override_default_address_fields' ); function stm_custom_override_default_address_fields( $fields ) { $unvalidated_fields = array( 'first_name', 'last_name', 'billing_first_name', 'billing_last_name', 'billing_email', ); foreach ( $fields as $key => $field ) { if ( ! in_array( $key, $unvalidated_fields, true ) ) { $fields[ $key ]['required'] = false; } else { $fields[ $key ]['required'] = true; } } return $fields; } add_filter( 'woocommerce_form_field_args', 'stm_fields_checkout_args' ); function stm_fields_checkout_args( $args ) { $args['label_class'] = 'heading-font'; return $args; } // Add Woocommerce variation payment gateways /** * Create new fields for variations */ function stm_variation_settings_fields( $loop, $variation_data, $variation ) { $payment_gateways = array(); $available_methods = WC()->payment_gateways; if ( ! empty( $available_methods->payment_gateways ) ) { foreach ( $available_methods->payment_gateways as $payment_gateway ) { $payment_gateways[ $payment_gateway->id ] = $payment_gateway->title; } } // Select woocommerce_wp_select( array( 'wrapper_class' => 'stm-custom-select', 'id' => '_stm_payment_method[' . $variation->ID . ']', 'label' => __( 'Availble payment method', 'motors' ), 'description' => __( 'Choose payment method available only for this variable product. If this product will be in cart, all other payment methods will be disabled on checkout page.', 'motors' ), 'value' => get_post_meta( $variation->ID, '_stm_payment_method', true ), 'options' => $payment_gateways, ) ); } add_action( 'woocommerce_product_after_variable_attributes', 'stm_variation_settings_fields', 10, 3 ); // Save Woocommerce variation payment gateway add_action( 'woocommerce_save_product_variation', 'stm_save_variation_settings_fields', 10, 2 ); function stm_save_variation_settings_fields( $post_id ) { $select = sanitize_text_field( $_POST['_stm_payment_method'][ $post_id ] ); if ( ! empty( $select ) ) { update_post_meta( $post_id, '_stm_payment_method', esc_attr( $select ) ); } } add_filter( 'woocommerce_available_payment_gateways', 'stm_filter_gateways', 1 ); function stm_filter_gateways( $gateways ) { if ( is_admin() ) { return $gateways; } $gateway = array(); $cart_items = stm_get_cart_items(); if ( ! empty( $cart_items['car_class'] ) && ! empty( $cart_items['car_class']['payment_method'] ) ) { $payment_method = $cart_items['car_class']['payment_method']; if ( ! empty( $gateways[ $payment_method ] ) ) { $gateway[ $payment_method ] = $gateways[ $payment_method ]; } } if ( ! empty( $gateway ) ) { $gateways = $gateway; } return $gateways; } function stm_rental_total_order_info() { $fields = stm_get_rental_order_fields_values(); $items = stm_get_cart_items(); $billing_fields = stm_rental_billing_info(); $order_info = array( 'pickup' => array( 'title' => esc_html__( 'Pick Up', 'motors' ), 'content' => '', ), 'dropoff' => array( 'title' => esc_html__( 'Drop off', 'motors' ), 'content' => '', ), 'vehicle' => array( 'title' => esc_html__( 'Vehicle Type', 'motors' ), 'content' => '', ), 'addons' => array( 'title' => esc_html__( 'Add-ons', 'motors' ), 'content' => '', ), 'info' => array( 'title' => esc_html__( 'Your Information', 'motors' ), 'content' => '', ), 'payment' => array( 'title' => esc_html__( 'Payment information', 'motors' ), 'content' => '', ), ); if ( ! empty( $fields['pickup_location'] ) ) { $order_info['pickup']['content'] = $fields['pickup_location'] . ' '; } if ( ! empty( $fields['pickup_date'] ) ) { $order_info['pickup']['content'] .= $fields['pickup_date']; } if ( ! empty( $fields['return_location'] ) ) { $order_info['dropoff']['content'] = $fields['return_location'] . ' '; } if ( ! empty( $fields['return_date'] ) ) { $order_info['dropoff']['content'] .= $fields['return_date']; } if ( ! empty( $items['car_class']['name'] ) ) { $order_info['vehicle']['content'] = $items['car_class']['name'] . ' '; } if ( ! empty( $items['car_class']['subname'] ) ) { $order_info['vehicle']['content'] .= $items['car_class']['subname']; } if ( ! empty( $items['options_list'] ) ) { $order_info['addons']['content'] = implode( ', ', $items['options_list'] ); } if ( ! empty( $billing_fields['first_name'] ) && ! empty( $billing_fields['last_name'] ) ) { $order_info['info']['content'] = $billing_fields['first_name'] . ' ' . $billing_fields['last_name']; } if ( ! empty( $billing_fields['total'] ) ) { $order_info['payment']['content'] = sprintf( /* translators: %s total */ esc_html__( 'Estimated Total - %s', 'motors' ), $billing_fields['total'] ); } return apply_filters( 'stm_rental_order_info', $order_info ); } function stm_rental_total_order_info_rental_two() { $fields = stm_get_rental_order_fields_values(); $items = stm_get_cart_items(); $product = new WC_Product( $items['car_class']['id'] ); $product_attrs = stm_mcr_get_product_atts( $product ); $order_info = array(); $order_info['pickup_location'] = $fields['pickup_location']; $order_info['pickup_date'] = $fields['pickup_date']; $order_info['dropoff'] = $fields['return_location']; $order_info['dropoff_date'] = $fields['return_date']; $order_info['vehicle_name'] = $items['car_class']['name']; $order_info['vehicle_img'] = wp_get_attachment_image_url( get_post_thumbnail_id( $items['car_class']['id'] ), 'stm-img-350' ); $order_info['vehicle_atts'] = $product_attrs; if ( ! empty( $items['options_list'] ) ) { $order_info['addons'] = implode( ', ', $items['options_list'] ); } return apply_filters( 'stm_rental_total_order_info_rental_two', $order_info ); } function stm_rental_order_item_info_by_id_rental_two( $order_id, $prod_id ) { $product = new WC_Product( $prod_id ); $product_attrs = stm_mcr_get_product_atts( $product ); $order_info = array(); $date_format = get_option( 'date_format' ); $time_format = get_option( 'time_format' ); $date_time_format = $date_format . ' ' . $time_format; $date1 = DateTime::createFromFormat( $date_time_format, get_post_meta( $order_id, 'order_pickup_date', true ) ); $date2 = DateTime::createFromFormat( $date_time_format, get_post_meta( $order_id, 'order_drop_date', true ) ); $diff = 0; if ( $date1 && $date2 ) { $diff = $date2->diff( $date1 )->format( '%a.%h' ); $hm = explode( '.', $diff ); if ( '0' === $hm[0] ) { $diff = 0; } } $order_info['pickup_location'] = get_post_meta( $order_id, 'order_pickup_location', true ); $order_info['pickup_date'] = get_post_meta( $order_id, 'order_pickup_date', true ); $order_info['dropoff'] = get_post_meta( $order_id, 'order_drop_location', true ); $order_info['dropoff_date'] = get_post_meta( $order_id, 'order_drop_date', true ); $order_info['vehicle_img'] = wp_get_attachment_image_url( get_post_thumbnail_id( $prod_id ), 'stm-img-350' ); $order_info['vehicle_name'] = $product->get_name(); $order_info['vehicle_atts'] = $product_attrs; $order_info['order_days'] = $diff; if ( ! empty( $items['options_list'] ) ) { $order_info['addons'] = implode( ', ', $items['options_list'] ); } return apply_filters( 'stm_rental_total_order_info_rental_two', $order_info ); } function stm_get_order_id() { $order_id = false; $order_received = get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ); $view_order = get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ); if ( isset( $_GET[ $view_order ] ) ) { $order_id = intval( $_GET[ $view_order ] ); } elseif ( isset( $_GET[ $order_received ] ) ) { $order_id = intval( $_GET[ $order_received ] ); } else { $url = apply_filters( 'stm_get_global_server_val', 'SERVER_NAME' ) . apply_filters( 'stm_get_global_server_val', 'REQUEST_URI' ); if ( strpos( $url, '/' . $order_received . '/' ) !== false ) { $start = strpos( $url, '/' . $order_received . '/' ); $first_part = substr( $url, $start + strlen( '/' . $order_received . '/' ) ); $order_id = substr( $first_part, 0, strpos( $first_part, '/' ) ); } } return $order_id; } /*Update transient of user after new order*/ function stm_rental_billing_info() { $bill = array( 'first_name' => '', 'last_name' => '', 'email' => '', 'payment' => '', 'total' => '', ); $order_id = stm_get_order_id(); if ( ! is_user_logged_in() ) { $first_name = get_post_meta( $order_id, '_billing_first_name', true ); if ( ! empty( $first_name ) ) { $bill['first_name'] = $first_name; } $last_name = get_post_meta( $order_id, '_billing_last_name', true ); if ( ! empty( $last_name ) ) { $bill['last_name'] = $last_name; } } else { $id = get_current_user_id(); $name = get_user_meta( $id, 'billing_first_name', true ); $last_name = get_user_meta( $id, 'billing_last_name', true ); if ( ! empty( $name ) && ! empty( $last_name ) ) { $bill['first_name'] = $name; $bill['last_name'] = $last_name; } } $order = wc_get_order( $order_id ); if ( ! empty( $order ) ) { if ( preg_replace( '/\D/', '', $order ) ) { $bill['total'] = wc_price( $order->get_total() ); } } $payment = get_post_meta( $order_id, '_payment_method_title', true ); if ( ! empty( $payment ) ) { $bill['payment'] = $payment; } return apply_filters( 'stm_billing_rental_info', $bill ); } // change order info when on order page add_action( 'stm_cart_items_content', 'stm_order_page_information_rental' ); function stm_order_page_information_rental( $info ) { $order_id = stm_get_order_id(); if ( ! empty( $order_id ) ) { $info_car = get_post_meta( $order_id, 'order_car', true ); if ( ! empty( $info_car['car_class'] ) ) { $info = $info_car; } } return $info; } add_action( 'stm_rental_date_values', 'stm_order_page_date_rental' ); function stm_order_page_date_rental( $date ) { $order_id = stm_get_order_id(); if ( ! empty( $order_id ) ) { $date_car = get_post_meta( $order_id, 'order_car_date', true ); if ( ! empty( $date_car ) ) { $date = $date_car; } } return $date; } /*Taxes*/ function stm_rental_order_taxes() { $taxes = array(); $order_id = stm_get_order_id(); if ( ! empty( $order_id ) ) { $order = wc_get_order( $order_id ); $order_taxes = $order->get_taxes(); foreach ( $order_taxes as $order_tax ) { $taxes[ $order_tax['label'] ] = array( 'label' => $order_tax['label'], 'value' => wc_price( $order_tax['tax_amount'] ), ); } } else { $cart = WC()->cart->get_tax_totals(); foreach ( $cart as $name => $cart_item ) { $taxes[ $name ] = array( 'label' => $cart_item->label, 'value' => $cart_item->formatted_amount, ); } } return apply_filters( 'stm_rental_order_taxes', $taxes ); } if ( apply_filters( 'stm_me_get_nuxy_mod', true, 'enable_office_location_fee' ) ) { add_filter( 'woocommerce_product_variation_get_price', 'custom_product_price', 30, 2 ); add_filter( 'woocommerce_product_get_price', 'custom_product_price', 30, 2 ); } function custom_product_price( $price, $product ) { if ( 'car_option' === $product->get_type() ) { return $price; } $order_data = stm_get_rental_order_fields_values(); if ( 'off' === $order_data['return_same'] ) { $price = $price + floatval( $order_data['return_location_fee'] ); } if ( 'on' === $order_data['return_same'] && apply_filters( 'stm_me_get_nuxy_mod', true, 'enable_fee_for_same_location' ) ) { $price = $price + floatval( $order_data['pickup_location_fee'] ); } return $price; } add_action( 'woocommerce_new_order', 'stm_order_fields' ); function stm_order_fields( $order_id ) { if ( ! is_admin() ) { $cart_items = stm_get_cart_items(); $date = stm_get_rental_order_fields_values(); update_post_meta( $order_id, 'order_car', $cart_items ); update_post_meta( $order_id, 'order_car_date', $date ); update_post_meta( $order_id, 'order_pickup_date', change_date_format( $date['calc_pickup_date'] ) ); update_post_meta( $order_id, 'order_pickup_location', $date['pickup_location'] ); update_post_meta( $order_id, 'order_drop_date', change_date_format( $date['calc_return_date'] ) ); update_post_meta( $order_id, 'order_drop_location', $date['return_location'] ); } } //adding custom order meta add_action( 'woocommerce_checkout_order_created', 'stm_add_custom_order_meta' ); function stm_add_custom_order_meta( $order ) { $date = stm_get_rental_order_fields_values(); $order->update_meta_data( 'order_pickup_date', change_date_format( $date['calc_pickup_date'] ) ); $order->update_meta_data( 'order_pickup_location', $date['pickup_location'] ); $order->update_meta_data( 'order_drop_date', change_date_format( $date['calc_return_date'] ) ); $order->update_meta_data( 'order_drop_location', $date['return_location'] ); $order->save(); } /*Remove notice when adding item to cart*/ add_filter( 'wc_add_to_cart_message_html', '__return_empty_string' ); function stm_get_car_rent_info( $id ) { $car_info_points = apply_filters( 'stm_get_car_listings', array() ); $car_info = array(); if ( ! empty( $car_info_points ) ) { foreach ( $car_info_points as $car_info_point ) { $meta = get_post_meta( $id, $car_info_point['slug'], true ); if ( ! empty( $meta ) ) { $slug = $car_info_point['slug']; $car_info[ $slug ] = array( 'name' => $car_info_point['plural_name'], 'value' => $meta, 'font' => '', 'numeric' => false, ); if ( ! empty( $car_info_point['numeric'] ) ) { $car_info[ $slug ]['numeric'] = true; } if ( ! empty( $car_info_point['font'] ) ) { $car_info[ $slug ]['font'] = $car_info_point['font']; } } } } return apply_filters( 'stm_car_rent_info', $car_info ); } function stm_rental_order_fileds() { $blog_id = get_current_blog_id(); $rents = array( 'pickup_location', 'pickup_date', 'return_date', 'drop_location', 'return_same', 'calc_pickup_date', 'calc_return_date' ); foreach ( $rents as $i => $rent ) { $rents[ $i ] = 'stm_' . $rent . '_' . $blog_id; } return $rents; } function stm_check_rental_date_validation() { $r = true; $fields = stm_get_rental_order_fields_values(); if ( stm_get_empty_placeholder() === $fields['pickup_location'] || stm_get_empty_placeholder() === $fields['pickup_date'] || stm_get_empty_placeholder() === $fields['return_date'] ) { $r = false; } return $r; } function stm_get_rental_order_fields_values( $empty = false ) { $values = array( 'pickup_location_id' => '', 'pickup_location' => '', 'pickup_date' => '', 'calc_pickup_date' => '', 'return_date' => '', 'calc_return_date' => '', 'return_location_id' => '', 'return_location' => '', 'return_same' => '', 'pickup_location_fee' => '', 'return_location_fee' => '', 'order_days' => 0, 'order_hours' => 0, 'ceil_days' => 0, ); $values['format'] = stm_get_clear_date_format(); $d_format = get_option( 'date_format' ); $t_format = get_option( 'time_format' ); $t_format = str_replace( 'g', 'hh', $t_format ); //moment.js didn't work with 'g' $t_format = str_replace( 'i', 'mm', $t_format ); //moment.js didn't work with 'i' $d_format = str_replace( 'm', 'MM', $d_format ); $d_format = str_replace( 'd', 'DD', $d_format ); $d_format = str_replace( 'F', 'MM', $d_format ); $d_format = str_replace( 'j', 'DD', $d_format ); $date_time_format = $d_format . ' ' . $t_format; $values['moment_format'] = stm_get_clear_date_format( $date_time_format ); $fields = stm_rental_order_fileds(); $pickup_location = ! empty( $_COOKIE[ $fields[0] ] ) ? intval( sanitize_text_field( $_COOKIE[ $fields[0] ] ) ) : false; $pickup_date = ! empty( $_COOKIE[ $fields[1] ] ) ? urldecode( sanitize_text_field( $_COOKIE[ $fields[1] ] ) ) : false; $return_date = ! empty( $_COOKIE[ $fields[2] ] ) ? urldecode( sanitize_text_field( $_COOKIE[ $fields[2] ] ) ) : false; $return_location = ! empty( $_COOKIE[ $fields[3] ] ) ? intval( sanitize_text_field( $_COOKIE[ $fields[3] ] ) ) : false; $return_same = ! empty( $_COOKIE[ $fields[4] ] ) ? sanitize_text_field( $_COOKIE[ $fields[4] ] ) : 'on'; $pickup_date_calc = ! empty( $_COOKIE[ $fields[5] ] ) ? urldecode( sanitize_text_field( $_COOKIE[ $fields[5] ] ) ) : false; $return_date_calc = ! empty( $_COOKIE[ $fields[6] ] ) ? urldecode( sanitize_text_field( $_COOKIE[ $fields[6] ] ) ) : false; /*Pickup Location*/ if ( $pickup_location ) { $values['pickup_location_id'] = $pickup_location; $values['pickup_location'] = get_post_meta( $pickup_location, 'address', true ); $values['pickup_location_fee'] = get_post_meta( $pickup_location, 'office_fee', true ); } else { $values['pickup_location'] = stm_get_empty_placeholder( $empty ); } /*Pickup date*/ if ( $pickup_date ) { $values['pickup_date'] = $pickup_date; $values['calc_pickup_date'] = $pickup_date_calc; } else { $values['pickup_date'] = stm_get_empty_placeholder( $empty ); $values['calc_pickup_date'] = stm_get_empty_placeholder( $empty ); } /*Return date*/ if ( $return_date ) { $values['return_date'] = $return_date; $values['calc_return_date'] = $return_date_calc; } else { $values['return_date'] = stm_get_empty_placeholder( $empty ); $values['calc_return_date'] = stm_get_empty_placeholder( $empty ); } /*Drop Location*/ if ( 'on' === $return_same ) { $values['return_location'] = $values['pickup_location']; } else { if ( ! empty( $return_location ) ) { $values['return_location_id'] = $return_location; $values['return_location'] = get_post_meta( $return_location, 'address', true ); $values['return_location_fee'] = floatval( get_post_meta( $return_location, 'office_fee', true ) ); } else { $values['return_location'] = $values['pickup_location']; } } $values['return_same'] = $return_same; if ( stm_get_empty_placeholder() !== $values['calc_return_date'] && stm_get_empty_placeholder() !== $values['calc_pickup_date'] ) { try { $date1 = DateTime::createFromFormat( 'm d, Y H:i', $values['calc_pickup_date'] ); $date2 = DateTime::createFromFormat( 'm d, Y H:i', $values['calc_return_date'] ); if ( ! ( $date1 instanceof DateTime ) || ! ( $date2 instanceof DateTime ) ) { throw new Exception( 'could not parse dates' ); } $diff = $date2->diff( $date1 )->format( '%a.%h' ); $hm = explode( '.', $diff ); if ( '0' === $hm[0] ) { $diff = 0; } $values['ceil_days'] = (int) ceil( $diff ); $values['order_days'] = (int) floor( $diff ); } catch ( Throwable $exception ) { $values['pickup_date'] = ''; $values['return_date'] = ''; $values['calc_pickup_date'] = ''; $values['calc_return_date'] = ''; } } else { $values['ceil_days'] = 1; } return apply_filters( 'stm_rental_date_values', $values ); } // function for converting date format on frontend function change_date_format( $date ) { $date_format = get_option( 'date_format' ); $time_format = get_option( 'time_format' ); $date_time_format = $date_format . ' ' . $time_format; $input_date_format = 'm d, Y H:i'; $datetime = DateTime::createFromFormat( $input_date_format, $date ); if ( false === $datetime ) { return false; } return $datetime->format( $date_time_format ); } /*Rental locations*/ function stm_rental_locations( $locations = array(), $full = false ) { if ( ! is_array( $locations ) ) { $locations = array(); } $args = array( 'post_type' => 'stm_office', 'posts_per_page' => -1, 'post_status' => 'publish', ); $offices = new WP_Query( $args ); $i = 0; if ( $offices->have_posts() ) : while ( $offices->have_posts() ) : $offices->the_post(); $id = get_the_ID(); $lat = get_post_meta( $id, 'latitude', true ); $lng = get_post_meta( $id, 'longitude', true ); $phone = get_post_meta( $id, 'phone', true ); $fax = get_post_meta( $id, 'fax', true ); $address = get_post_meta( $id, 'address', true ); if ( ! empty( $lng ) && ! empty( $lat ) ) { $google_api_key = apply_filters( 'stm_me_get_nuxy_mod', '', 'google_api_key' ); $class = ''; if ( ! empty( $google_api_key ) ) { $class = 'with-map'; } $content = "