Last active
December 24, 2023 19:43
-
-
Save Asikur22/795f51aa1bcd3c295bfab2ae4637ce7c to your computer and use it in GitHub Desktop.
WP Admin AJAX Call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const item = { | |
| action: 'custom_add_to_cart', | |
| product_id: Button.getAttribute( 'data-product_id' ), | |
| quantity: document.querySelector( '.qty[name="quantity"]' ).value | |
| }; | |
| var formData = new FormData(); | |
| for ( let key in item ) { | |
| formData.append( key, item[key] ); | |
| } | |
| var xhr = new XMLHttpRequest(); | |
| xhr.onreadystatechange = function () { | |
| if ( xhr.readyState === 4 && xhr.status === 200 ) { | |
| let response = JSON.parse( xhr.responseText ); | |
| if ( response.success ) { | |
| window.location.replace( response.data.cart_url ); | |
| } else { | |
| alert( response.data ); | |
| } | |
| } | |
| } | |
| // xhr.open( 'POST', "<?php echo admin_url( 'admin-ajax.php' ); ?>", false ); | |
| xhr.open( 'POST', wc_add_to_cart_params.ajax_url, false ); | |
| xhr.send( formData ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // JS Code | |
| $( '.button' ).on( 'click', function ( event ) { | |
| var data = { | |
| action: 'action_name' | |
| }; | |
| $.post( lf_ajax.ajax_url, data, function ( response ) { | |
| console.log( response ); | |
| } ); | |
| } ); | |
| // PHP Code | |
| function lf_add_cookies_ip() { | |
| // code here... | |
| die(); | |
| } | |
| add_action( 'wp_ajax_lf_cookies_ip', 'lf_add_cookies_ip' ); | |
| add_action( 'wp_ajax_nopriv_lf_cookies_ip', 'lf_add_cookies_ip' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| document.querySelector( '#btn' ).addEventListener( 'click', function ( event ) { | |
| event.preventDefault(); | |
| let formData = new FormData(); | |
| formData.append( 'action', 'hand_dryers_filters' ); | |
| let xhr = new XMLHttpRequest(); | |
| xhr.onreadystatechange = function () { | |
| if ( this.readyState == 4 && this.status == 200 ) { | |
| let response = JSON.parse( this.responseText ); | |
| console.log( response ); | |
| if ( response.success ) { | |
| window.location.replace( response.data.cart_url ); | |
| } else { | |
| alert( response.data ); | |
| } | |
| } | |
| }; | |
| xhr.open( "POST", "<?php echo admin_url( 'admin-ajax.php' ); ?>", false ); | |
| xhr.send( formData ); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment