2017-05-03 83 views
1

我正在寻找opencart的解决方案。Opencart cart.add()与选项

需要重新生成cart.add函数以添加带选项值的产品。

默认:

'add': function(product_id, quantity) { 
    $.ajax({ 
     url: 'index.php?route=checkout/cart/add', 
     type: 'post', 
     data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1), 
     dataType: 'json', 
     beforeSend: function() { 
      $('#cart > button').button('loading'); 
     }, 
     success: function(json) { 
      $('.alert, .text-danger').remove(); 

      $('#cart > button').button('reset'); 

      if (json['redirect']) { 
       location = json['redirect']; 
      } 

      if (json['success']) { 
       $('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>'); 

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

       $('html, body').animate({ scrollTop: 0 }, 'slow'); 

       $('#cart > ul').load('index.php?route=common/cart/info ul li'); 
      } 
     } 
    }); 

结帐/车/加PHP端的选项:

 if (isset($this->request->post['option'])) { 
      $option = array_filter($this->request->post['option']); 
     } else { 
      $option = array(); 
     } 

任何解决方案?

+0

我用这里描述的解决方案:https://stackoverflow.com/a/24161192/4288232 – TimSC

回答

2

您可以通过以下方式完成此操作。创建一个简单的Javascript对象。

var productData = {'product_id' : PRODUCT_IDS.CARD, 'quantity': 1,'option':{'229':"Some option value"}}; 

并发送请求如下。使用param函数将对象转换为post params

$.ajax({ 
        url: 'index.php?route=checkout/cart/add', 
        type: 'post', 
        data: $.param(productData), 
        dataType: 'json', 
        beforeSend: function() { 
        }, 
        complete: function() { 
        }, 
        success: function (json) { 

        }, 
        error: function (xhr, ajaxOptions, thrownError) { 

        } 
       }); 

也有一些其他的方法可以做到这一点,你可以看到在/controller/checkout/cart.php文件可能有效载荷格式。