P
P
panterr922017-09-27 17:35:34
JSON
panterr92, 2017-09-27 17:35:34

How to pass the value of product options to the cart via AJAX(JSON)?

When transferring goods to the cart from the product card, the following code works

'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');
      },
      complete: function() {
        $('#cart > button').button('reset');
      },
      success: function(json) {
        $('.alert, .text-danger').remove();

        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>');

          // Need to set timeout otherwise it wont update the total
          setTimeout(function () {
            $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
          }, 100);

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

          $('#cart > ul').load('index.php?route=common/cart/info ul li');
        }
      },
      error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  },


If you set the options for a product as mandatory, then adding to the cart is not performed. When outputting a JSON string to the console, an error is written, it is necessary to select a color (this is the option).

I came to the conclusion that it is necessary to complete the line by adding &option

spoiler
data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),


The question is how to correctly transfer variables there, as I understand the option IDs and option ID values ​​in the array.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zoozag, 2017-09-28
@panterr92

The product card has a different add-to-cart code. There in the line you mentioned:

data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[ type=\'checkbox\']:checked, #product select, #product textarea'),

W
web-mechanic, 2017-09-29
@amfetamine

Open source and see

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question