1

I need to post data to a URL like this:

http://www.your_domain.com/checkout/cart/add?product=68&qty=1&super_attribute[528]=55&super_attribute[525]=56

See here for Magento documentation on query string

My current code looks like this, but it is not working.

var myObject = {
    super_attribute: {configOptionSuperAttribute: configOption}
};

        jQuery.post(productAddToCartURL, { product: productId, qty: qty, super_attribute: jQuery.param(myObject)  })
        .done(function(data) {
          alert("Data Loaded: " + data);
        });

The issue is with the super_atribute[]. How do I make myObject compatible?

Thanks for the help!

3
  • What's the value of configOption? Is it an array or object? How does it look like? The data is serialized using jQuery.param (api.jquery.com/jQuery.param). Have a look at the documentation to find out how to structure your object. Commented Jun 20, 2013 at 21:57
  • When you say "it is not working," what do you mean? Are you getting a JavaScript error? Is the call reaching the server? Is the done callback not firing? Commented Jun 20, 2013 at 22:00
  • Did you really use jQuery.param in your code or just now because I mentioned it? You should not call it explicitly, it's called internally by jQuery to serialize the whole data object. myObject in your update has to look like configOption in my answer. Don't make it more complicated than it is ;) Commented Jun 20, 2013 at 22:25

2 Answers 2

3

According to jQuery.param (which is used internally to serialize the data), your data should look like:

jQuery.post(
    productAddToCartURL, 
    {product: productId, qty: qty, super_attribute: configOption},
    function() { ... }
);

where configOption is an object of the form

var configOption = {
    528: 55,
    525: 56
};
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know if you pasted your code incorrectly, but your JavaScript has syntax errors. This is invalid because you have an unclosed quote. Perhaps you meant this:

jQuery
    .post(
        productAddToCartURL, 
        { product: productId, qty: qty, 'super_attribute[]': configOption })
    .done(function(data) { alert("Data Loaded: " + data); });

Or maybe this? (your super_attribute[] property name is weird):

jQuery
    .post(
        productAddToCartURL, 
        { product: productId, qty: qty, super_attribute: configOption })
    .done(function(data) { alert("Data Loaded: " + data); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.