-
Notifications
You must be signed in to change notification settings - Fork 13
/
header cart icon hide when zero.html
78 lines (40 loc) · 1.8 KB
/
header cart icon hide when zero.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
<script>
/*
begin header cart icon hide when zero
License : < https://tinyurl.com/s872fb68 >
Version : 0.1d0
SS Version : 7.1
By : Thomas Creedon < http://www.tomsWeb.consulting/ >
no user serviceable parts below
*/
window.addEventListener ( 'DOMContentLoaded', ( ) => {
// bail if no mutation observer available
if ( ! ( 'MutationObserver' in window ) ) return;
const observer = new MutationObserver ( mutations => {
for ( const mutation of mutations ) {
// bail if no added nodes
if ( ! mutation.addedNodes.length ) return;
const node = mutation.addedNodes [ 0 ];
if ( node.nodeName !== '#text' ) return; // bail if not text node
const elements =
document.querySelectorAll ( '.header-actions-action--cart' );
const quantity = Number ( node.nodeValue );
const showClass = 'twc-show';
for ( const element of elements ) {
if ( quantity ) {
element.classList.add ( showClass );
} else {
element.classList.remove ( showClass );
}
}
}
} );
// start listening for changes in specified element
const element = document.querySelector ( '.sqs-cart-quantity' );
observer.observe ( element, {
characterData : true,
childList : true
} );
} );
/* end header cart icon hide when zero */
</script>