用一个简单的 "drop down box" 替换 "input" 字段在 php 代码中不起作用?

Replacement of "input" field by a simple "drop down box" not working in php code?

我遇到了一个我无法解决的烦人问题,让我忙了 1 天。 在我的电子商务 Opencart2 程序的结帐页面中,人们在转移到付款页面之前会先了解购物车中的产品概况。在结帐页面,他们仍然可以更改所需的产品数量。当他们更改数量时,他们必须单击刷新按钮以重新计算购物车总数。

在原始的opencart脚本中,可以通过一个简单的输入字段来更改数量。但是我想用一个基于库存产品数量的下拉列表替换这个数量输入字段(这样它们就永远不会超过库存数量)。我已经在我的所有网站(中间购物车展示、产品页面等)中成功应用了这一点。但只有在最后的结帐页面,它才不起作用。当他们单击下拉列表的箭头时,下拉列表很好地显示并显示从 1、2、3 ... 到最大数量的可能数量。

Dropdown example

但是当他们 select 数量并单击刷新按钮时,他们将返回到购物车概览页面,告知购物车中不再有产品。

我假设数量不是(空的)或没有正确(值为零)。请有人看看下面的脚本并检查是否存在可能的代码错误?这让我发疯,这会让我开心。此外,是否有一种简单的方法(mozilla 插件)来检查在不同页面之间传递的数量值。

谢谢, 萨布科

  1. 带输入框的cart.tpl页面中的原代码有如下代码(并且有效):

                    <td class="text-left quantity">
                    <div class="input-group btn-block" style="max-width: 200px;">
                        <input type="text" name="quantity[<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>]" value="<?php echo $product['quantity']; ?>" size="1" class="form-control" />
                        <span class="input-group-btn">
                            <button type="submit" data-toggle="tooltip" title="<?php echo $button_update; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>,<?php echo $product['quantity']; ?>" class="btn btn-primary btn-update" ><i class="fa fa-refresh"></i></button>
                            <button type="button" data-toggle="tooltip" title="<?php echo $button_remove; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>" class="btn btn-danger  btn-delete"><i class="fa fa-times-circle"></i></button>
                        </span>
                    </div>
                </td>
    
  2. 我已将 cart.tpl 页面中的脚本更改如下:

                    <td class="text-left quantity">
                     <div class="input-group btn-block" style="max-width: 200px;">
    
                         <select name="quantity[<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>]" class="form-control_cart" id="input-quantity" value="<?php echo $product['quantity']; ?>" >
                         <?php foreach (range($product['minimum'], $product['stockhoeveelheid'], 1) as $stap) {
                              if ($stap == $product['quantity']) {
                                    echo "<option value='$stap' selected>$stap</option>";
                              } else {
                                    echo "<option value='$stap'>$stap</option>";
                              }
                            }  ?>
                         </select>
    
                        <span class="input-group-btn">
                            <button type="submit" data-toggle="tooltip" title="<?php echo $button_update; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>,<?php echo $product['quantity']; ?>" class="btn btn-primary btn-update" ><i class="fa fa-refresh"></i></button>
                            <button type="button" data-toggle="tooltip" title="<?php echo $button_remove; ?>" data-product-key="<?php echo $product[version_compare(VERSION, '2.1', '<') ? 'key' : 'cart_id']; ?>" class="btn btn-danger  btn-delete"><i class="fa fa-times-circle"></i></button>
                        </span>
                    </div>
                </td>
    
  3. class "btn-update" 在 checkout.tpl 中启动以下脚本:

    $(document).delegate('.checkout-product .input-group .btn-update', 'click', function () {
    var key = $(this).attr('data-product-key');
    var qty  = $('input[name="quantity[' + key + ']"]').val();
    $.ajax({
        url: 'index.php?route=journal2/checkout/cart_update',
        type: 'post',
        data: {
            key: key,
            quantity: qty
        },
        dataType: 'json',
        beforeSend: function() {
            triggerLoadingOn();
            $('#cart > button > a > span').button('loading');
            $('.checkout-cart').addClass('checkout-loading');
        },
        complete: function() {
            triggerLoadingOff();
            $('#cart > button > a > span').button('reset');
        },
        success: function(json) {
            setTimeout(function () {
                $('#cart-total').html(json['total']);
            }, 100);
    
            if (json['redirect']) {
                location = json['redirect'];
            } else {
                $('#cart ul').load('index.php?route=common/cart/info ul li');
    
                $(document).trigger('journal_checkout_reload_payment');
                $(document).trigger('journal_checkout_reload_shipping');
            }
        }
    });
    

    });

    $(document).delegate('.checkout-product .input-group .btn-delete', 'click', function () {
    var key = $(this).attr('data-product-key');
    $.ajax({
        url: 'index.php?route=journal2/checkout/cart_delete',
        type: 'post',
        data: {
            key: key
        },
        dataType: 'json',
        beforeSend: function() {
            triggerLoadingOn();
            $('#cart > button > a > span').button('loading');
            $('.checkout-cart').addClass('checkout-loading');
        },
        complete: function() {
            triggerLoadingOff();
            $('#cart > button > a > span').button('reset');
        },
        success: function(json) {
            setTimeout(function () {
                $('#cart-total').html(json['total']);
            }, 100);
    
            if (json['redirect']) {
                location = json['redirect'];
            } else {
                $('#cart ul').load('index.php?route=common/cart/info ul li');
    
                $(document).trigger('journal_checkout_reload_payment');
                $(document).trigger('journal_checkout_reload_shipping');
            }
        }
    });
    

    });

  4. 我的 checkout.php 页面上的 cart_update 代码如下:

    public function cart_update() {
    $key = Journal2Utils::getProperty($this->request->post, 'key');
    $qty = Journal2Utils::getProperty($this->request->post, 'quantity');
    $this->cart->update($key, $qty);
    
    $json = array();
    
    if (!$this->checkCart()) {
        $json['redirect'] = Journal2Utils::link('checkout/cart');
    } else {
        $json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), Journal2Utils::currencyFormat($this->model_journal2_checkout->getTotal()));
    }
    
    echo json_encode($json);
    exit;
    

    }

这是你的麻烦:

// As you can see, you're still trying to fetch the value from the input/textbox when you've changed it to a select.
var qty  = $('input[name="quantity[' + key + ']"]').val();

// So change the above like so:
var qty  = $('select[name="quantity[' + key + ']"]').val();

希望对您有所帮助,您可以继续您的工作:)