1.0.4 • Published 6 years ago

nodejs-cart-lna v1.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

Express Logo

Simple shopping cart for node.

var Cart = require('nodejs-cart-lna');

var cart = new Cart(req);

cart.add(productObject); // add productObject to session carts

cart.remove(_id) ;// remove productObject 

cart.items ; // propety all item in carts session

cart.update(_id,quantity); // update quantity of item

cart.clear();// clear all items 

cart.total_item(); // total quantity in carts sesion

cart.total_amount(); // total amount in carts session

cart.item_amount(_id) ;// sub amount of item in carts session

cart.promo(percent); // apply promotion percent to cart session

cart.item_promo(id,percent); // apply promotion percent of item in cart session

Installation

$ npm install nodejs-cart-lna

Example

var express = require("express");
var router = express.Router();// route
var Product = require('../models/Product'); // product model
var Cart = require('nodejs-cart-lna');

// route add product to carts ssession
router.get('/add-cart/:id',function(req,res){
  var cart = new Cart(req);
  Product.findOne({_id:req.params.id},function(err,data){
    cart.add(data);
    return res.redirect('/show-cart'); // chuyển hướng về trang bạn muốn
  });

});

// route show product in carts ssession
router.get('/show-cart/',function(req,res){
  var cart = new Cart(req);
  res.render('show-cart',{
    cart:cart
  });

});

// route remove product from carts ssession
router.get('/cart-remove/:id',function(req,res){
  var cart = new Cart(req);
  cart.remove(req.params.id);
  return res.redirect('/show-cart'); // chuyển hướng về trang bạn muốn
});

// route update product quantity in carts ssession
router.get('/cart-update/:id',function(req,res){
  var cart = new Cart(req);
  var quantity = req.query.quantity;
  quantity = (isNaN(quantity) || quantity < 1) ? 1 : quantity;
  cart.update(req.params.id,quantity);
  return res.redirect('/show-cart'); // chuyển hướng về trang bạn muốn
});

module.exports = router;