在 opencart 中添加“加”“减”代替“添加到购物车”

2023-12-13

我想用 OpenCart 2.0.1.1 中的 2 个加号和减号按钮替换添加到购物车,现在我无法正确编码减号按钮。 我在中添加了加号和减号按钮catalog/view/theme/*/template/module/featured.tpl并拨打电话catalog/controller/api/cart.php and in common.js我已经把网址像url: 'index.php?route=checkout/cart/minus其余代码如下

system/library/cart.php

public function minus($product_id, $qty)
{
    $this->data = array();
    $qnt1 = 1;
    $product['product_id'] = (int)$product_id;
    $key = base64_encode(serialize($product));
    if ((int)$qty && ((int)$qty > 0)) {
        if (!isset($this->session->data['cart'][$key])) {
            $this->session->data['cart'][$key]-= (int)$qty;
        }
        else {
            $this->remove($key);
        }
    }
}

[Image for plus minus button in place of "Add to cart" Button] [1]

为了减少您需要的产品数量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,则会递减,否则会删除整个产品。 希望您理解,如果您有任何疑问,请告诉我。也希望你也能为增量做点什么。祝你好运

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 opencart 中添加“加”“减”代替“添加到购物车” 的相关文章

随机推荐