Shortcode thông báo âm thanh khi có đơn

Eric (kedochanh)
0
03/10/26
06/10/2026

1Thông báo âm thanh đơn (1 lần)

• Mã shortcode: [woocommerce_cook_audio_alert] Các vị trí khuyên dùng:
• Khu vực Admin: Nếu bạn muốn âm thanh phát khi nhân viên đang mở màn hình quản trị của WordPress, hãy chèn đoạn code này vào file footer.php của theme qua hàm do_shortcode: .
• Trang màn hình nhà bếp (Frontend): Nếu bạn tạo một trang riêng ở ngoài giao diện để nhà bếp theo dõi đơn hàng, bạn chỉ cần tạo 1 trang mới bằng WordPress Page Editor và dán trực tiếp đoạn mã [woocommerce_cook_audio_alert] vào nội dung trang.

//**THÔNG BÁO ĐƠN HÀNG BẾP **//
/*Báo đơn 1 lần*/
// 1. Tạo Shortcode [woocommerce_cook_audio_alert]
add_shortcode('woocommerce_cook_audio_alert', 'wc_cook_audio_alert_shortcode');
function wc_cook_audio_alert_shortcode() {
    // Đường dẫn tới file âm thanh thông báo của bạn (thay bằng link file .mp3 của bạn)
    $audio_url = 'https://95deli.fship.vn/wp-content/uploads/2026/10/tieng_doc_1791004572388.mp3'; 

    ob_start();
    ?>
    <div id="wc-cook-alert-container" style="display:none;">
        <audio id="wc-cook-audio" src="<?php echo esc_url($audio_url); ?>" preload="auto"></audio>
    </div>

    <script>
    jQuery(document).ready(function($) {
        // Lưu trữ danh sách các ID đơn hàng đã được phát âm thanh để tránh lặp lại
        var playedOrders = JSON.parse(localStorage.getItem('wc_played_cook_orders')) || [];

        function checkCookOrders() {
            $.ajax({
                url: '<?php echo esc_url(rest_url('wc-cook-alert/v1/get-orders')); ?>',
                method: 'GET',
                success: function(response) {
                    if (response && response.length > 0) {
                        var hasNewOrder = false;

                        response.forEach(function(orderId) {
                            if (!playedOrders.includes(orderId)) {
                                playedOrders.push(orderId);
                                hasNewOrder = true;
                            }
                        });

                        if (hasNewOrder) {
                            // Lưu lại danh sách đã phát vào bộ nhớ trình duyệt
                            localStorage.setItem('wc_played_cook_orders', JSON.stringify(playedOrders));
                            
                            // Phát âm thanh thông báo
                            var audio = document.getElementById('wc-cook-audio');
                            if (audio) {
                                audio.play().catch(function(error) {
                                    console.log("Trình duyệt chặn tự động phát âm thanh. Cần tương tác với trang trước.", error);
                                });
                            }
                        }
                    }
                }
            });
        }

        // Tự động kiểm tra mỗi 10 giây (10000ms)
        setInterval(checkCookOrders, 10000);
        
        // Chạy kiểm tra ngay khi tải trang xong
        checkCookOrders();
    });
    </script>
    <?php
    return ob_get_clean();
}

// 2. Tạo REST API endpoint nội bộ để lấy danh sách đơn hàng có trạng thái "cook"
add_action('rest_api_init', function () {
    register_rest_route('wc-cook-alert/v1', '/get-orders', array(
        'methods'             => 'GET',
        'callback'            => 'wc_get_current_cook_orders',
        'permission_callback' => '__return_true', // Cho phép kiểm tra bất đồng bộ công khai
    ));
});

function wc_get_current_cook_orders() {
    // Lấy các đơn hàng có trạng thái "cook". 
    // Lưu ý: Nếu slug trạng thái của bạn là "wc-cook" thì điền 'wc-cook' vào mảng bên dưới.
    $orders = wc_get_orders(array(
        'status' => array('wc-cook', 'cook'), 
        'limit'  => -1,
        'return' => 'ids',
    ));
    
    return $orders;
}

1Thông báo âm thanh đơn NGỪNG khi chạm vào

//**THÔNG BÁO ĐƠN HÀNG BẾP **//
/*Báo đơn 1 lần & Ngừng khi chạm vào màn hình*/
// 1. Tạo Shortcode [woocommerce_cook_audio_alert]
add_shortcode('woocommerce_cook_audio_alert', 'wc_cook_audio_alert_shortcode');
function wc_cook_audio_alert_shortcode() {
    // Đường dẫn tới file âm thanh thông báo của bạn (thay bằng link file .mp3 của bạn)
    $audio_url = 'https://95deli.fship.vn/wp-content/uploads/2026/10/tieng_doc_1791004572388.mp3'; 

    ob_start();
    ?>
    <div id="wc-cook-alert-container" style="display:none;">
        <audio id="wc-cook-audio" src="<?php echo esc_url($audio_url); ?>" preload="auto"></audio>
    </div>

   <script>
    jQuery(document).ready(function($) {
        // Lưu trữ danh sách các ID đơn hàng đã được phát âm thanh để tránh lặp lại
        var playedOrders = JSON.parse(localStorage.getItem('wc_played_cook_orders')) || [];
        var audio = document.getElementById('wc-cook-audio');

        function checkCookOrders() {
            $.ajax({
                url: '<?php echo esc_url(rest_url('wc-cook-alert/v1/get-orders')); ?>',
                method: 'GET',
                success: function(response) {
                    if (response && response.length > 0) {
                        var hasNewOrder = false;

                        response.forEach(function(orderId) {
                            if (!playedOrders.includes(orderId)) {
                                playedOrders.push(orderId);
                                hasNewOrder = true;
                            }
                        });

                        if (hasNewOrder) {
                            // Lưu lại danh sách đã phát vào bộ nhớ trình duyệt
                            localStorage.setItem('wc_played_cook_orders', JSON.stringify(playedOrders));
                            
                            // Phát âm thanh thông báo
                            if (audio) {
                                audio.play().catch(function(error) {
                                    console.log("Trình duyệt chặn tự động phát âm thanh. Cần tương tác với trang trước.", error);
                                });
                            }
                        }
                    }
                }
            });
        }

        // Hàm tắt âm thanh khi phát hiện có chuyển động hoặc tương tác
        function stopAudioOnInteraction() {
            if (audio && !audio.paused) {
                audio.pause();
                audio.currentTime = 0; // Tua về đầu bài để lần sau phát lại từ đầu
            }
        }

        // Lắng nghe các sự kiện chuyển động chuột, gõ phím hoặc chạm màn hình (mobile)
        $(document).on('mousemove keydown touchstart', stopAudioOnInteraction);

        // Tự động kiểm tra mỗi 10 giây (10000ms)
        setInterval(checkCookOrders, 10000);
        
        // Chạy kiểm tra ngay khi tải trang xong
        checkCookOrders();
    });
    </script>
    <?php
    return ob_get_clean();
}

// 2. Tạo REST API endpoint nội bộ để lấy danh sách đơn hàng có trạng thái "cook"
add_action('rest_api_init', function () {
    register_rest_route('wc-cook-alert/v1', '/get-orders', array(
        'methods'             => 'GET',
        'callback'            => 'wc_get_current_cook_orders',
        'permission_callback' => '__return_true', // Cho phép kiểm tra bất đồng bộ công khai
    ));
});

function wc_get_current_cook_orders() {
    // Lấy các đơn hàng có trạng thái "cook". 
    // Lưu ý: Nếu slug trạng thái của bạn là "wc-cook" thì điền 'wc-cook' vào mảng bên dưới.
    $orders = wc_get_orders(array(
        'status' => array('wc-cook', 'cook'), 
        'limit'  => -1,
        'return' => 'ids',
    ));
    
    return $orders;
}

1Thông báo âm thanh đơn (lặp lại)

• Mã shortcode: [woocommerce_cook_audio_alert]

//**THÔNG BÁO ĐƠN HÀNG BẾP **//
/*Báo đơn lặp lại*/
// 1. Tạo Shortcode [woocommerce_cook_audio_alert]
add_shortcode('woocommerce_cook_audio_alert', 'wc_cook_audio_alert_shortcode');
function wc_cook_audio_alert_shortcode() {
    // Đường dẫn tới file âm thanh thông báo của bạn (thay bằng link file .mp3 của bạn)
    $audio_url = 'https://soundjay.com'; 

    ob_start();
    ?>
    <div id="wc-cook-alert-container" style="display:none;">
        <!-- Thêm thuộc tính loop để âm thanh tự động lặp lại liên tục -->
        <audio id="wc-cook-audio" src="<?php echo esc_url($audio_url); ?>" preload="auto" loop></audio>
    </div>

    <script>
    jQuery(document).ready(function($) {
        // Lưu trữ danh sách các ID đơn hàng đã được phát âm thanh để tránh lặp lại
        var playedOrders = JSON.parse(localStorage.getItem('wc_played_cook_orders')) || [];
        var audio = document.getElementById('wc-cook-audio');

        function checkCookOrders() {
            $.ajax({
                url: '<?php echo esc_url(rest_url('wc-cook-alert/v1/get-orders')); ?>',
                method: 'GET',
                success: function(response) {
                    if (response && response.length > 0) {
                        var hasNewOrder = false;

                        response.forEach(function(orderId) {
                            if (!playedOrders.includes(orderId)) {
                                playedOrders.push(orderId);
                                hasNewOrder = true;
                            }
                        });

                        if (hasNewOrder) {
                            // Lưu lại danh sách đã phát vào bộ nhớ trình duyệt
                            localStorage.setItem('wc_played_cook_orders', JSON.stringify(playedOrders));
                            
                            // Phát âm thanh thông báo vòng lặp
                            if (audio) {
                                audio.play().catch(function(error) {
                                    console.log("Trình duyệt chặn phát âm thanh. Cần chạm vào màn hình trước để cấp quyền.", error);
                                });
                            }
                        }
                    }
                }
            });
        }

        // Sự kiện: Khi người dùng chạm hoặc click vào bất kỳ đâu trên màn hình
        $(document).on('click touchstart', function() {
            if (audio && !audio.paused) {
                audio.pause();       // Tạm dừng âm thanh
                audio.currentTime = 0; // Tua lại từ đầu để sẵn sàng cho lần báo sau
                console.log("Đã tắt âm thanh thông báo đơn hàng mới.");
            }
        });

        // Tự động kiểm tra mỗi 10 giây (10000ms)
        setInterval(checkCookOrders, 10000);
        
        // Chạy kiểm tra ngay khi tải trang xong
        checkCookOrders();
    });
    </script>
    <?php
    return ob_get_clean();
}

// 2. Tạo REST API endpoint nội bộ để lấy danh sách đơn hàng có trạng thái "cook"
add_action('rest_api_init', function () {
    register_rest_route('wc-cook-alert/v1', '/get-orders', array(
        'methods'             => 'GET',
        'callback'            => 'wc_get_current_cook_orders',
        'permission_callback' => '__return_true',
    ));
});

function wc_get_current_cook_orders() {
    $orders = wc_get_orders(array(
        'status' => array('wc-cook', 'cook'), 
        'limit'  => 10,
        'return' => 'ids',
    ));
    
    return $orders;
}

Tags :
Tags: