当前位置: 首页 > 面试题库 >

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

范甫
2023-03-14
问题内容

我想用OpenCart
2.0.1.1中的加号和减号2个按钮替换添加到购物车,现在我无法正确编码减号按钮。我在其中添加了加号和减号按钮catalog/view/theme/*/template/module/featured.tpl并进行调用,catalog/controller/api/cart.php并在其中添加了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和数量。要减少数量,我们需要检查数量是否大于1(如果大于1),那么我们可以减少数量,否则,如果数量只有1,我们需要删除整个产品。

您需要更改的是在视图页面上添加加号,减号图标,然后依次是控制器和库,然后将ajax结果发送回去。我将尝试使其尽可能地容易。

在我的情况下,让我们从查看页面开始,这是products.tpl我writteb获得的代码加上减号按钮是

  <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}

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



 类似资料:
  • 问题内容: 单击后,我想重新标记“ 添加到购物车” 按钮,然后将一项添加到购物车, 再添加到购物车中 。 这可能吗? 我有第二个 转到购物车* 按钮的Child-Theme function.php ,并且它正在工作。 * 但是我不知道在将一件商品添加到购物车后如何解决此重新标签问题(商店仅出售一件尺寸不同的商品)。我希望我很清楚。 这是我的代码: 问题答案: 更新: 在下面,您将找到使此重新标记

  • 最初,我选择了一个包(Woo订阅) 然后添加了所有详细信息。 但未提交。 回到网站,所以再次购买我需要选择一个包。所以我选择了这个包,并填写了详细信息,然后转到付款包。 现在在我的购物车中,两个包都存在(即我没有第一次购买就选择的包和最近的包) 如何修复此问题,使最新选定的一个在购物车中,而较早的一个在选定最新的一个后立即删除。 我尝试了这个Woocommerce从购物车中删除所有产品,并将当前产

  • 在商品分类页面上,当有人点击“添加到购物车”时,WooCommerce通过Ajax在此按钮下方添加“查看购物车”。我发现处理这个的脚本是 /assets/js/frontend/add-to-cart.js 现在,我想添加"Procceed to check out",这样某人就可以立即去结账。 这是脚本的输出: 有没有人做过类似的事情?

  • WooCommerce插件添加到购物车每件物品更多的费用。 客户在购物车中选择以下内容: 产品A(大项目)-300美元 产品B(大项目)-500美元 产品C(普通项目)-30美元 产品D(普通商品)-10美元 让我们假设“大项目-正常项目”的规则如下: 大件商品-收费: 20美元/件 正常项目-收费:低于150美元,免费送货;超过150美元,将收取20美元的费用 如何让它自动当点击添加到购物车按钮

  • 本文向大家介绍Android 购物车加减功能的实现代码,包括了Android 购物车加减功能的实现代码的使用技巧和注意事项,需要的朋友参考一下 Android 实现购物车加减功能,效果图如下所示: 到此这篇关于Android 购物车加减功能的实现代码的文章就介绍到这了,更多相关android 购物车加减内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

  • 我想在单个产品页面中向“添加到购物车”按钮显示产品价值(版本) 我找到了必须编辑的文件:woocommerce\templates\single-product\add-to-cart\external.php 但现在我的“添加到购物车”按钮上什么也没有显示