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 7 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 5 months ago.
- AuthorPosts
- September 5, 2022 at 10:33 am #39203
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 EnterIf 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.
September 7, 2022 at 9:48 am #39228Hi,
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 :)
September 7, 2022 at 11:43 am #39233You cannot access this content.September 7, 2022 at 12:12 pm #39234You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
September 7, 2022 at 1:59 pm #39236You cannot access this content.September 10, 2022 at 12:09 pm #39239Sure!
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 :)
October 15, 2022 at 10:45 am #39662Hi Ernest,
I have “rethinked” my (old) request, after trying your suggestion but did not get it to work …
Perhaps you can provide me a code snippet to automatically add cart if an exact search (only 1 search result) exists.
Without the relying on a custom field like ean-code …It is then a more general use for your plugin too.
Perhaps a solution for that is much/a little simlpler and you can help me with that!?
Thanks again.
October 17, 2022 at 12:54 pm #39670Hi,
Of course. I am not sure how simpler it might get, but this should be very close to an actual solution:
Best,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 }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.