Creating customizable shopping cart items is easier than you may think

As I construct a shopping cart, the display object provides the available data to the shopper. On the other hand, the cart object contains the representation of the items selected by the shopper. The main issue arises when someone wants to modify an item in the cart and adjust its size. Unfortunately, the sizes are no longer accessible in the cart item because only one size has been chosen, eliminating the others.

// display object
{
    "sizes": [
        {
            "price": 4.99,
            "title": "s"
        },
        {
            "price": 5.99,
            "title": "m"
        },
        {
            "price": 6.99,
            "title": "l"
        }
    ],
    "desc": "here's our green hat",
    "title": "green hat"
}

// cart object
{
    "size": 
    {
        "price": 4.99,
        "title": "s"
    },
    "desc": "here's our green hat",
    "title": "green hat"
}

One possible solution could be passing the entire display object along with marking (using some method) which option has been selected. Alternatively, there might be another solution that I am overlooking. Any suggestions?

Answer №1

If the items in your shopping inventory are subject to frequent changes, it is important to retrieve updated information from your backend system to ensure data consistency. In such cases, it is advisable to query the database and fetch the complete object including all sizes. This approach is also beneficial for keeping track of the current status of your items, as availability may change based on factors like high purchase traffic.

Tip: Consider enabling caching in your backend system for frequently accessed items with their respective sizes to enhance performance outcomes.

However, if your shopping items remain relatively stable and do not require frequent updates, it may be more efficient to transmit the entire object along with all available sizes, while adding a new property to indicate the selected one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Securely Passing API Tokens in JavaScript: Best Practices

This script fetches data from an API and displays it on the HTML page using AJAX and Javascript. The TOKEN used for the API is visible in the Javascript code. I believe this approach is not secure as anyone with access to the page can view the token. Are ...

JavaScript is limited to functioning within the content area of WordPress websites

Currently, I am using WordPress and I am looking to have my share links follow the post as a user scrolls down. The issue I'm facing is that the JavaScript code is directly attached to content.php, causing it to load multiple times on pages with multi ...

Tips for controlling design elements in ReactHow to effectively manage styling in

Currently, I am diving into the world of React and have noticed a significant amount of style changes being done within JavaScript. Traditionally, styles are managed through CSS files, but I am uncertain if this same practice applies to React. What is co ...

Using Angular $resources in the query() function often results in the error message "Undefined is not a function

When I try to call the query() function using ngResource, I keep getting an error message saying "Undefined is not a function." [demo.js] var app = angular.module('StarterApp', ['ngResource']); app.factory("resourceFactory", function ...

Updating a function by passing in arguments in JavaScript

I am currently using Winston as the logger for my Node.js application. Within my script, I have a line that looks like this: log.error("my error", {input: [1,2]}); When I check the console, I see the following output: error: my error input=[1, 2] What ...

Tips for simultaneously hovering over SVG and text elementsFeel free to hover over both SVG

After importing an SVG image and creating a box containing both an icon and text, I noticed that when hovering over the box, only the color changes to yellow while the SVG remains unaffected. How can I resolve this issue so that both the text and icon chan ...

Retrieve variables from an external JavaScript file

I need to fetch the currentID variable from renderermain.js and utilize it in renderermem.js. However, I am looking for a way to achieve this without modifying mem.html: <script src="renderermain.js"></script> <script src="renderermem.js"& ...

Unable to save cookies on mobile browsers, but functioning properly on desktop computers

Currently, I am facing an issue with my ExpressJS app hosted on Heroku and a frontend React app hosted on Netlify. The problem arises when a user logs in successfully and is directed to the home page showing personalized content. Upon landing on the home p ...

JQuery UI - Initial Load Error

Here is the script I am working with (paraphrased but in the same order): <script type="text/javascript" src="jquery-1.4.4.min.js"></script> <script type="text/javascript" src="jquery-ui-1.8.9.custom.min.js"></script> <script ty ...

What's the best way to exclude a column header in node.js with _.omit lodash?

const workbook = new Excel.Workbook(); const worksheet = workbook.addWorksheet(RESCOL); const collection = recdb.getCollection(RESCOL); collection.find().forEach(row => { if (RESCOL) { if (!hdrsWritten) { workbook.xlsx.writeFile ...

When an AJAX request is made, it can either return an array or a single object, potentially leading to

My proficiency in Javascript is definitely lacking, and I've only just begun to understand it. I have set up an AJAX call to an API using the GET method. The JSON data returned by the API is pretty standard. If I don't include an ID, I receive ...

Transfer a single property from a particular object in one array to another array of objects based on a condition (JavaScript ES6)

I have 2 sets of data arrays coming from 2 separate API calls const data1 = [ { name: 'John', age: 30, id: 1, }, { name: 'Sarah', age: 28, id: 2, }, ]; const data2 = [ { status: 'active', ...

Having trouble downloading a PDF file on a local server with React and the anchor tag element

Having trouble downloading a pdf file from my react app to my Desktop. I've reached out for help with the details How to download pdf file with React. Received an answer, but still struggling to implement it. If anyone new could provide insight, that ...

display a table using angularjs only if the table contains at least one row

I am currently developing a web application using MVC5 and AngularJS. The main component of my app is a table. <div class="table-responsive scroll" ng-hide="hidetable"> <table id="table" class="table table-bordered table-condensed"> ...

Creating a Yup field that is an object and making it required when another field is set to true in the validation process

Just getting started with Yup validation and I'm facing an issue. I am attempting to make certain fields required based on a condition. Specifically, I want the digital object to be required only if hasDigital is true; otherwise, it should be optional ...

How can you organize and save models in Three.js in the order they are loaded/called?

When loading models, I want to ensure they are placed in the object array in the same order as they are called. However, sometimes they get assigned a different number and end up in a different sequence. Is there a way to guarantee that the models are pla ...

Is there a way in jQuery Validation to apply a rule to the entire form rather than just individual elements within the form?

I am facing an issue with a form where each element has its own custom rules that work perfectly. However, the form cannot be submitted unless values are provided for at least one of its elements. It seems like this is a rule for the entire form rather th ...

Canvas is unable to duplicate image content

I am trying to duplicate a PNG captcha image in order to remove its transparency so that the captcha resolver (ocrad.js) function can work properly. However, I am facing an issue where the duplicated captcha image is being moved and resized. How can I ke ...

What is the best way to trigger a child function from the parent component in React?

In my React application, there are three components: two child components and one parent component. I need to pass data (projectId) from ChildOne to ChildTwo through the Parent component and trigger a function once the data is received. Essentially, I am s ...

Node.js is being utilized to upload data to an FTP server with the help of js

I'm attempting to send a file to an FTP server using Node.js with the help of jsftp module.exports = async function() { const jsftp = require("jsftp"); console.log(__dirname) const ftp = new jsftp({ host: "host.name", ...