• I am creating an email sending mechanism, where as recipients I would like to collect the emails of each store that fell within the range of the search radius.
    How can I collect this data in a variable?

    Many thanks in advance

    • This topic was modified 3 years, 6 months ago by lucatartarini.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Hi,

    I don’t understand very well where you want to run this. Is this something you are going to do in your backend, in a custom plugin or code snippet? Is it something you are going to allow your users to do from the search results of the store locator?

    Maybe you can get some inspiration from the wpsl_sql filter. In the provided link there is a code snippet, and between lines 20 and 29 you can see the query that is made to retrieve the stores that fall within a certain radius.

    Depending on where you want to implement your soultion, that query could be used as the basis for it, but I’m not sure if this will be what you want or not, so let me know.

    Regards,

    Thread Starter lucatartarini

    (@lucatartarini)

    Thanks a lot for the answer,
    let me explain better, the user of my site creates a cart with woocommerce, then through the mail function of php I send this cart as a message. I would like this email to be sent to all the stores found on the map. I attack the code. I would like the email addresses of the map to be saved in the $to variable

    <?php
    global $woocommerce;
    foreach ($woocommerce->cart->get_cart() as $item_id => $values) 
    {$string .= $values['quantity'].$values['data']->get_name().',';}
    
     
    $to = /*here collect the email addresses of the map*/
    
    ?>
    <form action="#" id="form" method="post" name="form">
    <input type="text" name="email">
    <textarea name="message"></textarea>	
    <button type="submit" name="send_message_btn">Send</button>
    </form>
    <?php 
    	
    if(isset($_POST['send_message_btn']))
    {
    $from = $_POST['email'];
    
    $subject = "Checking PHP mail";
    
    $headers = "From:" . $from;
    
    mail($to, $subject,$_POST['message'].$string, $headers);
    
     echo 'Email Sent.';
    }
    
    ?>

    Hi again,

    Thanks for your clarifications.

    However there is still something I don’t understand. In order to retrieve the emails from the stores that fall within a certain radius, we ned a start point. Otherwise, where is the reference for searching within that radius? We will need a pair of coordinates (lat, lng) so we can perform a search similar to the query that is done in the mentioned wpsl_sql filter.

    Regards,

    Thread Starter lucatartarini

    (@lucatartarini)

    Good morning,
    I have not received a reply to my last message, I really need to solve this problem. Thank you in advance for your reply.

    Hello!

    I did reply to your message, please check the message above your latest reply. I really don’t know what your functionality should work, in terms of the start point.

    Thanks!

    Thread Starter lucatartarini

    (@lucatartarini)

    Hi, thanks for the reply and sorry, I didn’t see it.
    The starting point must be that of the geolocated user.
    I’ll explain what the user experience should be:

    – create the cart (no problem);
    – it is geolocated and sees all the stores in its vicinity (ok, no problem)

    – send an email to all the stores identified.
    this will happen with a button that calls a php mail function. I should pass to this function a variable that contains the email addresses of the stores identified in the map. How can I include these addresses in a variable?

    in the previous messages you can see the code that is inside the page template

    Many thanks in advance

    Hi again,

    Thanks for your response.

    Well, the only way to make this work is to run a custom SQL query and include the wpsl_email data in the response, so it can be used later. As I mentioned, you can get some inspiration in the query that is shown in the wpsl_sql filter documentation.

    This is a very specific customization. The actual implementation of the query will depend on your particular WordPress stack, since I don’t know exactly how you will retrieve the search parameters (i.e., lat and lng of the start point, maximum number of stores, search radius) from the code snippet you sent me. So this will require a bit of coding, but I don’t have a Woocommerce test project at hand to try things out.

    If you cannot figure it out, you can always find a developer that can help you on upwork.com or codeable.io.

    Regards ๐Ÿ™‚

    Thread Starter lucatartarini

    (@lucatartarini)

    Thanks a lot for the answer.
    The last request really:
    if instead I started from a research in lugo, so I had a starting point, would it be possible to do?
    Let me explain better, if instead of the user’s geolocation, I forced the user to do a search, would it be possible at that point to collect the email addresses in a variable?

    Really thank you

    Hi again,

    What you are asking is definitely possible to do, the problem is that the details of the implementation are going to be depending on where in your code stack it is going to be executed. That is why I cannot give you a specific answer. You can take a look at how the data is created for the different stores in the database and create a query similar to the one we use internally for searching with the store locator itself.

    The stores are saved in wp_posts (with the value “wpsl_stores” in the “post_type” column). And the stores metadata are in the wp_postmeta table. In particular, the coordinates are the rows with the value “wpsl_lat” and “wpsl_long” in the “meta_key” column for a particular “meta_id”, which relates to the stores via the “post_id” column. This is all very standard WordPress practice.

    So, if you have two coordinates, you can query the database inside of your Woocommerce function and retrieve the stores within a certain radius, and then get the “wpsl_email” meta for those stores.

    Regards,

    Thread Starter lucatartarini

    (@lucatartarini)

    Hi, I’m sorry but I have another question.
    My code will be executed inside a page template, after the user searches the wpsl map. At this point I should go to collect the emails of the stores included in the map, could you please give me an example of code that you would program to collect these maps?

    woocommerce has nothing to do with it, I just need to collect the emails relating to the stores searched by the user on the map

    Thank you very much, if needed I can also go and insert the page template code.

    Hi there,

    You should be able to use the wpdb object almost anywhere on your site, and perform a query very similar to the one you can find in the wpsl_sql filter, but including the email data, something like for example:

    SELECT
        post_lat.meta_value AS lat,
        post_lng.meta_value AS lng,
        post_email.meta_value AS email,
        posts.ID,
        ( acos(
              cos( radians( '44.66930' ) ) * cos( radians( post_lat.meta_value ) )
            * cos( radians( post_lng.meta_value ) - radians( '-90.17190'  ) )
                      + sin( radians( '44.66930' ) ) * sin( radians( post_lat.meta_value ) ) )
        ) AS distance
    FROM wp_posts AS posts
        INNER JOIN wp_postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
        INNER JOIN wp_postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
        LEFT JOIN wp_postmeta AS post_email ON post_email.post_id = posts.ID AND post_email.meta_key = 'wpsl_email'
    WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish'
    HAVING distance < 50 ORDER BY distance ASC LIMIT 0, 25

    Where your start point in the example is (44.66930, -90.17190)

    So you can use the “email” field right there.

    Thread Starter lucatartarini

    (@lucatartarini)

    Thank you very much, but how do I then recall the value of the email field in a variable that can be inserted in a php mail function?

    This is the code I put in my theme’s function.php file:

    add_filter( 'wpsl_sql', 'custom_sql' );
    
    function custom_sql() {
        
        global $wpdb, $wpsl_settings;
        
        // Set a limit for the results and order them by distance.    
        if ( isset( $_GET['autoload'] ) && $_GET['autoload'] ) {
            $limit = '';
    
            if ( $wpsl_settings['autoload_limit'] ) {
                $limit = 'LIMIT %d';
            }
    
            $sql_sort = 'ORDER BY distance DESC '. $limit;
        } else {
            $sql_sort = 'HAVING distance < %d ORDER BY distance DESC LIMIT 0, %d';
        }
        
        // The default query that selects store location that fall within the selected radius. 
        $sql = "SELECT post_lat.meta_value AS lat,
                       post_lng.meta_value AS lng,
                       post_email.meta_value AS email,
                       posts.ID, 
                       ( %d * acos( cos( radians( %s ) ) * cos( radians( post_lat.meta_value ) ) * cos( radians( post_lng.meta_value ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( post_lat.meta_value ) ) ) ) 
                    AS distance        
                  FROM $wpdb->posts AS posts 
            INNER JOIN $wpdb->postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
            INNER JOIN $wpdb->postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
            LEFT JOIN wp_postmeta AS post_email ON post_email.post_id = posts.ID AND post_email.meta_key = 'wpsl_email'
                 WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish' $sql_sort";    
        
        return $sql;
    }

    while this is the code I have in the page template, I would like to recall the email value in the to variable to be inserted in the mail function

    if(isset($_POST['send_message_btn']))
    {
    $from = $_POST['email'];
    $to = ' ???????';
    $subject = "Richiesta dal sito Mastro.it";
    $corpo = $_POST['message'];
    
    $header = "From:" . $from .  $_POST['nome'];
    
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     
    // Create email headers
    $headers .= 'From: '.$from."\r\n".
        'Reply-To: '.$from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
    $message = '<html><body style="background-image: url(https://www.lucatartarini.net/area-test/mastro-new-site/wp-content/uploads/2022/11/bck-mail.jpg);background-size:cover">';
    $message .= '<img src="https://www.lucatartarini.net/area-test/mastro-new-site/wp-content/uploads/2022/11/logo-1.png" width="250" /><p style="color:red; font-size:20px;background-color: rgba(0, 0, 0, 0.9);padding:10px 20px">Richiesta preventivo da &nbsp'.  $_POST['nome'].'</p>';
    $message .= '<p style="color:#fff;font-size:16px;background-color: rgba(0, 0, 0, 0.9);padding:10px 20px">'.$corpo.'</p>';
    $message .= '</body></html>';
    
    mail($to, $subject, $message, $headers);
    
     echo '<div id="sent" class=" align-items-center">La tua richiesta รจ stata inviata.<br>Sarai presto ricontattato.<br><a href="https://www.lucatartarini.net/area-test/mastro-new-site">Torna alla Homepage</a><a href="https://www.lucatartarini.net/area-test/mastro-new-site/officine-meccaniche/">Richiedi un altro preventivo</a></div>';
    }
    
    ?>

    Really thank you for your work, it is precious

    • This reply was modified 3 years, 5 months ago by lucatartarini.

    Hi again,

    Sorry, I think you misunderstood what I meant.

    If you want to capture the stores that are being queried and process the data somehow outside of the flow of wp store locator, as it is your intention, then the query that I mentioned in my previous post must be done wherever you want your e-mails. That query does not belong in the wpsl_sql filter, because this filter affects the search results of the plugin itself, but it is of no use to you because what you want is to have that same query done elsewhere, where you will have full access to the results of the query.

    So, the query that I mentioned, which is very similar to the one inside wpsl_sql filter, has to be executed (with the $wpdb object) anywhere on your site, it does not depend on wp store locator. It is a query that you are free to execute anywhere, I hope that makes sense.

    Thread Starter lucatartarini

    (@lucatartarini)

    Hello,

    Excuse me for returning to the topic, but if I understood correctly, by including the code you provided before the function that generates the email, I should be able to insert the $email variable in the to value of the function and intercept the emails from the stores that are within the search radius.

    <script>
    
    SELECT
        post_lat.meta_value AS lat,
        post_lng.meta_value AS lng,
        post_email.meta_value AS email,
        posts.ID,
        ( acos(
              cos( radians( '43.38104077335135' ) ) * cos( radians( post_lat.meta_value ) )
            * cos( radians( post_lng.meta_value ) - radians( '11.117474417224276'  ) )
                      + sin( radians( '43.38104077335135' ) ) * sin( radians( post_lat.meta_value ) ) )
        ) AS distance
    FROM wp_posts AS posts
        INNER JOIN wp_postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
        INNER JOIN wp_postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
        LEFT JOIN wp_postmeta AS post_email ON post_email.post_id = posts.ID AND post_email.meta_key = 'wpsl_email'
    WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish'
    HAVING distance < 50 ORDER BY distance ASC LIMIT 0, 25
    
    </script>
    	
    <?php
    global $woocommerce;
    foreach ($woocommerce->cart->get_cart() as $item_id => $values) 
    {$string .='<li style="color:#fff; font-size:12px; padding:15px; border-bottom:dotted thin; "> <p  style="display:inline-block"><strong>'. $values['data']->get_name().'</strong></p><p> Numero pezzi:&nbsp'.$values['quantity'].'</p></li><br>';}
    
     
    
    
    
    ?>
    <form action="#" id="form" class="sendlistform"  method="post" name="form">
    <input type="text" name="nome" placeholder="Nome"><input type="text" name="email" placeholder="Email">
    <textarea name="message" placeholder="Il tuo messaggio..."></textarea>	
    <button type="submit" name="send_message_btn">Send</button>
    </form>
    <?php
    	
    if(isset($_POST['send_message_btn']))
    {
    $from = $_POST['email'];
    $to = $email;
    $subject = "Richiesta dal sito Mastro.it";
    $corpo = $_POST['message'];
    
    $header = "From:" . $from .  $_POST['nome'];
    

    This is the code I put in my file but it didn’t work.
    Could you tell me what I did wrong?

    Thank you so much for your availability

    Hi again,

    Well, actually that won’t work. The SQL query was just an example of a query you could use in conjunction of the wpdb object, not inside a <script> block.

    The thing here, don’t get me wrong, is that the problem you are trying to solve is maybe a little more complex than you think, and it will require some coding and some specific considerations for your particular WordPress installation, so it is a bit difficult for me to offer you a one-size-fits-all solution, I just gave you some broad strokes to get you in the right direction, since this is not an issue that is strictly related to our plugin, I hope you understand.

    Maybe the best approach here would be to contact a developer that can help you on upwork.com or codeable.io.

    Regards,

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

The topic ‘Collect mail data in a variable’ is closed to new replies.