为了减少您的产品数量,您需要
product_id和数量。要减少数量,我们需要检查数量是否大于1(如果大于1),那么我们可以减少数量,否则,如果数量只有1,我们需要删除整个产品。
您需要更改的是在视图页面上添加加号,减号图标,然后依次是控制器和库,然后将ajax结果发送回去。我将尝试使其尽可能地容易。
在我的情况下,让我们从查看页面开始,这是
products.tpl我writteb获得的代码加上减号按钮是
<table > <tbody> <?php foreach ($products as $product) { ?> <tr > <td > <input type="text" disabled="true" value="<?php echo $product['quantity']; ?>" > </td> <td > <a href="<?php echo $product['href']; ?>"> <b><?php echo $product['name']; ?></b> </a> </td> <td > <i onclick="cart.remove('<?php echo $product['key']; ?>');"></i> </td> </tr> <tr> <td colspan="2" > <div role="group"> <button type="button" onclick="cart.decrement('<?php echo $product['product_id']; ?>');"> <i ></i> </button> <button type="button" onclick="cart.add('<?php echo $product['product_id']; ?>');"> <i ></i> </button> </div> </td> <td > <b><?php echo $product['total']; ?></b> </td> </tr> <?php } ?>在这里,我使javascript
ajax调用onclick。因此,让我们看看该调用的作用。我写在同一页上,如果需要,您可以写在任何.js文件中。script.js
'decrement': function(key) {$.ajax({url: 'index.php?route=checkout/cart/decrement',type: 'post',data: 'key=' + key,dataType: 'json',beforeSend: function() {$('#cart > button').button('loading');},complete: function() {$('#cart > button').button('reset');},success: function(json) {// Need to set timeout otherwise it wont update the totalsetTimeout(function () {$('#cart > button').html('<span id="cart-total"><i ></i> ' + json['total'] + '</span>');}, 100);if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {location = 'index.php?route=checkout/cart';} else {$('#cart > ul').load('index.php?route=common/cart/info ul li');}}});}现在,我们从ajax调用上方调用的url是控制器检出和函数递减的路径。这是
controller.php
public function decrement() { $this->load->language('checkout/cart'); $json = array(); // Remove if (isset($this->request->post['key'])) { $this->cart->decrement_product_quantity($this->request->post['key'],1); unset($this->session->data['vouchers'][$this->request->post['key']]); $this->session->data['success'] = $this->language->get('text_remove'); // rest of the pre keep same}现在您是否注意到我们
decrement_product_quantity通过传递数量和1
来调用库函数。这里的键不过是ajax参数,即
product_id。
现在库中的最终功能
public function decrement_product_quantity($product_id, $qty = 1){$this->data = array();$product['product_id'] = (int)$product_id;$key = base64_enpre(serialize($product));if ((int)$qty && ((int)$qty > 0)) {if ($this->session->data['cart'][$key]>1) {$this->session->data['cart'][$key] -= (int)$qty;} else {$this->remove($key);}}}这将检查购物车,如果数量大于1,它将减少,否则将删除整个产品。希望您能理解,如果您有任何疑问,请告诉我。也希望您也可以为增量做。祝好运



