Created
July 9, 2015 07:15
-
-
Save AchalJ/91ad2cc4f92cf26a392c to your computer and use it in GitHub Desktop.
WooCommerce Custom Input Quantity
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($){ | |
| $('.cart input[type="number"]').on('change', function(){ | |
| $(this).parent().parent().find('button[data-quantity]').attr('data-quantity', parseInt($(this).val())); | |
| }); | |
| jQuery.fn.disableSelection = function() { | |
| return this | |
| .attr('unselectable', 'on') | |
| .css('user-select', 'none') | |
| .on('selectstart', false); | |
| }; | |
| function load_ops(){ | |
| jQuery("div.quantity").append('<div class="inc icon">+</div>'); | |
| jQuery("div.quantity").prepend('<div class="dec icon">-</div>'); | |
| jQuery(".icon").on("click", function () { | |
| var $button = jQuery(this); | |
| var oldValue = $button.parent().find("input").val(); | |
| if ($button.text() === "+") { | |
| var newVal = parseFloat(oldValue) + 1; | |
| } else { | |
| /* Don't allow decrementing below zero */ | |
| if (oldValue > 1) { | |
| var newVal = parseFloat(oldValue) - 1; | |
| } else { | |
| newVal = 1; | |
| } | |
| } | |
| $button.parent().find("input").val(newVal); | |
| $button.parent().parent().find("button[data-quantity]").data("quantity", newVal); | |
| }); | |
| jQuery(".quantity").disableSelection(); | |
| } | |
| load_ops(); | |
| // To work with infinate scroll | |
| jQuery(window).on('scroll', function(){ | |
| jQuery(".quantity .icon").remove(); | |
| load_ops(); | |
| }); | |
| }); |
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
| .quantity .icon { | |
| float: left; | |
| width: 23%; | |
| padding: 3px 5px 4px; | |
| text-align: center; | |
| cursor: pointer; | |
| background: #eee; | |
| border: 1px solid #ddd; | |
| } | |
| input.qty, .quantity .qty { | |
| width: 40px; | |
| height: 35px; | |
| float: left; | |
| padding: 0 !important; | |
| margin: 0px 0 0; | |
| text-align: center; | |
| -webkit-border-radius: 2px 0 0 2px; | |
| border-radius: 2px 0 0 2px; | |
| box-shadow: inset 0 0 2px 0 #f7f6f7; | |
| -webkit-box-shadow: inset 0 0 2px 0 #f7f6f7; | |
| font-weight: 700; | |
| -moz-appearance: textfield; | |
| } | |
| input[type=number]::-webkit-inner-spin-button, | |
| input[type=number]::-webkit-outer-spin-button { | |
| -webkit-appearance: none; | |
| display: none; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment