NAME
Nitesi::Cart - Cart class for Nitesi Shop Machine
DESCRIPTION
Generic cart class for Nitesi.
CART ITEMS
Each item in the cart has at least the following attributes:
- sku
-
Unique item identifier.
- name
-
Item name.
- quantity
-
Item quantity.
- price
-
Item price.
CONSTRUCTOR
new
init
Initializer which receives the constructor arguments, but does nothing. May be overridden in a subclass.
METHODS
items
Returns items in the cart.
subtotal
Returns subtotal of the cart.
total
Returns total of the cart.
add $item
Add item to the cart. Returns item in case of success.
The item is a hash (reference) which is subject to the following conditions:
- sku
-
Item identifier is required.
- name
-
Item name is required.
- quantity
-
Item quantity is optional and has to be a natural number greater than zero. Default for quantity is 1.
- price
-
Item price is required and a positive number.
Price is required, because you want to maintain the price that was valid at the time of adding to the cart. Should the price in the shop change in the meantime, it will maintain this price. If you would like to update the pages, you have to do it before loading the cart page on your shop.
Example: Add 5 BMX2012 products to the cart
$cart->add( sku => 'BMX2012', quantity => 5, price => 200);
Example: Add a BMX2012 product to the cart.
$cart->add( sku => 'BMX2012', price => 200);
remove $sku
Remove item from the cart. Takes SKU of item to identify the item.
update
Update quantity of items 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 item, so in this case the remove hooks will be invoked instead of the update hooks.
clear
Removes all items from the cart.
find
Searches for an cart item with the given SKU. Returns cart item in case of sucess.
if ($item = $cart->find(9780977920174)) {
print "Quantity: $item->{quantity}.\n";
}
quantity
Returns the sum of the quantity of all items in the shopping cart, which is commonly used as number of items. If you have 5 apples and 6 pears it will return 11.
print 'Items in your cart: ', $cart->quantity, "\n";
created
Returns the time (epoch) when the cart was created.
last_modified
Returns the time (epoch) when the cart was last modified.
count
Returns the number of different items in the shopping cart. If you have 5 apples and 6 pears it will return 2 (2 different items).
apply_cost
Apply cost to cart. apply_cost is a generic method typicaly used for taxes, discounts, coupons, gift certificates,...
Example: Absolute cost
Uses absolute value for amount. Amount 5 is 5 units of currency used (ie. $5).
$cart->apply_cost(amount => 5, name => 'shipping', label => 'Shipping');
Example: Relative cost
Uses percentage instead of value for amount. Amount 0.19 in example is 19%.
relative is a boolean value (0/1).
$cart->apply_cost(amount => 0.19, name => 'tax', label => 'VAT', relative => 1);
Example: Inclusive cost
Same as relative cost, but it assumes that tax was included in the subtotal already, and only displays it (19% of subtotal value in example). Inclusive is a boolean value (0/1).
$cart->apply_cost(amount => 0.19, name => 'tax', label => 'Sales Tax', relative => 1, inclusive => 1);
clear_cost
It removes all the costs previously applied (using apply_cost). Used typically if you have free shipping or something similar, you can clear the costs.
cost
Returns particular cost by position or by name.
Example: Return tax value by name
$cart->cost('tax');
Returns value of the tax (absolute value in your currency, not percantage)
Example: Return tax value by position
$cart->cost(0);
Returns the cost that was first applied to subtotal. By increasing the number you can retrieve other costs applied.
id
Get or set id of the cart. This can be used for subclasses, e.g. primary key value for carts in the database.
name
Get or set the name of the cart.
error
Returns last error.
seed $item_ref
Seeds items within the cart from $item_ref.
Example:
$cart->seed([
{ sku => 'BMX2015', price => 20, quantity = 1 },
{ sku => 'KTM2018', price => 400, quantity = 5 },
{ sku => 'DBF2020', price => 200, quantity = 5 },
]);
AUTHOR
Stefan Hornburg (Racke), <racke@linuxia.de>
LICENSE AND COPYRIGHT
Copyright 2011-2013 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.