-2

I am building a shopping website for my stationery shop.
I have items like pen, pencil, book, copy, scale, bat, ball, etc.

I want to categorize products into price ranges:

  • Budget (₹10–₹100)

  • Mid-range (₹100–₹500)

  • Premium (₹500+)

I also want each category to open on a separate page like:

  • budget.html

  • midrange.html

  • premium.html

My question:
How can I create separate pages and filter products by price in JavaScript?
Do I need to store the product data in an array or JSON file?
What is the best way to load only the products that belong to that category?

New contributor
Nikhil kumar jaiswal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    You need server side coding and a database to do this properly. But...just don't. Fine for a hobby or improving your skills, but not for a serious business. Why re-invent the wheel? There are dozens of online shopping platforms you can make use of very easily, without the headache of building and maintaining your own. Even if you build what you're currently describing successfully, when you get to the point of trying to integrate it with a payment platform you will thank me Commented 10 hours ago

1 Answer 1

-1

You can check the URL Query string for the category, and display products based of that category and their price. Alternatively, you could put the category in the product itself. Here is some code that will return products based off category in the URL:

// Define the products
const products = [
    {
        name: "Product A",
        price: 100
    },
    {
        name: "Product B",
        price: 200
    },
    {
        name: "Product C",
        price: 300
    },
    {
        name: "Product D",
        price: 400
    },
    {
        name: "Product E",
        price: 500
    }
]


// Define the categories
const categories = {
    "budget": [0, 100],
    "midrange": [100, 500],
    "premium": [500, Infinity]
}

function displayProducts() {
    const searchString = location.search;
    // Check if the search string is empty
    if (searchString.length == 0) {
        console.warn("The search string is empty");
        return [];
    }

    const urlParams = new URLSearchParams(window.location.search);
    const targetCategory = urlParams.get("category");
    const priceRange = categories[targetCategory]; // Get the prices range from targetRange string

    if (priceRange == undefined) {
        console.warn("Unknown category");
        return [];
    }

    return products.filter(product => 
        product.price > priceRange[0] && product.price <= priceRange[1]
    );
}

The code searches for category=<category> in the URL Query, gets the prices range and returns products, that are in the price range.

Hope this helps!

New contributor
oulol is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.