编程语言
首页 > 编程语言> > javascript-在opencart中将“加”,“减”代替“添加到购物车”

javascript-在opencart中将“加”,“减”代替“添加到购物车”

作者:互联网

我想用OpenCart 2.0.1.1中的加号和减号2个按钮替换添加到购物车,现在我无法正确编码减号按钮.
我在catalog / view / theme / * / template / module / featured.tpl中添加了加号和减号按钮,并在catalog / controller / api / cart.php和common.js中进行了调用,我将url像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及其数量.要减少数量,我们需要检查数量是否大于1(如果大于1),那么我们可以减少数量,否则,如果数量只有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文件中编写.
的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 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是控制器检出和函数递减的路径.这是

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 code keep same}

现在您是否注意到我们通过传递数量和1来调用库函数decrement_product_quantity.这里的键不过是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,它将减少,否则将删除整个产品.
希望您理解,如果您有任何疑问,请告诉我.也希望您也可以增加.祝好运

标签:opencart,opencart2-x,javascript,php,ajax
来源: https://codeday.me/bug/20191009/1880494.html