在opencart中用“加"“减"代替“添加到购物车" [英] Add 'plus' 'minus' in place of 'add to cart' in opencart

查看:65
本文介绍了在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之类,其余代码在下面

I want to replace add to cart with 2 buttons that are plus and minus in OpenCart 2.0.1.1, Now i am unable to code for minus button properly. I have added plus and mius button in catalog/view/theme/*/template/module/featured.tpl and make call in catalog/controller/api/cart.php and in common.js I have put url like url: 'index.php?route=checkout/cart/minus and rest of code is below

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,我们需要删除整个产品.

In order to decrement your product quantity you need product_id and its quantity. To decrement we need to check whether qty is greater than 1 or not if it is then we can decrement else we need to remove entire product if qty is only 1.

您需要更改的是在视图页面上添加plus,减号图标,然后控制器然后是库,然后将ajax结果发送回去.我将尝试使其尽可能简单.

Things you need to change is your view page add plus , minus icons there, then controller then library then send ajax result back. I will try to make this as easier as possible.

在我的情况下,让我们从查看页面开始,它是products.tpl我写得到的加减按钮的代码是

Lets start with view page in my case it is products.tpl the code i writteb to get plus minus buttons is

  <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

Here I have made javascript ajax call onclick. So lets see what that call does. I have written in same page you can write in any .js file if you wish. 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是控制器检出和函数递减的路径.这是

Now the url we calling from above ajax call is the path for our controller checkout and function decrement. Here is that

controller.php

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.

Now did you noticed we are calling library function decrement_product_quantity by passing qty and 1. Here key is nothing but ajax parameter which is 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,这将检查购物车,它将减少,否则将删除整个产品. 希望您能理解,如果您有任何疑问,请告诉我.也希望您也可以为增量做.祝你好运

This one checks cart if qty is greater than 1 it will decrement else remove the entire product. Hope you understood please let me know if any queries you have. Also hope you can do for increment too. good luck

这篇关于在opencart中用“加"“减"代替“添加到购物车"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆