I am developing right now a shopping cart and store the added items in a db table. on every add I refetch my cart (it automically does with caching). So is this right or should I use async storage ?
ps: if I press the btn AddToCart then I have to wait until my fetch has loaded then the user see "Item successfull added in Cart" .
I can handle it with redux but I have to store it in a DB because if anyone logs in with mobile then Computer so I have to fetch the data in shopping cart. So should I only use it without redux or any state management and do it only with my React-Query or RTK Query ?
Related
I'm trying to figure out how to implement the shopping cart functionality on a page using Vue.js. I get a list of items from an API call and print them on page load. But I'm stuck trying to add items to my cart. The functionality should include the ability to select a quantity of an item and add it to the cart by clicking the "add to cart" button. So it should allow, directly from the cart, to change quantities and remove items from the cart.
For this kind of situation, use a state management pattern like vuex (or pinia), and it will store your data after route change also.
For more clear context, go through this link:
https://vuex.vuejs.org/
How i can add a button on the cart page that will allow the customer to "Empty The Cart". Script will empty contents of the cart.
you could create the button in the template and use a script to call this endpoint: DeleteACart https://developer.bigcommerce.com/api-reference/store-management/carts/cart/deleteacart, to erase an entire cart.
You don't want to use the management api to do this, because that would require authentication. You need to use the storefront cart api I believe.
You would need to call GET /api/storefront/carts and get the id of the cart, and then call DELETE /api/storefront/delete/{id}.
Alternatively you could mimic the native functionality by looping through all the cart items to build a request for POST /remote/v1/cart/update.
The template file for cart in cornerstone is templates/pages/cart.html
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!
So there are 2 ways that a Shopify cart may get updated. Either via submitting form or using Ajax API. Assuming, you have already solved the form submission case by calling your calculate shipping function on page load. For Ajax based cart updates there is no standard event. Some themes may fire an event on cart update, but in case of a generic app, it is not possible to rely on them due to no defined standard behavior.
So what you can do is to listen to AJAX calls and call your function if it matches your conditions. Shopify Docs for Ajax API lists the 4 types of POST request that may update the Cart. So by patching the open function of XMLHttpRequest we add an event listener for load that fires when the request finished successfully. In the event callback, we check if the URL was the one used for updating cart, then call update shipping function.
const open = window.XMLHttpRequest.prototype.open;
function openReplacement() {
this.addEventListener("load", function () {
if (
[
"/cart/add.js",
"/cart/update.js",
"/cart/change.js",
"/cart/clear.js",
].includes(this._url)
) {
calculateShipping(this.response);
}
});
return open.apply(this, arguments);
}
window.XMLHttpRequest.prototype.open = openReplacement;
function calculateShipping(cartJson) {
console.log("calculate new shipping");
console.log(JSON.parse(cartJson));
}
Idea taken by answer from Nicolas
ajaxComplete was not used because it only listens to requests made using jQuery.
Passing response to your Shipping function saves the additional Get Cart Ajax call.
Update
If the above code does not work for you or does not catch all the calls, see my other answer that also listens to all Fetch API calls if that is being used to update cart.
Listen to Cart changes using Fetch API
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..
Just learning and building with VUEJS. I want to build a shoppingcart for our site so user can add multiple items in cart.
How do i store the items in the cart ?
User Add item 1 to cart
User browse site
User Add item 2 to cart
User leaves the site
User come back next day
User see that item 1 and 2 is still in basket
How does this work in VUEJS ? With cookies ?
best,
remco
We can use VUEX for storing data in our vue application, to persist the data we can use vuex-persistedstate plugin. This plugin will use the browser's local storage for the purpose.
I am building a react native apps using crna where there's flat list that showing data from API. It's quite like twitter or facebook's status feeds. And I would like to show some badge like this
When there's a new post and to make user aware of new post so they will refresh it.
Could anyone tell me what should I do to make this happen?
Thank you so much.
Not sure if you're using some kind of state management tool like redux, but it would just be a matter of calling the API every so-often and updating the state from there. I'm going to use redux as an example since it's quite popular
You'd prob want state that has the following info:
currentPosts: [] //array of posts that the user already sees
newPosts: [] // array of new posts user can't see until they hit "new posts" button
With redux, you can have a function that gets called every 5 or 10 seconds that checks the API to show where you get new post data from. If there is a new post, add it to the newPosts array. This would update state, and if your component is connected to state, it would update props.
With this logic, you'd be able to determine whether or not you should show the "new posts" button with a simple boolean. If the array is empty, hide it, if it isn't, show the button.
Once the button is clicked, you could update the state so that the newPosts data goes into the currentPosts array and those items will get rendered from there.
Hope this makes sense! There may be quite a lot I'm missing but that's the idea of how it could work. Lemme know if you have any questions