'Listen to shopify cart update quantity
I am writing a Shopify app and I would want to listen to the shopify cart update event. I know that when user clicks on remove or increase the item quantity. Shopify send a POST request to the backend to update the card. My app calculates the shipping value based on shopify cart item quantity. I add my app script to the shopify via script-tag.
I tried to listen to the quantity input but this event only fires one. I think shopify updates the input DOM after every cart update so it may remove my listener.
$('body').on('change', '.input[name="updates[]"]', function() { console.log('HELLO')});
How can I listen to the cart update event? It seems to be a simple question but I really cannot find any answer regarding to Shopify!
Solution 1:[1]
To update item quantity fields in the cart in sectioned theme
Step 1: You access Shopify admin, click Online Store and go to Themes.
Step 2: Find the themes that you want to edit, then press Actions then Edit Code.
Step 3: Look at Sections, you click on cart-template.liquid. In case that you are not using the theme that does not have the address, you can access Template directory and click cart.liquid.
Step 4: Find the phrase written as update[item.id] in input tag.
Step 5: Change the name into the value of name= “updates[]”.
Step 6: Reiterate these aforementioned steps whenever you see an name value of updates[] in input tag.
Step 7: Click Save and you update item quantity fields successfully..
Solution 2:[2]
Here's a simple way: You could calculate the time complexity by expanding each of the additive terms and observe the pattern:
T(n)
= T(n-1) + T(n-2) + T(n-3) + ... + T(1)
= (T(n-2) + T(n-3) + ... + T(1)) + T(n-2) + T(n-3) + ... + T(1)
= 2T(n-2) + 2T(n-3) + ... + 2T(1)
= 4T(n-3) + ... + 4T(1)
= 2^(k-1) T(n-k) + ... + 2^(k-1) T(1)
= 2^(n-2) T(1)
= 2^(n-2)
Solution 3:[3]
T(n) = (T(1) + T(2) + ... + T(n-2)) + T(n-1)
= T(n-1) + T(n-1)
= 2T(n-1)
Thus T(n) = c * 2^n, where c is T(1)/2.
Solution 4:[4]
You have the following:
T(1) = 1
T(2) = T(2-1) = T(1) = 1
T(3) = T(3-1) + T(3-2) = T(2) + T(1) = 2
T(4) = T(4-1) + T(4-2) + T(4-3) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4
T(5) = 4 + 2 + 1 + 1 = 8
T(6) = 8 + 4 + 2 + 1 + 1 = 16
...
From the above it follows by induction that T(n) = 2*T(n-1) with the base cases T(1) = T(2) = 1
We can further built on this saying that since T(n) = 2*T(n-1), T(n) = 2*2*T(n-2). It follows that T(n) = (2^i)*T(n-i) for i < n-2 and n > 2.
So in terms of time complexity we get O(2^n).
Note: Please correct me if I'm wrong. I'm not the very best with this stuff. My appologies in advance. I'm trying to learn myself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jingjin Wang |
| Solution 3 | Paul Hankin |
| Solution 4 |
