Okay
  Public Ticket #3773154
Database Booking Count Not Increasing
Closed

Comments

  •  1
    Wai Yee started the conversation

    I am trying to create a custom php to check if a customer that has purchased a package is down to its last booking. If there is only 1 session left in their package, I'll set up a cron job to run the php and an automated email will be sent. 

    In order for these to work, I'll need to be able to track the number of bookings/appointments a client has made. 

    Isn't the bookingsCount in the screenshot supposed to increase when a customer make a booking? I need some way to track how many bookings a customer has made. I remember trying it about a month ago and I believe that I did make it work but I can't seem to remember what the issue is right now. 

    This is my custom php: 

    <?php

    // Load WordPress

    require_once('/home1/oqknirmy/public_html/wp-load.php');

    // Custom function to check for customers with only one session left

    function check_one_session_left() {

        global $wpdb;

        // Query to get customers with only 1 session left

        $results = $wpdb->get_results("

        SELECT 

            customers.email, 

            customers.firstName, 

            customers.lastName, 

            package_customers.bookingsCount AS booked,  

            services.bookingsCount AS total, 

            package_customers.id AS packageCustomerId

        FROM c30_amelia_packages_to_customers AS package_customers

        JOIN c30_amelia_packages_customers_to_services AS services 

            ON package_customers.id = services.packageCustomerId

        JOIN c30_amelia_users AS customers 

            ON package_customers.customerId = customers.id

        WHERE (services.bookingsCount - package_customers.bookingsCount = 1)

        AND package_customers.notifiedOneSession = 0

    ");

        // Check if results are returned

        if (!empty($results)) {

            echo "Found " . count($results) . " customers with only 1 session left.<br>";  // Display how many customers were found

            // Send notification email to each customer

            foreach ($results as $result) {

                $email = $result->email;

                $first_name = $result->firstName;

                $last_name = $result->lastName;

                $booked = $result->booked;

                $total = $result->total;

                $packageCustomerId = $result->packageCustomerId;

                

            // Personalize the email message

                $subject = 'Reminder: One Last Session Left!';

                

                // Format the email message

                $message = "Hi $first_name $last_name, ";

                $message .= "I sincerely hope you’re enjoying your Pilates sessions! This is a friendly reminder that you have just one session left in your current package. ";

                $message .= "To continue your progress, I encourage you to renew your package soon. If you have any questions or need assistance with the renewal process, please let me know! ";

                $message .= "Thank you for your continued support, and see you in the studio! ";

                $message .= "Best regards, ";

                $message .= "The Team ";

                echo "Sending email to: $email (1 session remaining). ";  // Log the email sending

                // Send email via WordPress wp_mail function

                $mail_sent = wp_mail($email, $subject, $message);

                // Log if the mail wasn't sent

                if (!$mail_sent) {

                    echo "Failed to send email to: $email.<br>";  // Display error on screen

                    error_log("Failed to send email to: $email");

                } else {

                    echo "Email successfully sent to: $email.<br>";  // Display success on screen

                    error_log("Email successfully sent to: $email");

                    // Mark this customer as notified

                    $wpdb->update(

                        'c30_amelia_packages_to_customers',

                        array('notifiedOneSession' => 1), // Update notifiedOneSession to 1

                        array('id' => $packageCustomerId) // Where package ID matches

                    );

                }

            }

        } else {

            echo "No customers with only 1 session left or notifications have been sent.<br>";  // Display message if no customers were found

            error_log("No customers with only 1 session left found.");

        }

    }

    // Run the session left check

    check_one_session_left();

    Attached files:  Screenshot 2024-11-28 at 1.11.11 PM.png

  •  1,596
    Marko replied

    Hello there,

    Thank you for reaching out to us.

    Unfortunately, currently this feature is not available in Amelia. Since this is a custom solution and it is not built in in Amelia and this is not covered by our support. We can ask for our dev opinion and see if they have some advice that we can give but we can not make any promises.

    We wish you all the best and hope you have a wonderful day ahead. 

    Kind Regards, 

    Marko Davidovic [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps, floor plans, choropleth maps, and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  1
    Wai Yee replied

    Hey Marko, 

    I understand, I am ok figuring out the rest myself but I just have one major question. I would like to know why is the bookingsCount number not increasing as I book an appointment. What should I do to fix it? 

    Attached files:  Screenshot 2024-11-29 at 6.08.33 AM.png

  •  1,596
    Marko replied

    Hello there,

    Thank you for reaching out to us.

    When you have several questions or issues which are not related to the title of the active ticket, please open a new ticket, and we will help you there. In that way, issues and questions that are related to different subjects will be in separate tickets so other customers or our support agents can find them easily. 

    Our policy is to have one issue or question per ticket because of the reasons that are described already.

    Thank you for understanding. 

    We just got the feedback from our colleagues and they have told us sent query. Here is a query which will return the customer packages that have only one appointment slot left

    SELECT
              pc.id AS id,
              IF(pc.bookingsCount > 0, pc.bookingsCount, (
                		SELECT SUM(pcs2.bookingsCount) FROM wp_amelia_packages_customers_to_services pcs2 WHERE pcs2.packageCustomerId = pc.id GROUP BY pc.id
              		)) AS totalCount,
              COUNT(cb.id) as bookingsCount,
              cu.email as customer_email,
              cu.firstName as customer_first_name
      
      FROM wp_amelia_packages_to_customers pc
      INNER JOIN wp_amelia_users cu ON cu.id=pc.customerId
      INNER JOIN wp_amelia_packages_customers_to_services pcs ON pc.id = pcs.packageCustomerId
      LEFT JOIN wp_amelia_customer_bookings cb ON pcs.id = cb.packageCustomerServiceId
      GROUP BY pc.id
      HAVING COUNT(cb.id)>0 AND COUNT(cb.id) = (totalCount - 1);

    Hope this helps.

    Should you have any further inquiries, we kindly request that you open separate tickets for each question and we will gladly help you there.

    We wish you all the best and hope you have a wonderful day ahead.  

    Kind Regards, 

    Marko Davidovic [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps, floor plans, choropleth maps, and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables