php 在选定时间段内未完成订单的电子邮件未发送

ltskdhd1  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(93)

我遇到了一个问题,我的系统,我试图发送电子邮件给我的送货司机,如果一个订单没有在客户选择的时间槽内完成。尽管成功添加了订单Meta,电子邮件没有被发送,我正在努力找出这个问题的根源。我正在寻求社区的专业知识来帮助我理解和解决这个问题。任何见解或敬请指导。

// Add custom column to admin orders page
add_filter('manage_edit-shop_order_columns', 'gojo_add_custom_order_columns');
function gojo_add_custom_order_columns($columns) {
    // Check if the current user has either 'Shop manager' or 'Administrator' role
    if (current_user_can('shop_manager') || current_user_can('administrator')) {
        $columns['gojo_order_delayed'] = 'Order Delayed';
    }
    return $columns;
}

// Populate the custom column with data
add_action('manage_shop_order_posts_custom_column', 'gojo_custom_order_column_content', 10, 2);
function gojo_custom_order_column_content($column, $post_id) {
    $order = wc_get_order($post_id);

    if ($column === 'gojo_order_delayed' && in_array($order->get_status(), array('processing', 'driver-assigned', 'out-for-delivery'))) {
        $delivery_date = get_post_meta($post_id, 'Delivery Date', true);
        $time_slot = get_post_meta($post_id, 'Time Slot', true);

        if ($delivery_date && date('j F, Y') === $delivery_date && $time_slot) {
            $current_time = current_time('H:i'); // Get current time in 24-hour format

            list($start_time, $end_time) = explode(' - ', $time_slot);

            $order_id = $order->get_id(); // Get order ID

            if (strtotime($current_time) > strtotime($end_time)) {
                // If order is delayed, add meta data
                update_post_meta($post_id, 'Order Delayed', 'Yes');
                echo '<span style="color: white; font-weight: bold;">Order Delayed. Please call the driver.</span>';

                // Check for email conditions and send email only if 'Order Delayed' is 'Yes'
                $status = $order->get_status();
                if ($status == 'driver-assigned' || $status == 'out-for-delivery') {
                    $meta_data = $order->get_items();

                    foreach ($meta_data as $meta) {
                        if ($meta->key === 'lddfw_driverid') {
                            $driver_id = $meta->value;
                        }
                        if ($meta->key === 'Order Delayed') {
                            $order_delay_value = $meta->value;

                            if ($order_delay_value === 'Yes') {
                                // Check if 'Order Delayed' is 'Yes' before sending the email
                                $order_delayed = get_post_meta($post_id, 'Order Delayed', true);
                                if ($order_delayed === 'Yes') {
                                    // Check if email has already been sent
                                    $email_sent_order_delayed = $order->get_meta('email_sent_order_delayed', true);

                                    if ($email_sent_order_delayed != 'Yes') {
                                        // Get driver information
                                        $driver = get_userdata($driver_id);
                                        $driver_name = $driver->display_name;
                                        $driver_email = $driver->user_email;

                                        // Send email to driver
                                        $subject = 'Order no. ' . $order_id . ' is Delayed'; // Use $order_id
                                        $message = 'Hello ' . $driver_name . ',<br><br> Order no. ' . $order_id . ' has been delayed. Please call the customer & assure order will be delivered soon!.<br>';

                                        // Get customer information
                                        $billing_phone = $order->get_billing_phone();
                                        $shipping_phone = $order->get_shipping_phone();

                                        // Add phone numbers to the message
                                        $message .= 'Billing Phone: ' . $billing_phone . '<br>';
                                        $message .= 'Shipping Phone: ' . $shipping_phone . '<br>';

                                        $headers = array('Content-Type: text/html; charset=UTF-8');

                                        add_filter('wp_mail_from', 'gojo_original_mail_address_email');
                                        add_filter('wp_mail_from_name', 'gojo_email_name_order_delayed');

                                        if (wp_mail($driver_email, $subject, $message, $headers)) {
                                            // Update email_sent meta value
                                            $order->update_meta_data('email_sent_order_delayed', 'Yes');
                                            $order->save();
                                            remove_filter('wp_mail_from', 'gojo_original_mail_address_email');
                                            remove_filter('wp_mail_from_name', 'gojo_email_name_order_delayed');
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } 
        }
    }
}

function gojo_original_mail_address_email() {
    $email = '[email protected]';
    return $email;
}

function gojo_email_name_order_delayed() {
    return 'Oven Fresh';
}

字符串

t30tvxxf

t30tvxxf1#

您的代码中奇怪的是,您在订单本身和该订单中的每个订单项上使用'Order Delayed'元数据 (**问题:**对于订单项,什么时候设置'Order Delayed'元数据?)..(奇怪吗?)*.你应该试着更好地解释你的问题中的每一件事,因为没有人能通过魔法猜出上下文和相关细节。
我尝试改进和优化您的主要功能代码,只使用CRUD methods,它与WooCommerce HPOS new feature一起工作。尝试以下 (未经测试)

// Add custom column to admin orders page
add_filter('manage_edit-shop_order_columns', 'gojo_add_custom_order_columns');
function gojo_add_custom_order_columns($columns) {
    // Check if the current user has either 'Shop manager' or 'Administrator' role
    if (current_user_can('shop_manager') || current_user_can('administrator')) {
        $columns['gojo_order_delayed'] = 'Order Delayed';
    }
    return $columns;
}

// Populate the custom column with data
add_action('manage_shop_order_posts_custom_column', 'gojo_custom_order_column_content', 10, 2);
function gojo_custom_order_column_content($column, $post_id) {
    global $the_order;
    $order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order($post_id);

    if ($column === 'gojo_order_delayed' && $order->has_status(['processing', 'driver-assigned', 'out-for-delivery'])) {
        $delivery_date  = $order->get_meta('Delivery Date');
        $time_slot      = $order->get_meta('Time Slot');

        if ( $delivery_date && date('j F, Y') === $delivery_date && $time_slot ) {
            $current_time = current_time('H:i'); // Get current time in 24-hour format
            $end_time     = end( explode(' - ', $time_slot) );

            if (strtotime($current_time) > strtotime($end_time)) {
                // If order is delayed, add meta data
                $order->update_meta_data('Order Delayed', 'Yes');
                echo '<span style="color: white; font-weight: bold;">Order Delayed. Please call the driver.</span>';

                // Check for email conditions and send email only if 'Order Delayed' is 'Yes'
                if ( $order->has_status(['driver-assigned', 'out-for-delivery']) ) {
                    $update_order  = false; // Initializing

                    if ( $order->get_meta('email_sent_order_delayed') !== 'Yes' ) {
                        add_filter('wp_mail_from', 'gojo_original_mail_address_email');
                        add_filter('wp_mail_from_name', 'gojo_email_name_order_delayed');
                        // Loop through order items
                        foreach ($order->get_items() as $item) {
                            if ( $item->get_meta('Order Delayed') === 'Yes' ) {
                                // Get driver WP_User object
                                $driver_id = $item->get_meta('lddfw_driverid');
                                $driver    = get_userdata($driver_id);

                                $order_num = $order->get_order_number();

                                // Send email to driver
                                $subject = 'Order no. ' . $order_num . ' is Delayed'; 
                                $message = 'Hello ' . $driver->display_name . ',<br><br> Order no. ' . $order_num . ' has been delayed. Please call the customer & assure order will be delivered soon!.<br>';

                                // Add customer phone numbers to the message
                                $message .= 'Billing Phone: ' . $order->get_billing_phone() . '<br>';
                                $message .= 'Shipping Phone: ' . $order->get_shipping_phone() . '<br>';

                                $headers = array('Content-Type: text/html; charset=UTF-8');

                                if ( wp_mail($driver->user_email, $subject, $message, $headers) ) {
                                    $update_order = true;
                                }
                            }
                        }
                        if ( $update_order ) {
                            $order->update_meta_data('email_sent_order_delayed', 'Yes'); // Update email_sent meta value
                        }
                        remove_filter('wp_mail_from', 'gojo_original_mail_address_email');
                        remove_filter('wp_mail_from_name', 'gojo_email_name_order_delayed');
                    }
                }
                $order->save(); // Save order updated metadata (just once)
            } 
        }
    }
}

function gojo_original_mail_address_email() {
    $email = '[email protected]';
    return $email;
}

function gojo_email_name_order_delayed() {
    return 'Oven Fresh';
}

字符串
由于您的代码不是真正可测试的,我不知道这是否会对您的问题做出任何更改。

**最后一点:**电子邮件处理不应该在管理员订单列表中触发,而是在其他地方,使用例如woocommerce_order_status_changed动作挂钩,当订单状态更改时触发.

相关问题