Martiniboy Martiniboy - 4 months ago 8
Ajax Question

Jquery loop within a var

I am hoping that somebody can help me I have a jquery function which is getting values from a php form and I need to create variable for an ajax call. Is it possible to add a loop within a var

Here is my code so hopefully will explain better what I am trying to do

...
var teacher_ids = $( '#teacher_ids' ).val();
var status = 'pending';
var array = teacher_ids.split(',');

var data = {
name: title,
short_description: excerpt,
description: content,
status: status,
type: 'variable',
variations : [


This is where I am having the issue, I have multiple values which I want to be able to loop though

$.each(array,function (i, item) {
variation_type = item.split('_');
{
regular_price: half_hour,
attributes: [{
id:3,
slug: 'pa_lessonduration',
//name: 'lessonduration',
option: '1-hour'
},{
id: 1,
slug: 'pa_weekday',
// name: 'weekday',
option: variation_type[0]
},{
id: 2,
slug: 'pa_daytime',
//name: 'daytime',
option: variation_type[1],
}]
//"expected an assignment or function call and instead saw an expression?"
}
//"expected ( saw { "
})

$.ajax({
method: "PUT",
url: POST_SUBMITTER.root + 'wc/v1/products/'+ product_id +'',
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
},
success : function( response ) {
console.log( response );
alert( POST_SUBMITTER.success );
},
fail : function( response ) {
console.log( response );
alert( POST_SUBMITTER.failure );
}


});


Any suggestion on how to get this to work please. This should print the following but I get errors

var data = {
name: title,
short_description: excerpt,
description: content,
status: status,
type: 'variable',
variations: [{
regular_price: '19.99',
attributes: [{
id: 3,
name: 'pa_lessonduration',
option: '1-hour'
}, {
name: 'pa_daytime',
option: '0900'
}, {
name: 'weekday',
option: 'monday'
}]
},
{
regular_price: '19.99',
attributes: [{
id: 3,
name: 'pa_lessonduration',
option: '1-hour'
}, {
name: 'pa_daytime',
option: '1100'
}, {
name: 'weekday',
option: 'wednesday'
}]
}]
}


etc etc...

I hope that makes sense, if not ask and I will try to make it more clear

Answer

The code below defines the array variation to be later used in data.

var teacher_ids = $( '#teacher_ids' ).val();
var status = 'pending';
var array = teacher_ids.split(','); 

variation=[];                   // Declare an empty array
                                // Then define it
for(i=0;i<array.length;i++){
    variation_type = array[i].split('_');

    variation[i] = {
      regular_price: half_hour,
        attributes: [{
          id:3,
          slug: 'pa_lessonduration',
          //name: 'lessonduration',
          option: '1-hour'
        },{
          id: 1,
          slug: 'pa_weekday',
          //  name: 'weekday',
          option: variation_type[0]
        },{
          id: 2,
          slug: 'pa_daytime',
          //name: 'daytime',
          option: variation_type[1]
        }]
    }
}


var data = {                    // Define data
    name: title,
    short_description: excerpt,
    description: content,
    status: status,
    type: 'variable',
    variations : variation      // This is the array defined in the loop above
}

$.ajax({
    method: "PUT",
    url: POST_SUBMITTER.root + 'wc/v1/products/'+ product_id +'',
    data: data,
    beforeSend: function ( xhr ) {
        xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
    },
    success : function( response ) {
        console.log( response );
        alert( POST_SUBMITTER.success );
    },
    fail : function( response ) {
        console.log( response );
        alert( POST_SUBMITTER.failure );
    }
});
Comments