1.0.1 • Published 3 years ago

@nichoth/shopping-cart v1.0.1

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

shopping cart

A shopping cart UI widget

Uses preact

install

npm i -S @nichoth/shopping-cart

A browserified bundle is in /dist. Or you would import src/cart if you are bundling these prior to use.

example

With import

import Cart from '@nichoth/shopping-cart'
import EVENTS from '@nichoth/shopping-cart/src/EVENTS'

// args are optional
var cart = new Cart({
    storage: true // store the state in localStorage?
    key: 'cart'  // default is 'cart'
})

// `cart` is an event emitter
cart.on(EVENTS.quantity.change, ev => {
    console.log('quantity', ev.quantity)
    console.log('index', ev.index)
})

cart.on(EVENTS.cart.remove, index => {
    console.log('index of the removed item', index)
})

cart.on(EVENTS.product.change, (index, updatedProduct) => {
    console.log('product change', updatedProduct)
})

// pass in an element to mount it at
// or it will automatically mount to the given ID
// for any product, if the `quantity` is greater than the
// `quantityAvaileble`, the icon will render as an exclamation point
cart.createIcon(document.getElementById('shopping-cart-icon'), {
    link: '/my-cart'
})
cart.createPage(document.getElementById('shopping-cart-page'), mapper)

function mapper (html, product) {  // `html` here is from 'htm' on npm
    console.log('in map', product)
    return html`
        <span>name: ${product.name || 'none'}, </span>
        <span>price: ${product.price || 'none'}<//>
    `
}

var products = cart.products()

cart.remove(1) // remove the product at index 1
cart.empty()  // remove everything in the cart

// add something
// icon and page in the UI will auto update
// index is for the item that was added
// default quantity of 1 will be added if no quantity key is present
var index = cart.add({ name: 'my product', quantity: 1, price: 10 }) {

// index of item & desired quantity
// returns itself for chaining
// will emit EvENTS.quantity.change
cart = cart.changeQuantity(0, 2) {

// will emit EVENTS.product.change (index, updatedProduct)
cart = cart.update(0, { quantityAvailable: 4 })

// -----------------------------------------------------------


// get the cart state
var state = cart.state()
var { products } = state

Without importing

<script src="/path/to/cart/dist/bundle.js"></script>
<script>
    var cart = new window.Cart({})
    cart.createIcon(document.getElementById('shopping-cart-icon'), {
        link: '/my-shopping-cart'
    })
    cart.createPage(document.getElementById('shopping-cart-page'))
</script>

develop

Start a local dev server

npm start

Creating A Shopping Cart With HTML5 Web Storage

Ecommerce cart design best practices

1.0.1

3 years ago

1.0.0

3 years ago

0.0.37

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.26

3 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago