【问题标题】:Adding two products to cart with one click on the "Add to cart" button. Open Cart 1.5.5.1只需单击“添加到购物车”按钮即可将两种产品添加到购物车。打开购物车 1.5.5.1
【发布时间】:2014-01-17 21:44:26
【问题描述】:

我在 configurator.tpl 文件(product.tpl 的副本 + 一些编辑)中创建了 configurator.tpl 文件strong>catalog/view/theme/theme-name/template/product/configurator.tpl 以便在产品页面上同时拥有 2 个产品。另外,我创建了控制器文件 catalog/controller/product/configurator.phpproduct.php 的副本> + 一些编辑)。一切安好。您可以查看here

现在,我正在尝试将这 2 种产品添加到购物车,单击一次“添加到购物车”按钮。这两个产品总是相同的两个(product_id=50 和 product_id=51)。
我是否必须修改“添加到购物车功能”,或者可能调用该函数两次,每个 product_id 一次?任何方法都可以。

我找到了一些有价值的信息here,但我无法想象解决方案。请帮忙!

打开购物车 1.5.5.1

【问题讨论】:

    标签: add opencart cart


    【解决方案1】:

    替换

      $('#button-cart').bind('click', function() {
    
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: $('.product-info.first input[type=\'text\'], .product-info.first input[type=\'hidden\'], .product-info.first input[type=\'radio\']:checked, .product-info.first input[type=\'checkbox\']:checked, .product-info.first select, .product-info.first textarea'),
    

    addtocart2(pid) {
    
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: $('#product-'+pid+' .product-info.first input[type=\'text\'], #product-'+pid+' .product-info.first input[type=\'hidden\'],#product-'+pid+'  .product-info.first input[type=\'radio\']:checked, #product-'+pid+' .product-info.first input[type=\'checkbox\']:checked, #product-'+pid+' .product-info.first select,#product-'+pid+'  .product-info.first textarea'),
    

    并替换

     <input type="button" value="Adauga in cos" id="button-cart" class="button" />
    

     <input type="button" value="Adauga in cos" onclick="addtocart2(<?=$product_id ?>)" class="button" />
    

    最后将 html id 添加到 priduct-info div

    <div class="product-info first">
    

    替换为

    <div class="product-info first" id="product-<?=$product_id?>">
    

    不知道你是怎么输出的,所以$product_id可以改成$product['product_id']或者$product2_id;

    【讨论】:

    • 我做过类似的事情:1) 将 $('#button-cart').bind('click', function() { 函数体加倍。 2) 替换数据:$('.product-info.second... 3) 将
      更改为
      4) 更改 我将发布我的类似解决方案作为单独的答案
    【解决方案2】:

    所有更改均针对 configurator.tpl
    替换 $('#button-cart').bind('click', function() {

    $('#button-cart').bind('click', function() {
     $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('.product-info.first input[type=\'text\'], .product-info.first input[type=\'hidden\'], .product-info.first input[type=\'radio\']:checked, .product-info.first input[type=\'checkbox\']:checked, .product-info.first select, .product-info.first 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['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                $('.success').fadeIn('slow');
                $('#cart-total').html(json['total']);
                $('html, body').animate({ scrollTop: 0 }, 'slow'); 
            }   
        }
    });
    
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: $('.product-info.second input[type=\'text\'], .product-info.second input[type=\'hidden\'], .product-info.second input[type=\'radio\']:checked, .product-info.second input[type=\'checkbox\']:checked, .product-info.second select, .product-info.second 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['success']) {
                $('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                $('.success').fadeIn('slow');
                $('#cart-total').html(json['total']);
                $('html, body').animate({ scrollTop: 0 }, 'slow');
            }   
        }
    });
    });
    

    &lt;div class="product-info"&gt;替换为
    &lt;div class="product-info first"&gt;(适用于产品1)和
    &lt;div class="product-info second"&gt;(适用于产品2)

    仅替换第二个产品
    &lt;input type="hidden" name="product_id" size="2" value="&lt;?php echo $product; ?&gt;" /&gt;
    &lt;input type="hidden" name="product_id" size="2" value="&lt;?php echo $product_id_2; ?&gt;" /&gt;

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      相关资源
      最近更新 更多