• Resolved anwind

    (@anwind)


    Hello
    There is a service, https://dadata.ru / with which you can implement hints for the location field. Here is an attempt at integration:
    <script>
    $(“#adverts_location”).suggestions({
    token: “11111111111111111111111111111”,
    type: “ADDRESS”,
    /* Called when the user selects one of the suggestions */
    onSelect: function(suggestion) {
    console.log(suggestion);
    }
    });
    </script>
    But from the add form does not work. Presumably because of the styles. Can you help in the implementation of the plan?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    can you send a link to the page where you have this code or your whole code snippet?

    The way you have it now I think that either:
    – there will be error saying the $ is null since the code does not wait for the jQuery library to be loaded
    – there will be error saying the .suggestions() is undefined as it does not look like you are loading it anywhere.

    Thread Starter anwind

    (@anwind)

    https://websgood.ru/primer

    it would be great to implement this in the location field on the add page and in the search form.

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    thanks, but this field does not seem to be connected to the [adverts_add] form in any way?

    Does the https://dadata.ru have some documentation in English so i could take a look at it?

    Thread Starter anwind

    (@anwind)

    Unfortunately not in English. The example is not related to [adverts_add] it’s just the logic of what would be nice to do. It is a pity that it is impossible to implement the plan.

    Plugin Author Greg Winiarski

    (@gwin)

    If you could point me to some Russian docs with code snippets/examples showing how to use the API I might be able to work with it as well.

    I think the suggestions should be possible and it shouldn’t really be hard to use them but I first need to understand how it is being run.

    Thread Starter anwind

    (@anwind)

    https://dadata.ru/suggestions/usage/address/

    To connect to the domain, registration on the website is required https://dadata.ru

    Plugin Author Greg Winiarski

    (@gwin)

    The minimal code required to get the Dadata.ru working with adverts_location field would be something like this

    
    add_action( "init", function() {
        wp_register_style( 'dataru-css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/suggestions.min.css', array(), "21.12.0" );
        wp_register_script( 'dataru-js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/jquery.suggestions.min.js', array( 'jquery' ), "21.12.0" );
    });
    add_action( 'wp_enqueue_scripts', function() {
        wp_enqueue_script( 'dataru-js' );
        wp_enqueue_style( 'dataru-css' );
    });
    add_action( 'wp_footer', function() {
        ?>
        <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function(event) { 
            if( jQuery("#adverts_location").length === 0) {
                return;
            }
            jQuery("#adverts_location").suggestions({
                token: "${API_KEY}",
                type: "ADDRESS",
                /* Вызывается, когда пользователь выбирает одну из подсказок */
                onSelect: function(suggestion) {
                    console.log(suggestion);
                }
            });
        });
        </script>
        <?php
    },1000);
    

    You just need to update the API key and probably change the line console.log(suggestion); to something that will fill the location field value with the selected suggestion.

    Thread Starter anwind

    (@anwind)

    Good afternoon. Did I understand correctly.
    1. The code I added to the plugin is profunctions
    2. Added my API

    add_action( "init", function() {
        wp_register_style( 'dataru-css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/suggestions.min.css', array(), "21.12.0" );
        wp_register_script( 'dataru-js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/jquery.suggestions.min.js', array( 'jquery' ), "21.12.0" );
    });
    add_action( 'wp_enqueue_scripts', function() {
        wp_enqueue_script( 'dataru-js' );
        wp_enqueue_style( 'dataru-css' );
    });
    add_action( 'wp_footer', function() {
       ?>
        <script>
        document.addEventListener("DOMContentLoaded", function(event) { 
            if( jQuery("#adverts_location").length === 0) {
                return;
            }
            jQuery("#adverts_location").suggestions({
                token: "71198f64877251cba46a2a88ab061f4a4369298a",
                type: "ADDRESS",
                /* Вызывается, когда пользователь выбирает одну из подсказок */
                onSelect: function(suggestion) {
                    console.log(suggestion);
                }
            });
        });
        </script>
        <?php
    },1000);
    

    Does not work https://websgood.ru/adverts/add?preselected_category=dlya-avtomobilej

    • This reply was modified 4 years, 1 month ago by anwind.
    • This reply was modified 4 years, 1 month ago by anwind.
    Plugin Author Greg Winiarski

    (@gwin)

    It seems to be working fine but the suggestions are conflicting with the theme CSS and are not visible try adding the below code in the wp-admin / Appearance / Customize / Additional CSS panel

    
    .suggestions-wrapper {
      overflow: hidden !important;
      height: auto !important;
      min-height: 100px;
      position: initial !important;
    }
    

    Also the code snippet from the previous message is showing in your source code twice for some reason, you should show it once only.

    Thread Starter anwind

    (@anwind)

    Oh, yes, adding styles helped, it works. Thanks.

    Plugin Author Greg Winiarski

    (@gwin)

    Ok great, I am glad you have it the way you wanted it, I am marking the thread as resolved, maybe someone in the future will find it helpful.

    BTW. If you are finding the plugin useful i would be thankful if you could write a short one or two sentence review here https://ww.wp.xz.cn/support/view/plugin-reviews/wpadverts

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘location’ is closed to new replies.