通过一个简单的“下拉框”替换“输入”字段不能在PHP代码中工作?

时间:2022-07-04 17:31:56

I encounter an ennoying problem which I can not solve and keeps me busy already 1 day. In the checkout page of my ecommerce Opencart2 program, people get an overview of their products in the cart before they are transferred to the payment page. At the checkout page, they still have the possibility to change the desired quantity of the products. When they change the quantity, they have to click on the refresh button to reculate the cart total.

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

In the original opencart script, the quantity can be changed by means of a simple input field. But I want to replace this quantity input field by a drop down list based on the number of products in stock (so they can never exceed the stock quantity). I have applied this succesfully throughout all my website (intermediate cart display, product pages, etcetera). But only at the final checkout page, it is not working. The drop down list nicely appears and is showing the possible quantities from 1, 2, 3 ... to maximum quantity) when they click on the arrow of the drop down.

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

Dropdown example

But when they select a quantity and click on the refresh button, they are returned to the cart overview page telling that there is no product anymore in the cart.

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

I assume that the quantity is not (empty) or not properly (value zero) posted. Please could somebody have a look at the scripts below and check if there is a possible code error? It's driving me crazy and it would make my day. Besides, is there an easy way (mozilla addon) to check the quantity value that is passed between the different pages.

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

Thanks, SabKo

  1. The original code in the cart.tpl page with the input field has the following code (and is working):

    带有输入字段的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. I have changed the script in the cart.tpl page as following:

    我已经更改了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. The class "btn-update" initiates the following script in the checkout.tpl:

    “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. The cart_update code at my checkout.php page is the following:

    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;
    

    }

1 个解决方案

#1


0  

Here's your trouble:

这是你的麻烦:

// 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();

Hope that helps and you can carry on with your work :)

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

#1


0  

Here's your trouble:

这是你的麻烦:

// 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();

Hope that helps and you can carry on with your work :)

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