2017-05-03 4 views
1

私はopencartのソリューションを探しています。opencart cart.add()with option

オプション値を持つ製品を追加する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ファイルで可能ペイロード形式を見ることができます。