2017-05-03 1 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 파일에 가능한 페이로드 형식을 볼 수 있습니다.