-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupsell-quantity-selector-scripts.html
107 lines (85 loc) · 2.59 KB
/
upsell-quantity-selector-scripts.html
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
<script>
/**
* Custom Script to get a list of products with quantity options in the Upsell
* By Jean Manzo https://jdevm.com
*/
$(function(){
var start = 0;
var stop = 10; // Set a limit for the product's quantity
// How many products you have, separated each one by comma (,)
const products = {
'PRODUCT_ID': 'PRODUCT_NAME'
};
// Set PRODUCT_ID to the product number ID. Ex 1234567
// You can set each one separated by comma (,)
var shippingHandle = {
'PRODUCT_ID_qty': 0
};
// You can set a shipping cost as a product in your funnel. Set the PRODUCT_ID for that product.
const shippingCostProduct = 'PRODUCT_SHIPPING_ID';
$('[name="purchase[product_id]"]').remove();
$.each(products, function(index, product_name) {
var id = `pid-${index}`;
var val = index;
var $productRows = (`
<div class="product_rows">
<select
id="${val}_qty"
name="${val}_qty"
class="qty_select"
data-product-id="${val}"
></select>
<p>${product_name}</p>
</div>
`);
$('#qty_container').append( $productRows );
for (i = start; i <= stop; i++) {
$(`#${val}_qty`).append( $('<option/>', { value: i, text: i }) );
}
});
// Add the Shipping & Handling option
if ( shippingCostProduct !== '' ) {
$('#qty_container').append($('<input/>', {
id: shippingCostProduct + '_qty',
type: 'hidden',
name: shippingCostProduct + '_qty',
class: 'qty_select',
"data-product-id": shippingCostProduct
}));
}
// Assign a total value when the value is changed
$('.qty_select').change(function() {
var total_shipping_qty;
shippingHandle[this.id] = parseFloat($(this).val());
total_shipping_qty = shippingAndHandlingTotal(shippingHandle);
$('#' + shippingCostProduct + '_qty').val(total_shipping_qty);
});
function shippingAndHandlingTotal(obj) {
var total_shipping = 0;
$.each(obj, function(index, value){
if (value > 0) {
total_shipping += value;
}
});
return total_shipping;
}
$('#BTN_SELECTOR').click(function(){
$('#cfAR [name="purchase[product_ids][]"]').remove();
$('.qty_select').each(function(){
var qty = $(this).val();
var cartProd = $('<input/>', {
type: "checkbox",
name: 'purchase[product_ids][]',
"data-storage": false,
checked: true
});
var prodId = this.id.split('_')[0];
$('[name="purchase[product_ids][]"] [value="'+prodId+'"]').remove();
cartProd = $(cartProd).val(prodId).attr('checked', true);
for (var j = 0; j < qty; j++) {
$('#cfAR').append($(cartProd).clone());
}
});
});
})
</script>