[ic] proposal: Interchange6 Cart costs change

Peter Mottram peter at sysnix.com
Wed Sep 17 10:43:30 UTC 2014


Yesterday I started working on extending the Interchange6::Cart costs
framework to allow costs to be applied to cart products in addition to
the cart itself. One major issue that was discovered is the handling of
taxes along with discounts which is currently messed up. Having
discussed this with racke we now propose to change the way discounts are
handled.

Currently discounts can only be applied to the cart as a whole as a
negative cost. You may continue to do that if you wish but do NOT expect
that to work correctly with taxes. The new proposal is as follows:

Interchange6::Cart::Product will have the following attributes added:

discount
discount_percent
original_price
subtotal

With no discount applied 'original_price' will hold the same value as
'price' and both discount and discount_percent will be zero. Discount
can be applied to the product as it is added to the cart using ::Cart's
'add' method:

$cart->add({ sku => 'ABC', name => 'product1', price => 3.45,
discount_percent => 30, quantity => 2 });

This would result in a product like this:

{
    sku => 'ABC',
    name => 'product1',
    original_price => 3.45,
    discount_percent => 30
    discount => 1.04
    price => 2.41
    quantity => 2
    subtotal => 4.82
}

Rounding is performed as sprintf("%.2f", $number) on single unit price.

If a simple fixed discount amount in currency is required then the
following is also possible:

$cart->add({ sku => 'ABC', name => 'product1', price => 3.45, discount
=> 1, quantity => 2 });

This would result in a product like this:

{
    sku => 'ABC',
    name => 'product1',
    original_price => 3.45,
    discount_percent => 29.99
    discount => 1
    price => 2.45
    quantity => 2
    subtotal => 4.90
}

Rounding of discount_percent is applied in the same way as price in the
previous example.

If both discount and discount_percent are passed as args this will cause
an exception.

The subtotal attribute will have its value updated automatically
whenever quantity or discount/discount_percent is changed.

Cart-wide discounts effectively become per-product discounts. This is
essential for countries which charge varying rates of sales tax (e.g. EU
VAT).

A new ::Cart method will be added: apply_discount

This can be called with a single numeric as argument:

$cart->apply_discount(4);

This 4% discount is then applied to the current 'price' attribute of
each product and other attributes are then recalculated to reflect the
change. When applied to the second example above this would result in
this product:

{
    sku => 'ABC',
    name => 'product1',
    original_price => 3.45,
    discount_percent => 31.88
    discount => 1.1
    price => 2.35
    quantity => 2
    subtotal => 4.70
}

The following would produce the same result:

$cart->apply_discount({ discount_percent => 4 });

Currently there are no plans to allow a fixed amount of cart discount to
be applied though this is left as a possibility for the future if there
is demand.

In addition to the existing cart costs for taxes, handling, shipping,
etc we also now support per-product costs for the same uses. Product
costs are applied to the product subtotal in the same way that cart
costs are applied to the cart subtotal.

There is nothing to stop you from using the existing costs
infrastructure for discounts: just be aware that they are all applied to
subtotal so a combination of discount and tax costs may not produce the
total you are expecting.

This has NOT yet been implemented but it will be very soon unless there
are objections. Feel free to throw in you 1c worth.

Cheers
PeteM









More information about the interchange-users mailing list