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

adonis-shopping-cart

v1.0.5

Published

Adonis Shopping Cart

Downloads

10

Readme

Adonis Shopping Cart

Basic Shopping Cart functionality for AdonisJS v5

Requirements

The Cart is using @adonisjs/session to store the data, please ensure you have this package installed and configured.

Installation

# npm
npm i adonis-shopping-cart

Register and configure package with AdonisJS

# npm
node ace configure adonis-shopping-cart

Adinis Shopping Cart

Update variable value in .env file of your project, the default CART_VAT is 20%. Assume that the VAT is already included in the item price. Example: Item price: £100.00 Sub total: £80.00 Vat: £20.00 (based on 20% VAT rate) Total: £100.00


CART_VAT=20

Number format Locale and Options can be specified in config/cart.ts

// default format values
  format:{
    locale: 'en-GB',
    options: { style: 'currency', currency: 'GBP' },
  },

How to use

Cart accepts items of CartItem Type

{
    id: number;
    name: string;
    price: number;
    priceFormat: string;
    quantity: number;
    attributes: [key: string];
}

{
    id: 1234,
    name: 'Classic T-Shirt',
    price: 21.99,
    priceFormat: '£21.99',
    quantity: 1,
    attributes: {
        image: 'https://image.url',
        color: 'white',
        size: 'M',
        // more attributes
      }
}

Adding an item to the Cart

Cart creates unique rowId for each item by hashing item.attributes object allowing to distinguish items with the different atributes, for eaxmple:

  • Classic T-Shirt color: White and size:M is a different item to
  • Classic T-Shirt color: White and size:M

To update an item in the Cart use add method, it will find the item in the Cart and update it or create a new item


Cart.add(item);

Upading item


  let rowId = "lcoLo4tYTGnCn9pqnSgRLqeYV/KAbgDtfXeRUwRL24k=";
  let item ={
    "id": 1,
    "price": 22.5,
    "priceFormat": "£22.50",
    "name": "A green door",
    "quantity": 4,
    "attributes": {
      "image": "https://tailwindui.com/img/ecommerce-images/shopping-cart-page-01-product-01.jpg",
      "color": "sky",
      "size": "s"
    }

Cart.update(rowId:string, item:CartItem);

Removing an item


Cart.remove(item);

//or

Cart.removeByRowId(rowId);

Getting Cart content


Cart.getContent()


{
  "lcoLo4tYTGnCn9pqnSgRLqeYV/KAbgDtfXeRUwRL24k=": { // rowId
    "id": 1,
    "price": 22.5,
    "priceFormat": "£22.50",
    "name": "A green door",
    "quantity": 4,
    "attributes": {
      "image": "https://tailwindui.com/img/ecommerce-images/shopping-cart-page-01-product-01.jpg",
      "color": "sky",
      "size": "s"
    }
  },
  "fLBUy2jj8rI1KcBvI0G2MV1nPD2pkoySPUeBjIp6U30=": { // rowId
    "id": 2,
    "price": 24.5,
    "priceFormat": "£24.50",
    "name": "Basic Tee",
    "quantity": 1,
    "attributes": {
      "image": "https://tailwindui.com/img/ecommerce-images/shopping-cart-page-01-product-02.jpg",
      "color": "pink",
      "size": "s"
    }
  }
}

Other Methods


// has Cart given item
Cart.has(rowId:string):boolean

// Shipping Amount
Cart.setShippingAmmount(10): void   
Cart.getShiping():string            // "£10.00"
Cart.getShipingNumber():number      // 10

// subTotal - (subTotal * VAT)
Cart.getSubtotal(): string          // "£22.50"

//subTotal * VAT
Cart.getVat():string                // "£4.50"
Cart.getVatNumber():number          // 4.50 

// subTotal + VAT + Shipping
Cart.getTotal():string              // "£27.00"
Cart.getTotalNumber():number        // 27 

// calculate quantity of all items in Cart
Cart.getTotalQuantity():number      // 1

Including Cart in your Controller or Service

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Cart from '@ioc:Adonis/Addons/Cart'

export default class CartController {

  public async add({ request }: HttpContextContract) {
    let data = request.body()
    Cart.add(data)
    ...
}

Test

npm run clean
npm run test