3

I'm developing a site with WooCommerce and have used the plugin Product Addons to add extra fields of text to the item being purchased (name, email address, phone number, etc). When someone clicks on "Add to Cart", the site currently adds the product to the cart, refreshes the page, but the previous data remains in the fields. I want to reset all the fields and make them empty after the product has been added.

I tried using this function:

( function($) {
    $( document.body ).on( 'added_to_cart', function() { 
            $('input').val('');  } 
        );
} ) ( jQuery );

Any suggestions?

MacGyver_97
  • 229
  • 3
  • 14
  • You might want to look at this: https://stackoverflow.com/questions/6364289/clear-form-fields-with-jquery –  Oct 16 '17 at 18:07
  • Not sure this will work, since I need to make sure the contents of the form are submitted to the cart, prior to resetting the fields. – MacGyver_97 Oct 16 '17 at 19:45

2 Answers2

1

If the page literally refreshes itself then it is not at all standard. It was done just to update the mini cart at the top right corner of your menu and products are being added through ajax. In this case you can't empty all fields on some event because the page is being refreshed, what you have to do is write you code under document.ready function, perform $.each function on the common class or input and empty the input fields.

Logan Young
  • 385
  • 2
  • 9
  • Sorry, not sure what you mean. The page isn't refreshing itself, only does so when someone clicks on the Add to Cart button. – MacGyver_97 Oct 16 '17 at 19:46
  • When you click on normal add to cart button on normal woocommerce then it posts the product data and adds products to cart via form but in your case as soon as you are clicking on add to cart button products are being added to cart via ajax and after success your javascript is reloading the page. That is what i meant by refreshing or reloading the page. – Logan Young Oct 17 '17 at 02:53
0

Try this action hook in your plugin or theme functions.php file

add_filter( 'woocommerce_checkout_get_value', 'misha_clear_checkout_fields_vals' );

function misha_clear_checkout_fields_vals($input){
        return '';
}
Misha Rudrastyh
  • 866
  • 10
  • 16
  • Hm. This doesn't seem to work. I was indeed looking for a hook, thanks, but this one doesn't empty the fields. Perhaps because the fields are generated from a plugin, not from Woocommerce itself. The fields have names as such: name="addon-484-last-name-3[0]" and so forth. – MacGyver_97 Oct 16 '17 at 19:43