【问题标题】:what happens when add to cart button clicks in opencart在 opencart 中单击添加到购物车按钮时会发生什么
【发布时间】:2013-05-02 18:14:42
【问题描述】:

谁能告诉我,当我们点击店面的“添加到购物车”按钮时,会执行哪个功能?单击“添加到购物车”按钮时将执行哪个代码或方法?我观察到发生了一些计算。

在视图/主题/*/template/product/product.tpl

 <input type="hidden" name="product_id" size="2" value="<?php echo $product_id; ?>" />
          &nbsp;
          <input type="button" value="<?php echo $button_cart; ?>" id="button-cart" class="button" />

在 JavaScript 中

<script type="text/javascript"><!--
$('#button-cart').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>');
                    }
                }
            } 
            alert(json['success']);
            if (json['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/palioxis/image/close.png" alt="" class="close" /></div>');

                $('.success').fadeIn('slow');

                $('#cart-total').html(json['total']);

                $('html, body').animate({ scrollTop: 0 }, 'slow'); 
            }   
        }
    });
});
//--></script>

有人可以简要介绍一下这里发生了什么吗?

【问题讨论】:

    标签: opencart


    【解决方案1】:

    jQuery 代码查找 ID 为 'button-cart' 的按钮。

    一旦点击它,它就会运行下面的 Ajax 代码。数据将传输到route=checkout/cart/add url。

    该文件调用您的/controller/checkout/cart.php 文件并查找add() 函数。传递给add() 函数的数据将来自“数据”。

    如果数据正确传递,jQuery 代码会提示来自json['success'] 的警报消息并加载一个DIV 层。

    否则,如果数据没有正确传递,就会在页面上报错。

    如果这有助于您理解它,请告诉我。我使用 Opencart。

    【讨论】:

    • jsmoove88。万分感谢。如果我对产品有强制性选项(单选按钮/复选框),则对于计算,它只需要那些被选中的字段的数据。我没有得到这个计算发生在哪里?我的意思是它如何知道这些字段被选中/选中?
    • 此计算发生在 /catalog/controller/checkout/cart 中的 Add() 函数中。当访问者单击添加按钮时,脚本会查看该用户的 JSON 中是否存在现有数据,如果存在,它将获取产品信息,然后将其添加到一起。添加后,它将写回 JSON 数据。希望这会有所帮助。
    • 请注意,购物车的实际保存($this-&gt;cart-&gt;add($this-&gt;request-&gt;post['product_id'], $quantity, $option, $recurring_id); - 我正在寻找的那个)发生在库library/cart/cart.php 的购物车中,再次add 函数带有一堆参数。
    猜你喜欢
    • 1970-01-01
    • 2017-08-08
    • 2021-12-31
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多