栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

为了减少您的产品数量,您需要

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,它将减少,否则将删除整个产品。希望您能理解,如果您有任何疑问,请告诉我。也希望您也可以为增量做。祝好运



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号