PHP Class OrderModifier, silvershop-core

This is especially useful for discounts or a shipping calculator. Based on what items are in the users cart, you could charge an additional amount to the order for shipping costs. There are three types of OrderModifiers (denoted by the "Type" enum field below): - 'None': This is for a state when you need to indicate to the user an amount, but it doesn't modify the total. (very useful for indicating tax on an order where tax is inclusive on the products being purchased) - 'Chargable': Charges an amount to the order. A good example of this would be shipping costs. - 'Deductable': Discounts an amount from the order. A good example is a discount being offered to the customer; a rewards scheme, or offering a 10% discount if the customer is a VIP or frequent customer. OrderModifiers can be as simple or as complex as needed. {@link OrderModifier::Amount()} returns the amount to be charged or discounted. The amount is calculated in {@link OrderModifier::calculateAmount()} (this should be implemented on your modifier classes). If the OrderModifier is saved to the DB (user has checked out), then {@link OrderModifier::Amount()} will return the amount from the DB instead of performing a calculation.
Inheritance: extends DataObject
Show file Open project: burnbright/silverstripe-shop Class Usage Examples

Public Methods

Method Description
Amount ( ) This function is always called to determine the amount this modifier needs to charge or deduct.
IsChargable ( ) : boolean Checks if this modifier has type = Chargable
TableValue ( ) Monetary to use in templates.
Total ( ) : boolean Provides a modifier total that is positive or negative, depending on whether the modifier is chargable or not.
canRemove ( ) : boolean Checks if the modifier can be removed.
modify ( $subtotal, $forcecalculation = false ) Modifies the incoming value by adding, subtracting or ignoring the value this modifier calculates.
required ( ) Specifies whether this modifier is always required in an order.
valid ( ) Check if the modifier should be in the cart.
value ( float $incoming ) Calculates value to store, based on incoming running total.

Method Details

Amount() public method

If the modifier exists in the DB, in which case it already exists for a given order, we just return the Amount data field from the DB. This is for existing orders. If this is a new order, and the modifier doesn't exist in the DB ($this->ID is 0), so we return the amount from $this->LiveAmount() which is a calculation based on the order and it's items.
public Amount ( )

IsChargable() public method

Checks if this modifier has type = Chargable
public IsChargable ( ) : boolean
return boolean

TableValue() public method

Monetary to use in templates.
public TableValue ( )

Total() public method

Provides a modifier total that is positive or negative, depending on whether the modifier is chargable or not.
public Total ( ) : boolean
return boolean

canRemove() public method

Checks if the modifier can be removed.
public canRemove ( ) : boolean
return boolean

modify() public method

Sets $this->Amount to the calculated value;
public modify ( $subtotal, $forcecalculation = false )
$subtotal - running total to be modified
$forcecalculation - force calculating the value, if order isn't in cart

required() public method

Specifies whether this modifier is always required in an order.
public required ( )

valid() public method

Check if the modifier should be in the cart.
public valid ( )

value() public method

Calculates value to store, based on incoming running total.
public value ( float $incoming )
$incoming float the incoming running total.