NAME

Interchange6::Cart - Cart class for Interchange6 Shop Machine

DESCRIPTION

Generic cart class for Interchange6.

SYNOPSIS

my $cart = Interchange6::Cart->new();

$cart->add( sku => 'ABC', name => 'Foo', price => 23.45 );

$cart->update( sku => 'ABC', quantity => 3 );

my $product = Interchange::Cart::Product->new( ... );

$cart->add($product);

$cart->apply_cost( ... );

my $total = $cart->total;

ATTRIBUTES

id

Cart id can be used for subclasses, e.g. primary key value for carts in the database.

name

The cart name. Default is 'main'.

products

Called without args returns a hash reference of Interchange6::Cart::Product. Should not normally be called with args but rather via the various "PRODUCT METHODS" detailed below.

sessions_id

The session ID for the cart.

subtotal

Returns current cart subtotal excluding costs.

total

Returns current cart total including costs.

users_id

The user id of the logged in user.

PRODUCT METHODS

clear

Removes all products from the cart.

count

Returns the number of different products in the shopping cart. If you have 5 apples and 6 pears it will return 2 (2 different products).

is_empty

Return boolean 1 or 0 depending on whether the cart is empty or not.

product_get($index)

Returns the product at the specified index;

product_index( sub {...})

This method returns the index of the first matching product in the cart. The matching is done with a subroutine reference you pass to this method. The subroutine will be called against each element in the array until one matches or all elements have been checked.

This method requires a single argument.

my $index = $cart->product_index( sub { $_->sku eq 'ABC' } );

products_array

Returns an array of Interchange::Cart::Product(s)

OTHER METHODS

See Interchange6::Role::Costs for details of cost attributes and methods.

new

Inherited method. Returns a new Cart object.

add($product)

Add product to the cart. Returns product in case of success.

The product is an Interchange6::Cart::Product or a hash (reference) of product attributes that would be passed to Interchange6::Cart::Product->new().

add_hook( $hook );

This binds a coderef to an installed hook.

$hook = Interchange6::Hook->new(
    name => 'before_cart_remove',
    code => sub {
        my ( $cart, $product ) = @_;
        if ( $product->sku eq '123' ) {
            die 'Product not removed due to hook.';
        }
    }
)

$cart->add_hook( $hook );

See "HOOKS" for details of the available hooks.

find

Searches for an cart product with the given SKU. Returns cart product in case of sucess or undef on failure.

if ($product = $cart->find(9780977920174)) {
    print "Quantity: $product->{quantity}.\n";
}

has_subtotal

predicate on "subtotal".

has_total

predicate on "total".

quantity

Returns the sum of the quantity of all products in the shopping cart, which is commonly used as number of products. If you have 5 apples and 6 pears it will return 11.

print 'Products in your cart: ', $cart->quantity, "\n";

remove($sku)

Remove product from the cart. Takes SKU of product to identify the product.

seed $product_ref

Seeds products within the cart from $product_ref.

NOTE: use with caution since any existing products in the cart will be lost and since cart hooks are not executed since $cart->add is not used.

$cart->seed([
    { sku => 'BMX2015', price => 20, quantity = 1 },
    { sku => 'KTM2018', price => 400, quantity = 5 },
    { sku => 'DBF2020', price => 200, quantity = 5 },
]);

update

Update quantity of products in the cart.

Parameters are pairs of SKUs and quantities, e.g.

$cart->update(9780977920174 => 5,
              9780596004927 => 3);

Triggers before_cart_update and after_cart_update hooks.

A quantity of zero is equivalent to removing this product, so in this case the remove hooks will be invoked instead of the update hooks.

HOOKS

The following hooks are available:

before_cart_add_validate

Called in "add" for items added as hash(ref)s. Not called for products passed into "add" that are already Interchange6::Cart::Product objects.

Receives: $cart, \%args

before_cart_add

Called in "add" immediately before the Interchange6::Cart::Product is added to the cart.

Receives: $cart, $product

after_cart_add

Called in "add" after product has been added to the cart.

Receives: $cart, $product

before_cart_remove_validate

Called at start of "remove" before arg has been validated.

Receives: $cart, $sku

before_cart_remove

Called in "remove" before product is removed from cart.

Receives: $cart, $product

after_cart_remove

Called in "remove" after product has been removed from cart.

Receives: $cart, $product

before_cart_update
after_cart_update
before_cart_clear
after_cart_clear
before_cart_set_users_id
after_cart_set_users_id
before_cart_set_sessions_id
after_cart_set_sessions_id
before_cart_rename
after_cart_rename

AUTHORS

Stefan Hornburg (Racke), <racke@linuxia.de>
Peter Mottram (SysPete), <peter@sysnix.com>

LICENSE AND COPYRIGHT

Copyright 2011-2014 Stefan Hornburg (Racke) <racke@linuxia.de>.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.