|
84 | 84 | } ); |
85 | 85 |
|
86 | 86 | var generate_cart = function( callback ) { |
87 | | - var data = { |
88 | | - 'nonce': wc_ppec_generate_cart_context.generate_cart_nonce, |
89 | | - 'attributes': {}, |
90 | | - }; |
91 | 87 |
|
92 | | - var field_pairs = form.serializeArray(); |
93 | | - |
94 | | - for ( var i = 0; i < field_pairs.length; i++ ) { |
95 | | - // Prevent the default WooCommerce PHP form handler from recognizing this as an "add to cart" call |
96 | | - if ( 'add-to-cart' === field_pairs[ i ].name ) { |
97 | | - field_pairs[ i ].name = 'ppec-add-to-cart'; |
| 88 | + var formData = new FormData(), |
| 89 | + formParams = form.serializeArray(); |
| 90 | + |
| 91 | + for ( var i = 0; i < formParams.length; i++ ) { |
| 92 | + // Prevent the default WooCommerce PHP form handler from recognizing this as an "add to cart" call. |
| 93 | + if ( 'add-to-cart' === formParams[ i ].name ) { |
| 94 | + formParams[ i ].name = 'ppec-add-to-cart'; |
98 | 95 | } |
99 | 96 |
|
100 | | - // Save attributes as a separate prop in `data` object, |
101 | | - // so that `attributes` can be used later on when adding a variable product to cart |
102 | | - if ( -1 !== field_pairs[ i ].name.indexOf( 'attribute_' ) ) { |
103 | | - data.attributes[ field_pairs[ i ].name ] = field_pairs[ i ].value; |
| 97 | + // Save attributes in a nested array, |
| 98 | + // so that `attributes` can be used later on when adding a variable product to cart. |
| 99 | + if ( -1 !== formParams[ i ].name.indexOf( 'attribute_' ) ) { |
| 100 | + formData.append( "attributes[" + formParams[ i ].name + "]" , formParams[ i ].value ); |
104 | 101 | continue; |
105 | 102 | } |
106 | 103 |
|
107 | | - if ( -1 !== field_pairs[ i ].name.indexOf( 'addon-' ) ) { |
108 | | - field_pairs[ i ].name = field_pairs[ i ].name + field_pairs[ i ].value; |
109 | | - } |
110 | | - |
111 | | - data[ field_pairs[ i ].name ] = field_pairs[ i ].value; |
| 104 | + formData.append( formParams[ i ].name, formParams[ i ].value ); |
112 | 105 | } |
| 106 | + |
| 107 | + $.each( form.find( 'input[ type="file" ]' ), function( i, tag ) { |
| 108 | + $.each( $( tag )[ 0 ].files, function( i, file ) { |
| 109 | + formData.append( tag.name, file ); |
| 110 | + } ); |
| 111 | + } ); |
113 | 112 |
|
114 | | - // If this is a simple product, the "Submit" button has the product ID as "value", we need to include it explicitly |
115 | | - data[ 'ppec-add-to-cart' ] = $( '[name=add-to-cart]' ).val(); |
| 113 | + formData.append( 'nonce', wc_ppec_generate_cart_context.generate_cart_nonce ); |
| 114 | + |
| 115 | + if ( ! formData.has('ppec-add-to-cart') ) { |
| 116 | + formData.append( 'ppec-add-to-cart', $( '[name=add-to-cart]' ).val() ); |
| 117 | + } |
116 | 118 |
|
117 | 119 | $.ajax( { |
118 | | - type: 'POST', |
119 | | - data: data, |
120 | | - url: wc_ppec_generate_cart_context.ajaxurl, |
| 120 | + url: wc_ppec_generate_cart_context.ajaxurl, |
| 121 | + cache: false, |
| 122 | + contentType: false, |
| 123 | + processData: false, |
| 124 | + data: formData, |
| 125 | + type: 'POST', |
121 | 126 | success: callback, |
122 | 127 | } ); |
123 | 128 | }; |
|
0 commit comments