If exact search for a product then automatically add to cart

Home Forums Product Support Forums Ajax Search Pro for WordPress Support If exact search for a product then automatically add to cart

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 weeks, 1 day ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39203
    pprosch41
    pprosch41
    Participant

    Hi there,
    first I want to say I’m a happy long-time user of your plugin.

    For QuickOrder purpose I use the code-snippet for adding a add-to-cart-button to the search results (for simple products or product variations)..
    This works perfect for years now …

    Now I want to get a barcode-scanning solution done with your plugin.

    For example:
    If the search result is getting 1 result only and “Enter” is pressed (exact match for a custom field “ean_code”), then the simple product (or product variation) should be instantly added to cart.
    After that empty the search input field and “activate” it for a next search.

    1.) Count search results
    2a.) Check if only one search result
    2b.) Check if product is a simple product or a product variation
    2c.) Check if the result comes from a product (or product variation) custom field “ean_code”)
    2d.) Check if user has pressed Enter

    If all of the above is true, then instantly add the the product(variation) to the cart!

    Can you please help me with that?
    I think that perhaps could be real cool feature for your plugin!
    If not possible at your team, perhaps you can help me with custom snippet …

    Thanks.

    #39228
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I wish I could suggest a code snippet to this, but it is a bit more complicated.

    I can however point you to the right direction of how I would start off. First, I would use the advanced content field to print some custom HTML with the EAN code. This HTML code can be used on the front-end to check if the product should be added.
    Something like:
    {descriptionfield}[<div class="ean_exists">{ean_code}</div>]

    Then using the asp_search_end hook to check if that ean is in the results, an exactly only once:

    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_with_ean = document.querySelectorAll( '.asp_r_' + id + '_' + instance + ' .ean_exists' );
    		if ( results_with_ean.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
    }

    This does not handle enter key press etc.., but should be a very good start to a possible solution.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39233
    pprosch41
    pprosch41
    Participant
    You cannot access this content.
    #39234
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39236
    pprosch41
    pprosch41
    Participant
    You cannot access this content.
    #39239
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure!

    It basically executes, whenever a search is finished, it finds the first result, and if an element with the “button” class exists (which is the add to cart button), then clicks it.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.