Interchange6::Cart - Cart class for Interchange6 Shop Machine
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;
Generic cart class for Interchange6.
Returns a new Cart object.
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(). See Interchange6::Cart::Product for details.
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' ) { $cart->set_error('Product not removed due to hook.'); } } ) $cart->add_hook( $hook );
See "HOOKS" for details of the available hooks.
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);
Removes all products from the cart.
Removes all the costs previously applied (using apply_cost). Used typically if you have free shipping or something similar, you can clear the costs.
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 percentage)
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.
Returns an array of all costs associated with the cart. Costs are ordered according to the order they were applied.
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).
Returns the time the cart was created as a DateTime object.
Returns the last error.
Searches for an cart product with the given SKU. Returns cart product in case of sucess.
if ($product = $cart->find(9780977920174)) { print "Quantity: $product->{quantity}.\n"; }
Returns cart name
Returns an arrayref of Interchange::Cart::Product(s)
Return boolean 1 or 0 depending on whether the cart is empty or not.
Returns the product at the specified index;
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' } );
An alias for get_products for backwards compatibility.
Returns an array of Interchange::Cart::Product(s)
Returns the time the cart was last modified as a DateTime object.
An alias for get_name and set_name for backwards compatibility.
$cart->name
Returns current name of cart (default is 'main').
$cart->name('newname')
Set new name of cart.
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 product from the cart. Takes SKU of product to identify the product.
Seeds products within the cart from $product_ref.
NOTE: use with caution since any existing products in the cart will be lost. This method primarily exists for testing purposes only.
$cart->seed([ { sku => 'BMX2015', price => 20, quantity = 1 }, { sku => 'KTM2018', price => 400, quantity = 5 }, { sku => 'DBF2020', price => 200, quantity = 5 }, ]);
Returns current cart subtotal excluding costs.
Returns current cart total including costs.
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.
Returns the id of the user if user is logged in.
The following hooks are available:
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
Called in "add" immediately before the Interchange6::Cart::Product is added to the cart.
Receives: $cart, $product
Called in "add" after product has been added to the cart.
Called at start of "remove" before arg has been validated.
Receives: $cart, $sku
Called in "remove" before product is removed from cart.
Called in "remove" after product has been removed from cart.
Stefan Hornburg (Racke), <racke@linuxia.de> Peter Mottram (SysPete), <peter@sysnix.com>
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.
To install Interchange6, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Interchange6
CPAN shell
perl -MCPAN -e shell install Interchange6
For more information on module installation, please visit the detailed CPAN module installation guide.