Home / eCommerce / WWOF v3: Force “View Cart” link in the success side-popup to open in the same tab
Duplicate Snippet

Embed Snippet on Your Site

WWOF v3: Force “View Cart” link in the success side-popup to open in the same tab

Code Preview
php
<?php
/**
 * WWOF v3: Force "View Cart" link in the success side-popup to open in the same tab.
 */
add_action( 'wp_footer', 'wwof_v3_view_cart_same_tab_script', 999 );
function wwof_v3_view_cart_same_tab_script() {
    ?>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            
            // Watch the document for the Ant Design side-drawer being opened
            const observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.addedNodes.length) {
                        mutation.addedNodes.forEach(function(node) {
                            if (node.nodeType === 1) { 
                                
                                // Find the "View Cart" link inside the success popup container
                                const viewCartLinks = node.querySelectorAll ? node.querySelectorAll('.view-cart a') : [];
                                
                                viewCartLinks.forEach(function(link) {
                                    // If it is set to open in a new tab, remove that behavior
                                    if (link.getAttribute('target') === '_blank') {
                                        link.removeAttribute('target');
                                    }
                                });
                                
                            }
                        });
                    }
                });
            });
            // We observe the entire body because the side-popup is rendered at the very end of the HTML document
            observer.observe(document.body, { childList: true, subtree: true });
            
        });
    </script>
    <?php
}

Comments

Add a Comment