• Resolved prolys

    (@prolys)


    Hi,
    I want to create a booking website with in the search form possibility search by address or city with a possibility to search within a radius of km

Viewing 1 replies (of 1 total)
  • Plugin Author bookingalgorithms

    (@bookingalgorithms)

    Hello,

    There are many ways to do this, but they all involve customizing the code in your child theme. So you need to be familiar with PHP and read the BA Book Everything plugin code to find appropriate hooks.

    As a starting point you can use the next example.
    BA Book Everything contains ‘places’ post type with address field, longitude and latitude data.
    They may be cities, streets, or anything else you like.
    And it’s possible to add ‘places’ posts as a dropdown select field to the search form.
    The custom code to use in the child theme functions.php look like this:

    /// Add place selection to the booking object admin screen
    add_action('cmb2_booking_obj_after_address', 'owpc_cmb2_booking_obj_after_address', 10, 3);
    function owpc_cmb2_booking_obj_after_address( $cmb, $prefix, $category ){
    
        $cmb->add_field( array(
            'name'       => __( 'Place', BABE_TEXTDOMAIN ),
            'id'         => $prefix . 'place_'.$category->slug,
            'type'       => 'owpc_places_select',
            'desc'       => '',
            'attributes' => array(
                'data-conditional-id'    => $prefix . BABE_Post_types::$categories_tax,
                'data-conditional-value' => $category->slug,
            ),
        ) );
    }
    //////////////////////////////
    add_filter( 'cmb2_render_owpc_places_select', 'owpc_cmb2_owpc_places_select', 10, 5 );
    /**
     * Get places option list.
     * @return void
     */
    function owpc_cmb2_owpc_places_select($field, $value, $object_id, $object_type, $field_type){
    
        $output = '';
    
        $args = array(
            'post_type'   => BABE_Post_types::$mpoints_post_type,
            'numberposts' => -1,
            'post_parent' => 0,
            'post_status' => 'publish',
            'orderby' => 'title',
            'order' => 'ASC',
        );
    
        $children_args = $args;
        $posts = get_posts( $args );
    
        if ( $posts ) {
    
            $post_type_obj = get_post_type_object( BABE_Post_types::$mpoints_post_type );
    
            $output .= '<select class="cmb2_mpoints_select" name="'.$field_type->_name().'" id="'.$field_type->_id().'" data-conditional-id="'.$field->args['attributes']['data-conditional-id'].'" data-conditional-value="'.$field->args['attributes']['data-conditional-value'].'">
            <option class="" value=""'.selected( $value, 0, false ).' data-parent="0">'.$post_type_obj->labels->all_items.'</option>';
    
            foreach ( $posts as $post ) {
    
                $children_args['post_parent'] = $post->ID;
                $children = get_children( $children_args );
                $disabled = !empty($children) ? ' disabled' : '';
                $output .= '<option class="" value="'.$post->ID.'"'.$disabled.' '.selected( $value, $post->ID, false ).' data-parent="0">'.$post->post_title.'</option>';
    
                if (!empty($children)){
                    foreach($children as $child_post){
                        $output .= '<option class="" value="'.$child_post->ID.'" '.selected( $value, $child_post->ID, false ).' data-parent="'.$post->ID.'"> - '.$child_post->post_title.'</option>';
                    }
                }
            }
            $output .= '</select>';
        }
        echo $output;
    }
    //////////////////////// Extend search form
    add_filter('babe_search_form_before_date_fields', 'owpc_babe_search_form_before_date_fields', 10, 1);
    function owpc_babe_search_form_before_date_fields($output){
    
        ///// create places select field
        $args = array(
            'post_type'   => BABE_Post_types::$mpoints_post_type,
            'numberposts' => -1,
            'post_status' => 'publish',
            'orderby' => 'menu_order',
            'order' => 'ASC',
        );
    
        $posts = get_posts( $args );
        $field = '';
    
        if ( empty($posts) ){
            return $output;
        }
    
        $post_type_obj = get_post_type_object( BABE_Post_types::$mpoints_post_type );
    
        $selected_name = $post_type_obj->labels->name;
        $selected_id = !empty($_GET['place']) ? (int)$_GET['place'] : '';
        $add_class_all_items = ' term_item_selected';
    
        foreach ($posts as $post){
            $add_class = $post->ID == $selected_id ? ' term_item_selected' : '';
            $post_title = get_the_title($post->ID);
    
            if($post->ID == $selected_id){
                $selected_name = $post_title;
                $add_class_all_items = '';
            }
            $field .= '<li class="term_item'.$add_class.'" data-id="'.$post->ID.'" data-value="'.$post_title.'">'.$post_title.'</li>';
        }
        $field = '<li class="term_item'.$add_class_all_items.'" data-id="0" data-value="'.$post_type_obj->labels->name.'">'.$post_type_obj->labels->all_items.'</li>'.$field;
    
        $output .= '
    <div class="add_input_field add_input_field_places is-active" tabindex="0">
                        <i class="fas fa-map-marker-alt"></i><div class="add_ids_title">
                            <div class="add_ids_title_value">' . $selected_name . '</div><i class="fas fa-chevron-down"></i>
                            <ul id="add_ids_list_places" class="add_ids_list add_ids_list_places">
                            '.$field.'
                            </ul>
                            <input type="hidden" class="input_select_input_value" name="place" value="'.$selected_id.'">
                          </div>  
    </div>
    ';
        return $output;
    }
    ///////////////Hook in search args to get appropriate result
    add_filter('babe_search_result_args', 'owpc_babe_search_result_args', 10, 1);
    function owpc_babe_search_result_args($args){
    
        global $wpdb;
    
        if ( !empty($_GET['place']) ){
    
            $place_id = (int)$_GET['place'];
    
            $query = "SELECT * FROM ".$wpdb->postmeta."
            WHERE meta_key LIKE 'place_%' AND meta_value = '".$place_id."'";
    
            $results = $wpdb->get_results($query, ARRAY_A);
    
            if ( !empty($results) ){
                foreach($results as $i => $result){
                    $args['post__in'][] = $result['post_id'];
                }
            } else {
                $args['post__in'] = [0];
            }
        }
        return $args;
    }


    Best Regards,
    Booking Algorithms team

Viewing 1 replies (of 1 total)
  • The topic ‘search form’ is closed to new replies.