Trigger added_to_cart event

This code triggers added to cart
 avatar
unknown
html
3 years ago
1.2 kB
6
Indexable
<script>
//Wait page to load
window.addEventListener('load', function(){
    //Grab the element with class 'ags-divi-wc-shop-ajax'
    const targetNode = document.querySelector('.ags-divi-wc-shop-ajax');
    //If the element does not exist abort
    if(!targetNode){return}
    //Track when loading of the element is finished
    let responded = 0;
    //Observe for changes of the element
    const observer = new MutationObserver(function(mList){
        //Loop any change is class of the element
        mList.forEach(function(mutation){
            if(mutation.type == 'attributes' && mutation.attributeName == 'class'){
                //If is loading track this
                if( mutation.target.matches('.ags-woo-shop-loading') ){
                    responded = 1;
                //Once loding completed
                }else if(responded){
                    //Untrack it
                    responded = 0;
                    //Fire the woocommerce event 'added_to_cart'
                    jQuery('body').trigger('added_to_cart',[])
                }
            }
        })
    });
    //Start the observer
    observer.observe(targetNode, {childList: false,subtree: false,attributes: true});
});
</script>
Editor is loading...