How to display a popup when all products are in cart?

Hello, I use dawn theme and what I am trying to do may sound unusual. What I would like to have happen is when the product is limited or all items of the same product are in cart, for example Item A has only 2 product and customer added 3 products in a cart rather than it take them to a warning page I want to show a popup where it tells all products are in your cart. How can I change the path so when someone clicks it goes where I want it to? Here is store URL https://rti-mailin-program.myshopify.com password is teststore. I appreciate any help you can offer!

Hi,

Either need to use app or for custom coding need to locate the theme.js or similar file. If it doesn’t exist, you can create a new JavaScript file.

Insert JavaScript Code

// Check if all products are in cart
function checkCartItems() {
    var productCount = {{ cart.item_count }};
    var availableProducts = {{ product.variants | map: 'inventory_quantity' | json }};
    var cartItems = {{ cart.items | json }};
    
    for (var i = 0; i < cartItems.length; i++) {
        if (cartItems[i].quantity >= availableProducts[i]) {
            // Display a pop-up warning message
            alert("All available products have been added to your cart.");
            break;
        }
    }
}

// Add an event listener to trigger the checkCartItems function
document.addEventListener('DOMContentLoaded', function() {
    checkCartItems();
});

If you still need help you can contact us, details are given at signature

Thank you so much to reply. I tried the above code in theme.js file which I created but still it takes me to the same page.