npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-cart-middleware

v1.0.0

Published

Node.js(Express) shopping cart middleware

Downloads

2

Readme

Express Shopping Cart Middleware

License

A simple shoppingcart middleware for Express. inspired by LaravelShoppingcart

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install express-cart-middleware

API

const expressCart = require('express-cart-middleware')

Overview

Look at one of the following topics to learn more about Express Shopping Cart Middleware

Usage

The shoppingcart gives you the following methods to use:

cart.add( [ options ] )

Adding an item to the cart is really simple, you just use the add() method, which accepts an Options Object.

  • Options < Object >

this Options object contains the following keys

  • id: product id (required and unique),

  • name: product name (required),

  • price: cost of product (required),

  • quantity: quantity of product (required,

  • preview: url to any resource (optional),

  • attributes : extra details you want to add (optional)

 const options = {} // keys
 
 req.cart.add(options);
 

cart.update( [productId, [ data]] )

  • productId < Integer >
  • data < Object >

To update an item in the cart, you'll first need the product Id of the item. Next you can use the update() method to update it.

If you simply want to update the quantity, you'll pass to the update method the product id and the with an object quantity:

 const productId = 233;

 req.cart.update(productId, { quantity: 19 }); // Will update the quantity

cart.remove( [ productId ])

  • productId < Integer >

To remove an item for the cart, you'll again need the product Id.

 const productId = 233;

 req.cart.remove(productId);

cart.get( [ productId ])

  • productId < Integer >

If you want to get an item from the cart using its product id, you can simply call the get() method on the cart and pass it the product id.

 const productId = 233;

 req.cart.get(productId);

cart.content([ instance ])

  • instance: < string > optional
  • Returns: < Array >

Of course you also want to get the carts content. This is where you'll use the content method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.

 req.cart.content();

cart.checkout([ instance ])

  • instance: < string > optional
  • Returns: < >

If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove all CartItems from the cart for the current cart instance.

 req.cart.checkout();

total([ instance ])

  • instance: < string > optional
  • Returns: < Integer >

The total() method can be used to get the calculated total of all items in the cart, given there price and quantity.

 req.cart.total();

cart.tax([ instance ])

  • instance: < string > optional
  • Returns: < Integer >

The tax() method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.

 req.cart.tax();

cart.subTotal([ instance ])

  • instance: < string > optional
  • Returns: < Integer >

The subTotal() method can be used to get the total of all items in the cart, minus the total amount of tax.

 req.cart.subTotal();

cart.count([ instance ])

  • instance: < string > optional
  • Returns: < Integer >

If you want to know how many items there are in your cart, you can use the count() method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items.

  req.cart.Count();

cart.totalQuantity([ instance ])

  • instance: < string > optional
  • Returns: < Integer >
  req.cart.totalQuantity();

Instances

The packages supports multiple instances of the cart. The way this works is like this:

You can set the current instance of the cart by calling req.cart.add( Options, 'newInstance'). From this moment, the active instance of the cart will be newInstance, so when you add, remove or get the content of the cart, you're work with the newInstance instance of the cart. If you want to switch instances, you just call req.cart.add( Options, 'otherInstance') again, and you're working with the otherInstance again.

So a little example:

    const options = { 
               id: 223, 
               name: 'mouse',
               price: 8900, 
               quantity: 1, 
               preview: 'http://myimages/src/google.png', 
               attributes: [] 
               };
    // add product to the 'music' cart
    req.cart.add(options, 'music')

    // Get the content of the 'music' cart
    req.cart.content('music')

    // update the content of the 'music' cart
    req.cart.update(233, { name: 'new product name'}, 'music')

    // destroy all the content of the 'music' cart
    req.cart.checkout('music')

N.B. all methods provided by the req.cart takes in instance of a cart as it last Optional Argument .

N.B.2 Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.

N.B.2 The default cart instance is called default, so when you're not using instances,req.cart.content() is the same as req.cart.content('default').

Example

N.B this package requires express-session

    const express = require("express")
    const session = require('express-session')
    const expressCart = require('express-cart-middleware')

    const app = express();

    app.use(session({
        secret: 'keyboard cat',
        resave: false,
        saveUninitialized: true,
        cookie: { secure: true }
    }))


    app.use(expressCart({tax: '12'})) // tax in %

    Or

    app.use(expressCart()) // without tax 


    app.get(/add_to_cart, (req, res)=>{

        req.cart.add({ id: 3, name: 'mouse', price: 8900, quantity: 1, preview: 'http://myimages/src/google.png', attributes: [] })
    })

More Examples