为了减少您需要的产品数量product_id
及其数量。为了递减,我们需要检查 qty 是否大于 1,如果是,那么我们可以递减,否则,如果 qty 只有 1,我们需要删除整个产品。
您需要更改的是您的视图页面添加加号、减号图标,然后是控制器,然后是库,然后将 ajax 结果发送回来。我会尽量让这件事变得更容易。
让我们从查看页面开始,就我而言,它是products.tpl
我编写的获取加减按钮的代码是
<table class="table scroll">
<tbody>
<?php foreach ($products as $product) { ?>
<tr >
<td >
<input type="text" style="width: 20px; height: 20px;font-weight:700 " disabled="true" value="<?php echo $product['quantity']; ?>" class="text-center">
</td>
<td class="text-left">
<a href="<?php echo $product['href']; ?>">
<b><?php echo $product['name']; ?></b>
</a>
</td>
<td style=" text-align:right">
<i class="fa fa-minus-circle fa-2x" style="cursor: pointer;color:red;" onclick="cart.remove('<?php echo $product['key']; ?>');"></i>
</td>
</tr>
<tr>
<td colspan="2" style="border:0 none;">
<div class="btn-group form-inline" role="group">
<button type="button" style="height:25px;width:25px" class="btn btn-default btn-xs " onclick="cart.decrement('<?php echo $product['product_id']; ?>');">
<i class="fa fa-minus"></i>
</button>
<button type="button" style="height:25px;width:25px" class="btn btn-default btn-xs" onclick="cart.add('<?php echo $product['product_id']; ?>');">
<i class="fa fa-plus"></i>
</button>
</div>
</td>
<td style="border:0 none;" class="text-right" >
<b><?php echo $product['total']; ?></b>
</td>
</tr>
<?php } ?>
在这里我做了 javascript ajax 调用 onclick。那么让我们看看这个调用做了什么。我已在同一页面中编写,如果您愿意,您可以在任何 .js 文件中编写。
脚本.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 total
setTimeout(function () {
$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></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 是控制器签出和函数递减的路径。这是
控制器.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 code keep same}
现在你注意到我们正在调用库函数吗decrement_product_quantity
通过传递 qty 和 1。这里的 key 只是 ajax 参数,即product_id
.
现在库中的最终函数
public function decrement_product_quantity($product_id, $qty = 1){
$this->data = array();
$product['product_id'] = (int)$product_id;
$key = base64_encode(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,则会递减,否则会删除整个产品。
希望您理解,如果您有任何疑问,请告诉我。也希望你也能为增量做点什么。祝你好运