simpleCart(js)

items
Items:
Total:

'beforeAdd' Event

The ‘beforeAdd’ event is triggered when an item is about to be added to the cart.  It passes a single parameter of the Item about to be added, which can be modified.

If a callback for the event returns false, the item will not be added.

// simple callback example
simpleCart.bind( 'beforeAdd' , function( item ){
  console.log( item.get('name') ); 
});


// example of modifying the price of the item based on a 'size' attribute
simpleCart.bind( 'beforeAdd' , function( item ){
  if( item.get( 'size' ) == 'Small' ){
    item.price( 10 );
  } else if( item.get( 'size' ) == 'Large' ){
    item.price( 12 );
  } 
});


// example of preventing items from being added
// that have a 'color' attribute set to 'Red'
simpleCart.bind( 'beforeAdd' , function( item ){
  if( item.get( 'color' ) === 'Red'){
    return false; // prevents item from being added to cart
  }
});

 

Comments

  1. There are no comments for this entry yet.
Commenting is not available in this channel entry.