【问题标题】:Create a custom quantity field in Woocommerce在 Woocommerce 中创建自定义数量字段
【发布时间】:2018-08-03 20:48:03
【问题描述】:

我目前正在使用 wordpress 开发一个电子商务网站,试图改变 qty spinner 在购物车页面上的显示方式,我使用以下代码编辑了模板文件 quantity-input.php:

if ( $max_value && $min_value === $max_value ) {
    ?>
    <div class="quantity hidden">
        <input type="hidden" id="<?php echo esc_attr( $input_id ); ?>" class="qty" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $min_value ); ?>" />
    </div>
    <?php
} else {
    ?>
    <div class="quantity">
        <div class="quantity-button quantity-up">+</div>
        <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></label>
        <input type="number" id="<?php echo esc_attr( $input_id ); ?>" class="input-text qty text" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( 0 < $max_value ? $max_value : '' ); ?>" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" size="4" pattern="<?php echo esc_attr( $pattern ); ?>" inputmode="<?php echo esc_attr( $inputmode ); ?>" />
        <div class="quantity-button quantity-down">-</div>
    </div>
    <?php

以及附加到页面的脚本:

$('.quantity-up').html('<span>+</span>');
    $('.quantity-down').html('<span>-</span>');
    $('.quantity').each(function() {
        var spinner = $(this),
        input = spinner.find('input[type="number"]'),
        btnUp = spinner.find('.quantity-up'),
        btnDown = spinner.find('.quantity-down'),
        min = input.attr('min'),
        max = input.attr('max');

        btnUp.click(function() {
            var oldValue = parseFloat(input.val());
            if (oldValue >= max) {
              var newVal = oldValue;
            } else {
              var newVal = oldValue + 1;
            }
            spinner.find("input").val(newVal);
            spinner.find("input").trigger("change");
          });
      btnDown.click(function() {
        var oldValue = parseFloat(input.val());
        if (oldValue <= min) {
          var newVal = oldValue;
        } else {
          var newVal = oldValue - 1;
        }
        spinner.find("input").val(newVal);
        spinner.find("input").trigger("change");
      });
});

当我运行它时,似乎只有 btnDown 有效,而 btnUp 无效,我不知道我做错了什么

哦,CSS:

.quantity {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    height: 32px;
    border: solid 1px red;
  }

  input[type=number]::-webkit-inner-spin-button,
  input[type=number]::-webkit-outer-spin-button{
    -webkit-appearance: none;
    margin: 0;
  }

  input[type=number]{
    -moz-appearance: textfield;
  }

  .quantity input {
    width: 35px;
    height: 32px;
    line-height: 1.65;
    display: block;
    padding: 0;
    margin: 0;
    border: none;
  }

  .quantity input:focus {
    outline: 0;
  }

  .quantity-button {
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    width: 35px;
    height: 100%;
    text-align: center;
    color: #fff;
    line-height: 1.7;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
    background-color: rgba(0,0,0,0.7);
    border: solid 1px red;
  }
  .quantity-button span{
    font-size: 1.2em;
    font-weight: 900;
  }

  .quantity-button.quantity-up {
  }

  .quantity-button.quantity-down {
  }

screenshot

【问题讨论】:

    标签: php jquery wordpress woocommerce product-quantity


    【解决方案1】:

    我对 PHP 和 CSS 做了一些轻微的改动,并且主要重写了 jQuery。

    PHP/HTML 代码:

    <?php // Begin "Just For testing" (to be removed)
    
        $min_value = 0;
        $max_value = -1;
        $input_value = 1;
        $step = 1;
        $pattern = '';
        $inputmode = 'numeric';
        $input_id = 'quantity-custom';
        $input_name = 'quantity';
    
    // -- End of "Just For testing"
    
    ## -- -- -- -- Real code start here below -- -- -- -- ## ?>
    
      <div class="quantity">
        <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></label>
        <div class="quantity-button minus"><span>-</span></div>
        <input type="number" id="<?php echo esc_attr( $input_id ); ?>" class="input-text qty text" step="<?php echo esc_attr( $step ); ?>" min="<?php echo esc_attr( $min_value ); ?>" max="<?php echo esc_attr( 0 < $max_value ? $max_value : '' ); ?>" name="<?php echo esc_attr( $input_name ); ?>"
          value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" size="4" pattern="<?php echo esc_attr( $pattern ); ?>" inputmode="<?php echo esc_attr( $inputmode ); ?>" />
        <div class="quantity-button plus"><span>+</span></div>
      </div>
    

    jQuery 代码:

    (function($){
        $('.quantity').on('click', '.quantity-button.minus',
            function(e) {
            $input = $(this).next('input.qty');
            var val = parseInt($input.val());
            var step = $input.attr('step');
            step = 'undefined' !== typeof(step) ? parseInt(step) : 1;
            if (val > 0) {
                $input.val( val - step ).change();
            }
        });
        $('.quantity').on('click', '.quantity-button.plus', function(e) {
            $input = $(this).prev('input.qty');
            var val = parseInt($input.val());
            var step = $input.attr('step');
            step = 'undefined' !== typeof(step) ? parseInt(step) : 1;
            $input.val( val + step ).change();
        });
    })(jQuery);
    

    CSS 仅更改

    .quantity-button span {
      font-size: 1.2em;
      font-weight: 900;
      color: white;
    }
    
    .quantity-button.plus {}
    
    .quantity-button.minus {}
    

    代码进入您的活动子主题(或主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2019-03-04
      • 2018-02-24
      相关资源
      最近更新 更多