• Resolved WeDev.Africa

    (@mad2kx)


    I am trying to send a custom email after 1h to someone who tries to place an order in my store but did not make a successful payment. The code I am using below, but for some reason no email is being sent after 1h.

    I do not want to install any 3rd party plugins that clogg up my wordpress and will use the same hook.

    Can someone please help?

    function schedule_custom_pending_payment_email($order_id) {
        // Schedule the email to be sent in 1 hour (3600 seconds)
        wp_schedule_single_event(time() + 3600, 'send_custom_pending_payment_email', array($order_id));
    }
    
    add_action('woocommerce_order_status_pending', 'schedule_custom_pending_payment_email');
    
    function send_custom_pending_payment_email($order_id) {
        $order = wc_get_order($order_id);
        if (!$order) {
            return;
        }
        $customer_email = $order->get_billing_email();
    
        // Email subject
        $subject = 'Your Order is Awaiting Payment';
    
        // Email content with dynamic data in HTML format
        $message = '<html><body>';
        $message .= '<p>Hello ' . $order->get_billing_first_name() . ',</p>';
        $message .= '<p>Thank you for considering X for your purchase.</p>';
        $message .= '<p>Unfortunately, we have not received payment for order ' . $order->get_order_number() . '. In the event of struggling to make a payment, please do the following:</p>';
        $message .= '<ul>';
        $message .= '<li>Attempting to buy? Try one of the other secure payment methods. We offer X and Y. Your cart should still have your selected products in it.</li>';
        $message .= '<li>Do you want to sell on X? <a href="link">Click here to finalise your payment</a>.</li>';
        $message .= '<li>Existing Creator on X? We will do three attempts to withdraw your subscription, please ensure that funds are available and that your credit card details are up to date.</li>';
        $message .= '</ul>';
        $message .= '<p>Alternatively, you can contact us at <a href="mailto:link">link</a> or via WhatsApp:.</p>';
        $message .= '<p><a href="link">PLACE A NEW ORDER</a></p>'; 
        $message .= '</body></html>';
    
        // Headers for HTML email
        $headers = array('Content-Type: text/html; charset=UTF-8');
    
        // Send the email
        wp_mail($customer_email, $subject, $message, $headers);
    }
    
    // Hook the actual email sending function to the custom action
    add_action('send_custom_pending_payment_email', 'send_custom_pending_payment_email');
    
Viewing 1 replies (of 1 total)
  • Hi @mad2kx

    Thanks for reaching out!

    I understand that you are using the custom code snippet above to send a custom email after one hour to someone who tries to place an order in your store, however, it is not working as expected.

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Send email to customer for pending payments’ is closed to new replies.