Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › If exact search for a product then automatically add to cart › Reply To: If exact search for a product then automatically add to cart
October 17, 2022 at 12:54 pm
#39670
Keymaster
Hi,
Of course. I am not sure how simpler it might get, but this should be very close to an actual solution:
add_action('wp_footer', 'asp_add_to_cart_handler', 999999);
function asp_add_to_cart_handler() {
?>
<script>
let asp_add_to_cart_handler = function(e) {
let id = e.detail[0],
instance = e.detail[1],
phrase = e.detail[2],
data = e.detail[3],
results = document.querySelectorAll( '.asp_r_' + id + '_' + instance + ' .item' );
if ( results.length == 1 ) {
document.querySelectorAll( '.asp_r_' + id + '_' + instance + ' .button' ).click();
}
}
document.querySelectorAll('.asp_m').forEach(function(node) {
node.addEventListener('asp_search_end', asp_add_to_cart_handler);
});
</script>
<?php
}