ember-cart is built and maintained by DockYard, contact us for expert Ember.js consulting.
Shopping cart primitives for Ember applications
ember install ember-cart
If it is a bug please open an issue on GitHub.
ember-cart manages the adding, quantity editing, removal, and eventual payment processing of shopping cart items.
The primitives provided are intended to give a boilerplate base to work from. Customizing the templates is always a good place to start.
{{cart-items}}
Provides a list of the shopping cart. Each line item is a {{cart-item}}. Shows the total cost of all the items in the cart.
{{cart-item}}
The line-item for each item type in the cart. Adding more than one of the same type will increase the quantity. Also handles removal of the item from the cart.
{{cart-counter}}
A counter that displays the current number of unique items in the cart.
this.cart
is injected into Controllers and Components. If you want to
add an item to the cart you can create an action that does
this.cart.pushItem
:
actions: {
pushItem(item) {
this.cart.pushItem(item);
}
}
You can push POJOs or Ember models into the cart. The basic information
that an item
requires is name and cost. ember-cart will handle
checking to see if there is already an existing similar item in the
cart. If there is, the quantity for that item is incremented. If not the
item is added to the cart.
POJOs pushed into the cart:
this.cart.pushItem({
name: 'Doggie',
cost: 400
});
You can push another model into the cart. However you should provide a
toCartItem
handler on that model to typecast that model to a CartItem.
// app/models/dog.js
import Ember from 'ember';
const {
get,
Object: EmberObject,
getOwner
} = Ember;
export default EmberObject.extend({
toCartItem() {
let CartItem = getOwner(this)._lookupFactory('model:cart-item');
return CartItem.create({
name: get(this, 'name'),
price: get(this, 'cost')
});
}
});
If you want to persist the cart to localStorage
so the items are
available if the user comes back later you will need to set the
localStorage
flag in the Cart service to true
:
// app/services/cart.js
import CartService from 'ember-cart/services/cart';
export default CartService.extend({
localStorage: true
});
Please note: if you persist to localStorage
you will have to handle
the security around this. For example, if a user signs out of their
account you will probably want to clear their cart:
actions: {
signOut() {
// signout handler
this.cart.clearItems();
}
}
Make sure to use clearItems()
and not clear()
as the former will
ensure that localStorage
is properly cleared out.
You may only allow one of a specific type. To enforce the quantity
doesn't increment you should set increment: false
for the Cart Item:
this.cart.pushItem({
name: 'Foo',
price: 100,
increment: false
});
We are very thankful for the many contributors
This library follows Semantic Versioning
Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.
DockYard, Inc © 2015