From 15f84aaa44da586afa31bbd3e492c14a81b86b54 Mon Sep 17 00:00:00 2001 From: craftwerk Date: Thu, 16 Feb 2023 10:26:23 +0100 Subject: [PATCH] Update and improve `quantity-input.php` --- scss/bootscore_woocommerce/_wc_qty_btn.scss | 5 + woocommerce/global/quantity-input.php | 20 +-- woocommerce/js/woocommerce.js | 158 ++++++++++---------- 3 files changed, 94 insertions(+), 89 deletions(-) diff --git a/scss/bootscore_woocommerce/_wc_qty_btn.scss b/scss/bootscore_woocommerce/_wc_qty_btn.scss index ca7b20a0..ea66d1dd 100644 --- a/scss/bootscore_woocommerce/_wc_qty_btn.scss +++ b/scss/bootscore_woocommerce/_wc_qty_btn.scss @@ -27,3 +27,8 @@ WooCommerce Quantity Input @extend :disabled; } } + +// Hide quantity input if product is sold individually +.sold-individually .quantity { + display: none; +} diff --git a/woocommerce/global/quantity-input.php b/woocommerce/global/quantity-input.php index b15de390..e770ed46 100644 --- a/woocommerce/global/quantity-input.php +++ b/woocommerce/global/quantity-input.php @@ -12,7 +12,10 @@ * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates - * @version 7.2.1 + * @version 7.4.0 + * + * @var bool $readonly If the input should be set to readonly mode. + * @var string $type The input type attribute. */ defined( 'ABSPATH' ) || exit; @@ -20,13 +23,6 @@ /* translators: %s: Quantity. */ $label = ! empty( $args['product_name'] ) ? sprintf( esc_html__( '%s quantity', 'woocommerce' ), wp_strip_all_tags( $args['product_name'] ) ) : esc_html__( 'Quantity', 'woocommerce' ); -// In some cases we wish to display the quantity but not allow for it to be changed. -if ( $max_value && $min_value === $max_value ) { - $is_readonly = true; - $input_value = $min_value; -} else { - $is_readonly = false; -} ?>
- + id="" class="input-text qty text form-control " name="" @@ -50,7 +46,7 @@ class="input-text qty text form-control " max="" - + step="" placeholder="" inputmode="" diff --git a/woocommerce/js/woocommerce.js b/woocommerce/js/woocommerce.js index 3d60f9e7..900eb6fe 100644 --- a/woocommerce/js/woocommerce.js +++ b/woocommerce/js/woocommerce.js @@ -1,78 +1,82 @@ -jQuery(function ($) { - - // Review Checkbox Products - $('.comment-form-cookies-consent').addClass('form-check'); - $('#wp-comment-cookies-consent').addClass('form-check-input'); - $('.comment-form-cookies-consent label').addClass('form-check-label'); - - // Checkout Form Validation - $('body').on('blur change', '.form-row input', function () { - $('.woocommerce form .form-row.woocommerce-validated .select2-container, .woocommerce form .form-row.woocommerce-validated input.input-text, .woocommerce form .form-row.woocommerce-validated select, .woocommerce form .form-row.woocommerce-validated .form-check-input[type=checkbox]').removeClass('is-invalid').addClass('is-valid'); - $('.woocommerce form .form-row.woocommerce-invalid .select2-container, .woocommerce form .form-row.woocommerce-invalid input.input-text, .woocommerce form .form-row.woocommerce-invalid select, .woocommerce form .form-row.woocommerce-invalid .form-check-input[type=checkbox]').removeClass('is-valid').addClass('is-invalid'); - }); - - // Single-product Tabs - // First item active - $('.wc-tabs .nav-item:first-child a').addClass('active'); - - // Set active class to nav-link - $('body').on('click', '.wc-tabs li a', function (e) { - e.preventDefault(); - var $tab = $(this); - var $tabs_wrapper = $tab.closest('.wc-tabs-wrapper, .woocommerce-tabs'); - var $tabs = $tabs_wrapper.find('.wc-tabs'); - - $tabs.find('li a').removeClass('active'); - $tabs_wrapper.find('.wc-tab, .panel:not(.panel .panel)').hide(); - - $tab.closest('li a').addClass('active'); - $tabs_wrapper.find($tab.attr('href')).show(); - }); - // Single-product Tabs End - - // WC Quantity Input - if (!String.prototype.getDecimals) { - String.prototype.getDecimals = function () { - var num = this, - match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); - if (!match) { - return 0; - } - return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0)); - }; - } - // Quantity "plus" and "minus" buttons - $(document.body).on('click', '.plus, .minus', function () { - var $qty = $(this).closest('.quantity').find('.qty'), - currentVal = parseFloat($qty.val()), - max = parseFloat($qty.attr('max')), - min = parseFloat($qty.attr('min')), - step = $qty.attr('step'); - - // Format values - if (!currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0; - if (max === '' || max === 'NaN') max = ''; - if (min === '' || min === 'NaN') min = 0; - if (step === 'any' || step === '' || step === undefined || parseFloat(step) === 'NaN') step = 1; - - // Change the value - if ($(this).is('.plus')) { - if (max && currentVal >= max) { - $qty.val(max); - } else { - $qty.val((currentVal + parseFloat(step)).toFixed(step.getDecimals())); - } - } else { - if (min && currentVal <= min) { - $qty.val(min); - } else if (currentVal > 0) { - $qty.val((currentVal - parseFloat(step)).toFixed(step.getDecimals())); - } - } - - // Trigger change event - $qty.trigger('change'); - }); - // WC Quantity Input End - +jQuery(function ($) { + + // Review Checkbox Products + $('.comment-form-cookies-consent').addClass('form-check'); + $('#wp-comment-cookies-consent').addClass('form-check-input'); + $('.comment-form-cookies-consent label').addClass('form-check-label'); + + // Checkout Form Validation + $('body').on('blur change', '.form-row input', function () { + $('.woocommerce form .form-row.woocommerce-validated .select2-container, .woocommerce form .form-row.woocommerce-validated input.input-text, .woocommerce form .form-row.woocommerce-validated select, .woocommerce form .form-row.woocommerce-validated .form-check-input[type=checkbox]').removeClass('is-invalid').addClass('is-valid'); + $('.woocommerce form .form-row.woocommerce-invalid .select2-container, .woocommerce form .form-row.woocommerce-invalid input.input-text, .woocommerce form .form-row.woocommerce-invalid select, .woocommerce form .form-row.woocommerce-invalid .form-check-input[type=checkbox]').removeClass('is-valid').addClass('is-invalid'); + }); + + // Single-product Tabs + // First item active + $('.wc-tabs .nav-item:first-child a').addClass('active'); + + // Set active class to nav-link + $('body').on('click', '.wc-tabs li a', function (e) { + e.preventDefault(); + var $tab = $(this); + var $tabs_wrapper = $tab.closest('.wc-tabs-wrapper, .woocommerce-tabs'); + var $tabs = $tabs_wrapper.find('.wc-tabs'); + + $tabs.find('li a').removeClass('active'); + $tabs_wrapper.find('.wc-tab, .panel:not(.panel .panel)').hide(); + + $tab.closest('li a').addClass('active'); + $tabs_wrapper.find($tab.attr('href')).show(); + }); + // Single-product Tabs End + + // WC Quantity Input + if (!String.prototype.getDecimals) { + String.prototype.getDecimals = function () { + var num = this, + match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); + if (!match) { + return 0; + } + return Math.max(0, (match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0)); + }; + } + // Quantity "plus" and "minus" buttons + $(document.body).on('click', '.plus, .minus', function () { + var $qty = $(this).closest('.quantity').find('.qty'), + currentVal = parseFloat($qty.val()), + max = parseFloat($qty.attr('max')), + min = parseFloat($qty.attr('min')), + step = $qty.attr('step'); + + // Format values + if (!currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0; + if (max === '' || max === 'NaN') max = ''; + if (min === '' || min === 'NaN') min = 0; + if (step === 'any' || step === '' || step === undefined || parseFloat(step) === 'NaN') step = 1; + + // Change the value + if ($(this).is('.plus')) { + if (max && currentVal >= max) { + $qty.val(max); + } else { + $qty.val((currentVal + parseFloat(step)).toFixed(step.getDecimals())); + } + } else { + if (min && currentVal <= min) { + $qty.val(min); + } else if (currentVal > 0) { + $qty.val((currentVal - parseFloat(step)).toFixed(step.getDecimals())); + } + } + + // Trigger change event + $qty.trigger('change'); + }); + + // Make quantity input visible in product page if only 1 product is in stock + // WooCommerce 7.4.0 https://github.com/woocommerce/woocommerce/blob/2bf9f577952d7a225365fe7728b3187b5be7b701/plugins/woocommerce/templates/global/quantity-input.php + $('.quantity [max="1"]').attr('type', 'number'); + // WC Quantity Input End + }); // jQuery End \ No newline at end of file