On Tue, May 08, 2012 at 07:44:43PM +0530, Chankey Pathak wrote:
> Hi folks,
> 
> One of my friend is having a problem, he is not in the mailing list so I am
> posting this problem on his behalf. Please check how can this problem be
> solved and let me know, I'll let him know the answer, thanks :)
> 
> *Problem:*
> Where i work we use a perl based shopping cart (Cartit). It has a quantity
> discount feature however it is limited to the discount being applied to one
> item at a time (for example buy 25 or more of druk beads 10mm and get
> 10%off. Currently the shopping cart only applies the quantity discount one
> item at a time.) we would like it to be have a "mix and match discount" -
> using the previous example we like offer a customer the ability to get a
> discount based on the total quantity ordered (based upon the description
> being the same but the options ($description_ext being different)(for
> example 10 red druk beads,10 blue druk beads, 10 white druk beads = 30
> beads so they should get a 10 percent discount as long as the total order
> amounts meets the level for the quantity discount.
> the program already takes the "itemline" = FCART which is in an array and
> splits into a string $FCART from which ,using the SPLIT function various
> variables are extracted ($quantity,$sku,$description,etc..) and used for
> calculations or other functions. However using a FOR loop this splitting 
> isdone
> one line at a time. Before the line is available for printing or other
> things it is sent to a subroutine where the quantity discount is decided on
> a line by line basis. We were wondering if would be possible to write a
> subroutine that by using hashes and/ or arrays it would do the quantity for
> discount on an accumulative basis( foe example - for all items having the
> same sku or description (regardless on any options chosen)- it would add up
> all the quantities and then we could use that "total quantity" for the
> qualification for the quantity discount and pass it onto the "sub
> get_discount_amountofcode" routine.

You can send hash refs or array refs to a subroutine and dereference there to
calculate the total discount.
e.g. call your subroutine so:

&get_discount_amountofcode($quantity, $price, $discountcode, \%totalquantities);
and then use 
my ( $myquantity, $myamount, $codestuff, $totalquantities ) = @_;

accessing values with 
$totalquantities->{...} 


Of course you;d have to manage the program flow - what happens if the customer
buys 40 red druk beads 10 yellow 5 black and 10 pink.

In your current implementation the customer only gets the discount on the first
item, the remainder don't attract that discount.

Now if it is 10 red, 10 yellow, 40 black and 5 pink - what happens then?  
In your current situation again the discount is only applied to the black beads
and not the rest.  

Effectively for each description, you need to total the amount ordered to
decide if the discount applies to all in that description or none.  Then go
back and process that discount for each part of the total order.

You could work through the @FCART array, creating a hash with a key
value of the $description and a value of the total amount ordered in that
description, then progress through the order again, using that hash to decide
if a 'global' (as in $description) discount applies.

Your subroutine would require knowledge of the $description, so you'd have to
include that in your subroutine paramaters.

Or completely restructure the order from an array to a hash of hashes based on
$description keys then $description_ext keys with values and run a cumulative
total within that $description hash so that you have something like 

%order = (
  'druk beads' => (
      10mm => (
              red => 10,
              blue =>5,
              yellow => 6,
              black => 40,
              total => 61,
              ),
      8mm => (
              red => 10,
              blue =>5,
              yellow => 6,
              total => 21,
              ),
      total => 82
    ),
    earstud => ( ... 
    ),
    ...
);
Then you have access to your total amounts, can apply the discount and can then
do the printing accordingly.

Hope this makes some sense.

Regards

Lesley

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to