【问题标题】:How to empty basket before adding a product - Open Cart添加产品前如何清空购物篮 - 打开购物车
【发布时间】:2014-04-28 16:09:20
【问题描述】:

点击此链接: Opencart: Adding Buy Now Button in Opencart Product Page

我在我的页面上添加了以下脚本:

 $('#button-cart-buy').bind('click', function() {
                $.ajax({
                    url: 'index.php?route=checkout/cart/add',
                    type: 'post',
                    data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
                    dataType: 'json',
                    success: function(json) {
                        $('.success, .warning, .attention, information, .error').remove();

                        if (json['error']) {
                            if (json['error']['option']) {
                                for (i in json['error']['option']) {
                                    $('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
                                }
                            }

                            if (json['error']['profile']) {
                                $('select[name="profile_id"]').after('<span class="error">' + json['error']['profile'] + '</span>');
                            }
                        } 

                        if (json['success']) {window.location='<?php echo $this->url->link('checkout/checkout', '', 'SSL'); ?>'; 
                        }

                    }
                });
            });

但是,我想在添加相关产品之前清空购物车。

有谁知道怎么做或者有什么建议吗?

【问题讨论】:

    标签: opencart


    【解决方案1】:

    最简单的方法是编辑购物车类/system/library/cart.php

    找到这一行(从 1.5.5.1 安装 - 任何其他版本可能略有不同,但不太可能大不相同)

    public function add($product_id, $qty = 1, $option = array()) {
    

    在它之后的新行中添加

    $this->cart->clear();
    

    然后保存。请注意,这允许客户将大于 1 的数量添加到购物车中,只要它是相同的产品即可

    【讨论】:

    • 我猜它永远不会允许用户添加多个项目(不同类型),即使他们使用的是add to cart 而不是buy now。我想更好的办法是传递一个变量,指示请求来自 buy now button 与 ajax,然后在 controller/checkout/cart/add 中,如果 $_POST['buy_now'] 存在则执行 $this-&gt;cart-&gt;clear();。也在图书馆 cart.php 我想它应该$this-&gt;clear();
    • @ChetanPaliwal - 没错。这是一种快速而肮脏的方法,可以毫不费力地完成 OP 想要的操作。做你建议的事情需要相当多的工作
    • @ChetanPaliwal 请同时添加正确答案。在这里拥有它会很好,因为其他人可能会搜索相同/相似的解决方案。完成后,回复我的评论,以便我收到通知并支持正确的方法。
    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2011-04-09
    • 2011-12-19
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多