Created
December 24, 2014 18:00
-
-
Save WillBrubaker/dfd8e3a5cb53c0ebbf47 to your computer and use it in GitHub Desktop.
WooCommerce Name Your Price - disable input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery(document).ready(function($) { | |
| //get the price in the input | |
| var originalPrice = $('input#nyp').val() | |
| var newPrice = originalPrice; | |
| //need the product id so a cookie can be set against it, probably won't work if I change themes - but works with storefront | |
| var productId = parseInt($('main#main > div').attr('id').replace('product-', '')) | |
| //does a cookie exist for this product id? | |
| var cookie = getCookie('haggledon-' + productId) | |
| if (cookie != null) {//the cookie exists, therefore this person has 'haggled' within 24 hours. Disable the price input. | |
| $('input#nyp').prop('disabled', 'true') | |
| } | |
| $('input#nyp').on('blur', function() {//have they input a 'haggle' price? | |
| newPrice = $('input#nyp').val(); | |
| if (newPrice != originalPrice) {//prices differ, they only get one chance, disable the input | |
| $('input#nyp').prop('disabled', 'true') | |
| if (null == cookie) {//write the cookie if it doesn't exist | |
| cookie = "haggledon-" + productId + "=true;path=/;max-age=86400"; | |
| document.cookie = cookie | |
| } | |
| } | |
| }) | |
| }) | |
| function getCookie(name) {//reads cookies, returns the one named 'name' if exists, else returns null. Copy & paste from stackexchange or something so I didn't learn anything here :( | |
| var re = new RegExp(name + "=([^;]+)"); | |
| var value = re.exec(document.cookie); | |
| return (value != null) ? unescape(value[1]) : null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment