-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopify-cart-builder.liquid
137 lines (117 loc) · 4.46 KB
/
shopify-cart-builder.liquid
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
{% layout none %}
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<form id="cartForm">
<table>
<thead>
<tr>
<th>Select</th>
<th>Image</th> <!-- Image Column -->
<th>Name</th>
<th>Price</th> <!-- Price Column -->
<th>Variant ID</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
{% for product in collections.all.products %}
{% if product.variants.size > 0 %}
{% for variant in product.variants %}
<tr>
<td><input type="checkbox" name="variant" value="{{ variant.id }}"></td>
<td><img src="{{ variant.image | default: product.featured_image | img_url: 'medium' }}" loading="lazy" width="100" height="100"></td>
<td>{{ product.title }} - {{ variant.title }}</td>
<td>{{ variant.price | money_without_trailing_zeros }}</td>
<td>{{ variant.id }}</td>
<td><input type="number" name="quantity_{{ variant.id }}" value="1"></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td><input type="checkbox" name="variant" value="{{ product.id }}"></td>
<td><img src="{{ product.featured_image | img_url: 'medium' }}" loading="lazy" width="100" height="100"></td>
<td>{{ product.title }}</td>
<td>{{ product.price | money_without_trailing_zeros }}</td>
<td>N/A</td>
<td><input type="number" name="quantity_{{ product.id }}" value="1"></td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
<label for="sendVisitorTo">Send visitor to:</label>
<select id="sendVisitorTo" name="sendVisitorTo">
<option value="cart">Cart</option>
<option value="checkout">Checkout</option>
</select><br>
<label for="discountCode">Discount Code:</label>
<input type="text" id="discountCode" name="discountCode"><br>
<input type="button" value="Build URL" onclick="buildUrl()">
</form>
<p>Generated URL:</p>
<textarea id="generatedUrl" rows="4" cols="50" readonly></textarea>
<p>QR Code:</p>
<img id="qrCodeImage" src="" alt="QR Code" style="display:none;">
<script>
function buildUrl() {
console.log('Checking input elements...');
var variants = document.querySelectorAll('input[name="variant"]:checked');
console.log('Selected Variants:', variants);
var baseUrl = "https://" + "{{ shop.permanent_domain }}/cart";
var cartItems = [];
variants.forEach(function(variant) {
console.log('Variant ID:', variant.value);
var quantityInput = document.querySelector('input[name="quantity_' + variant.value + '"]');
console.log('Quantity Input for Variant ' + variant.value + ':', quantityInput);
var quantity = quantityInput.value;
cartItems.push(variant.value + ':' + quantity);
});
console.log('Cart Items:', cartItems);
var url = baseUrl + (cartItems.length > 0 ? '/' + cartItems.join(',') : '');
var queryParams = [];
var discountCodeElement = document.getElementById('discountCode');
console.log('Discount Code Element:', discountCodeElement);
var discountCode = discountCodeElement ? discountCodeElement.value.trim() : '';
if (discountCode) {
queryParams.push('discount=' + encodeURIComponent(discountCode));
}
var sendVisitorToElement = document.getElementById('sendVisitorTo');
console.log('Send Visitor To Element:', sendVisitorToElement);
var sendVisitorTo = sendVisitorToElement ? sendVisitorToElement.value : "cart";
if (sendVisitorTo === "cart") {
queryParams.push('storefront=true');
} else if (sendVisitorTo === "checkout") {
queryParams.push('storefront=false');
}
console.log('Query Params:', queryParams);
if (queryParams.length > 0) {
url += '?' + queryParams.join('&');
}
document.getElementById('generatedUrl').value = url;
console.log('Generated URL:', url);
var qrImageUrl = 'https://chart.googleapis.com/chart?cht=qr&chl=' + encodeURIComponent(url) + '&chs=180x180&choe=UTF-8&chld=L|2';
var qrCodeImageElement = document.getElementById('qrCodeImage');
qrCodeImageElement.src = qrImageUrl;
qrCodeImageElement.style.display = 'block';
console.log('QR Code Image Element:', qrCodeImageElement);
}
</script>
</body>
</html>