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
See also "ATTRIBUTES" in Interchange6::Role::Costs.
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.
Anything passed in as a value on object instantiation is ignored. To load products into a cart the preferred methods are "add" and "seed" which make sure appropriate arguements are passed.
product_class
To allow use of a subclassed Interchange6::Cart::Product. Defaults to Interchange6::Cart::Product
.
sessions_id
The session ID for the cart.
subtotal
Returns current cart subtotal excluding costs.
users_id
The user id of the logged in user.
weight
Returns total weight of all products in the cart. If all products have unedfined weight then this returns undef.
METHODS
See also "METHODS" in Interchange6::Role::Costs.
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_delete($index)
Deletes the product at the specified index.
product_get($index)
Returns the product at the specified index.
product_grep( sub {...})
This method returns every element matching a given criteria, just like Perl's core grep function. This method requires a subroutine which implements the matching logic. The returned list is provided as a Collection::Array object.
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' } );
product_push($product)
Like Perl's normal push
this adds the supplied Interchange::Cart::Product to "products".
product_set($index, $product)
Sets the product at the specified index in "products" to the supplied Interchange::Cart::Product.
products_array
Returns an array of Interchange::Cart::Product(s)
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().
find
Searches for a 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".
has_weight
predicate on "weight".
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
Remove product from the cart. Takes SKU of product to identify the product.
$self->remove('ABC123');
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.
$cart->seed([
{ sku => 'BMX2015', price => 20, quantity = 1 },
{ sku => 'KTM2018', price => 400, quantity = 5 },
{ sku => 'DBF2020', price => 200, quantity = 5 },
]);
If any product fails to be added (for example bad product args) then an exception is thrown and no products will be added to cart.
On success returns "products".
update
Update quantity of products in the cart.
Parameters are pairs of SKUs and quantities, e.g.
$cart->update(9780977920174 => 5,
9780596004927 => 3);
Or a list of hash references, e.g.
$cart->update(
{ index => 3, quantity => 2 },
{ id => 73652, quantity => 1 },
{ sku => 'AJ12', quantity => 4 },
);
A quantity of zero is equivalent to removing this product.
Returns an array of updated products that are still in the cart. Products removed via quantity 0 or products for which quantity has not changed will not be returned.
If you have products that cannot be combined in the cart (see "combine" in Interchange6::Cart::Product and "should_combine_by_sku" in Interchange6::Cart::Product) then it is possible to have multiple cart products with the same sku. In this case the arguments to "update" must be a list of hash references using either "id" in Interchange6::Cart::Product or index
where index
is the zero-based index of the product within "products".
AUTHORS
Stefan Hornburg (Racke), <racke@linuxia.de>
Peter Mottram (SysPete), <peter@sysnix.com>
LICENSE AND COPYRIGHT
Copyright 2011-2016 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.