add-to-cart-variation.js
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Wc add to cart version 1.2.8 */
jQuery( function( $ ) {
// wc_add_to_cart_params is required to continue, ensure the object exists
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
// Ajax add to cart
$( document ).on( 'click', '.variations_form .single_add_to_cart_button', function(e) {
e.preventDefault();
$variation_form = $( this ).closest( '.variations_form' );
var var_id = $variation_form.find( 'input[name=variation_id]' ).val();
var product_id = $variation_form.find( 'input[name=product_id]' ).val();
var quantity = $variation_form.find( 'input[name=quantity]' ).val();
//attributes = [];
$( '.ajaxerrors' ).remove();
var item = {},
check = true;
variations = $variation_form.find( 'select[name^=attribute]' );
/* Updated code to work with radio button - mantish - WC Variations Radio Buttons - 8manos */
if ( !variations.length) {
variations = $variation_form.find( '[name^=attribute]:checked' );
}
/* Backup Code for getting input variable */
if ( !variations.length) {
variations = $variation_form.find( 'input[name^=attribute]' );
}
variations.each( function() {
var $this = $( this ),
attributeName = $this.attr( 'name' ),
attributevalue = $this.val(),
index,
attributeTaxName;
$this.removeClass( 'error' );
if ( attributevalue.length === 0 ) {
index = attributeName.lastIndexOf( '_' );
attributeTaxName = attributeName.substring( index + 1 );
$this
//.css( 'border', '1px solid red' )
.addClass( 'required error' )
//.addClass( 'barizi-class' )
.before( '<div class="ajaxerrors"><p>Please select ' + attributeTaxName + '</p></div>' )
check = false;
} else {
item[attributeName] = attributevalue;
}
// Easy to add some specific code for select but doesn't seem to be needed
// if ( $this.is( 'select' ) ) {
// } else {
// }
} );
if ( !check ) {
return false;
}
//item = JSON.stringify(item);
//alert(item);
//return false;
// AJAX add to cart request
var $thisbutton = $( this );
if ( $thisbutton.is( '.variations_form .single_add_to_cart_button' ) ) {
$thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' );
var data = {
action: 'woocommerce_add_to_cart_variable_rc',
product_id: product_id,
quantity: quantity,
variation_id: var_id,
variation: item
};
// Trigger event
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
// Ajax action
$.post( wc_add_to_cart_params.ajax_url, data, function( response ) {
if ( ! response )
return;
var this_page = window.location.toString();
this_page = this_page.replace( 'add-to-cart', 'added-to-cart' );
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
} else {
$thisbutton.removeClass( 'loading' );
var fragments = response.fragments;
var cart_hash = response.cart_hash;
// Block fragments class
if ( fragments ) {
$.each( fragments, function( key ) {
$( key ).addClass( 'updating' );
});
}
// Block widgets and fragments
$( '.shop_table.cart, .updating, .cart_totals' ).fadeTo( '400', '0.6' ).block({
message: null,
overlayCSS: {
opacity: 0.6
}
});
// Changes button classes
$thisbutton.addClass( 'added' );
// View cart text
if ( ! wc_add_to_cart_params.is_cart && $thisbutton.parent().find( '.added_to_cart' ).size() === 0 ) {
$thisbutton.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
}
// Replace fragments
if ( fragments ) {
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
});
}
// Unblock
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
// Cart page elements
$( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$( document.body ).trigger( 'cart_page_refreshed' );
});
$( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
});
// Trigger event so themes can refresh other areas
$( document.body ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
}
});
return false;
} else {
return true;
}
});
});