Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit cb0a78e

Browse files
committed
Replace serialized form data with formData obj
This commit updates generate_cart function, replaces serialized data with FormData object. Using FormData fixes all the price related issues between PPEC and Woo Product Add-ons plugin. Tested for simple and variable products with Woo Product add-on plugin.
1 parent 9654117 commit cb0a78e

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

assets/js/wc-gateway-ppec-generate-cart.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,45 @@
8484
} );
8585

8686
var generate_cart = function( callback ) {
87-
var data = {
88-
'nonce': wc_ppec_generate_cart_context.generate_cart_nonce,
89-
'attributes': {},
90-
};
9187

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';
9895
}
9996

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 );
104101
continue;
105102
}
106103

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 );
112105
}
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+
} );
113112

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+
}
116118

117119
$.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',
121126
success: callback,
122127
} );
123128
};

0 commit comments

Comments
 (0)