Please consider leaving a donation if you appreciate this information. Lightning network now available
Snippet UPDATED FOR WooCommerce 2.6
By default, WooCommerce Subscriptions allows for users to cancel their subscriptions via their “My Account” page via a “Cancel” button. Currently, there is no confirmation – the workflow goes something like this: press cancel button, subscription is cancelled.
For some shop owners this may be undesirable – these shop owners may wish to prevent accidental cancellation. There are a couple of different approaches to this. Probably the most elegant and most WordPress way to do this would be to create a JavaScript file that watches for a click on the cancel button, pop-up a confirmation dialog and allow the user a chance to cancel the cancellation. The JavaScript needed is pretty simple – you can see an example here:
Getting that JavaScript enqueued properly, however, may not be quite as straightforward – if you’ve had some experience with conditionally enqueuing JavaScript in WordPress you can probably stop here and go forward and prevent accidental cancellations. If, on the other hand, you’re not quite sure how to implement that, I can offer a couple of workarounds.
The first would simply echo out the JavaScript on the WooCommerce provided action hook woocommerce_after_my_account – something like this:
The other way to handle this would be via a template override. You can read more about how template overrides work with WooCommerce here: Template Structure + overriding templates via a theme – Subscriptions overrides templates in a very similar fashion and in fact, the template override path for Subscriptions is the same as it is for WooCommerce! It is considered best practice to only override the templates that you need to – in this case the file is called my-subsriptions.php and the default file can be found in wp-content/plugins/woocommerce-subscriptions/templates/woocommerce-subscriptions/myaccount/ and you would override it in wp-content/themes/YOUR-THEME/woocommerce/myaccount/ – there’s not much difference between doing it this way or using a filter, simply add the JavaScript to the end of the template override file and – Shazam! confirmation dialog when pressing the cancel button!
Here’s an example of the edits – compare this to the default file:
If you would like to download, Register or LoginNot sure what to do with code snippets? See What Do I Do With These Code Snippets?
Please consider leaving a donation if you appreciate this information. Lightning network now available
Leave a Reply
You must be registered and logged in to post a comment.