Jump to content

Recommended Posts

Yes I can  ;),

I have a $discount variable which is

$discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax );

 

And for the array

 

$totalproductprice = $productprice * $record['quantity'];
$Prices = array ($totalproductprice);

foreach ($Prices as $label => $Price)
        	{
        		//code
        		print_r ($Prices);  
        		
        	}

print_r returns a two dimensional array

 

Array ( [0] => 975 ) Array ( [0] => 1567 ) Array ( [0] => 1669 ) Array ( [0] => 800 ) 

 

I would like to make the $discount take effect on an individual array element.

 

ding

which array...this looks like code that is inside a loop. also, i don't see the purpose of this code:

$Prices = array ($totalproductprice);

 

$discount = 0.2; //20% discount
$totalproductprice = $productprice * $record['quantity'];
$dicountedprice = $totalproductprice * (1 - $discount);

 

Is that not what you are looking to do?

What I'm working with is a shopping cart, which has the ability to have a discount on the total price of the cart.

I'm trying to add the functionality to have a discount per item in the cart.

 

I just started php, and decided to learn by using an open source project and add functionality I need :)

 

Although this does work

$discount = 0.2; //20% discount
$totalproductprice = $productprice * $record['quantity'];
$dicountedprice = $totalproductprice * (1 - $discount);

 

It discounts every item.

I have allready added a form with a selectbox o the HTML page, which calls the discount.

When I select the discount for a specific product, all the products are discounted.

 

That is basically where I get stuck.

 

As for this

$Prices = array ($totalproductprice);

I figured if I add all the contents of the cart to an array, I could use foreach to go over the individual elements and see if there is a discount and then add the discount or if there isn't then show just the $totalproductprice.

 

 

 

ok...i see where you are going with this...first i will say:

$Prices = array ($totalproductprice);

creates a new array each time. you would want to create the array before the loop then add the elements to it inside the loop:

$Prices = array();
while(...){
  $Prices[] = $totalproductprice;
}

 

how do you know which products get discounted?

ok...i see where you are going with this...first i will say:

$Prices = array ($totalproductprice);

creates a new array each time

 

Aha! I did not see this. Thank you!!

That was the part that actually got me stuck.

My array was not being properly build, each price was in it's own array with a key of 0.

Now it is properly build and indexed and I'm getting

 

Array ( [0] => 975 [1] => 1567 [2] => 1669 [3] => 800 )

 

instead of

 

Array ( [0] => 975 ) Array ( [0] => 1567 ) Array ( [0] => 1669 ) Array ( [0] => 800 ) 

 

As for

how do you know which products get discounted?

 

I didn't get that far yet, but I think I could make a function to get the position of the element and have it discounted.

 

 

 

I made progress (albeit little), I created a second array with the discounts.

 

$Prices = array();
$Discounts = array();
while(...){
  $Prices[] = $totalproductprice;
  $Discounts[] = $discountProduct; //variable $discountProduct is defined further up in the script
}

 

My logic is to compare the price array and the discount array If there is a value at a certain index in the discount array substract that from the value at the corresponding index at the price array.

 

At this moment for every price array element a discount array element is created with value 0.

 

So I have for the price array

Array ( [0] => 975 [1] => 1567 [2] => 1669 [3] => 800 )

 

and for the discount array

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 ) 

 

When I select a discount for example for index [2] (third product in list) of the products, the entire discount array is filled with the percentage discount

 

Array ( [0] => 195 [1] => 313.4 [2] => 333.8 [3] => 160 ) 

 

instead of just index [2] of discount array

 

How can I make it so that just the corresponding index is updated and subtracted from both array's

 

I hope I'm not confusing anyone, in my head it sound much more logical... really.

Let's cover the meaning of some of these variables....

$discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax );

this code is run after the loop right? should $totalproductprice be the shopping cart total? once a $discount is determined, should that discount be applied to each item in the cart?

No it's not the cart total.

$totalproductprice is the total price of one item, it's the $productprice * $record['quantity'].

so you have

 

donkey  price = €75,-  qty = 2 discount = select discount totalproductprice = €150,-

goat      price = €24,- qty = 1 discount = select discount totalproductprice = €24,-

waterpump price = 34,- qty =1 discount = select discount totalproductprice = €34,-

 

After re-reading your question I just noticed that the code is inside the while loop.

This is also where the $totalproductprice is calculated.

 

I'm trying to make the discount apply per item in the cart.

So when someone chooses to donate money for a donkey (it's for a charity, i'm not a farmer  :D) and they select a discount so that they pay less but give more (through an affiliate) that discount is only applied to the price of €150,-.

 

The fact that it's run inside the while loop is the issue right?

 

you have a loop, which loops over all the items...that has been established. my question now is, when do we know what and how much the discount is? before the loop or not until after the loop? is the function that calculates the discount run on each item? if so, is there any reason we can't use it inside the loop?

Well, the discount itself is calculated in a different script.

The getcoupon() function is defined later in the script.

 

This is is the if statement which defines the $discount variable.

 

if ( $_SESSION['CouponIsValid'] )
        {
            require_once( $config['filepath']."modules/coupon.product.inc.php" );
            $discount = calculatecoupon( getcoupon( ), $totalproductprice, $totalshipping, $totaltax );
        }
        else
        {
            $discountProduct = 0;
        }

 

This if statement is inside the while loop, just before where the $totalproductprice is calculated.

Wrong choice of words  :shy:

What I meant was it was before an if statement where it checks if tax option is selected.

The totalproductprice is defined before the piece of code I posted before. It was late yesterday, just ignore my last post.

 

This is the correct code

$totalproductprice = $productprice * $record['quantity'];
        
    if ( $_SESSION['CouponIsValid'] )
        {
            require_once( $config['filepath']."modules/coupon.product.inc.php" );
            $discountProduct = CalculateCouponProduct( getcouponproduct( ), $totalproductprice, $totalshipping, $totaltax );
        }
        else
        {
            $discountProduct = 0;
        }
        
        if ( $config['pricewithtax'] == "Y" )
        {
            $optionvalue = getpricewithtax( $optionvalue, $record['taxid'] );
            $productprice = getpricewithtax( $record['price'], $record['taxid'] ) + $optionvalue;
            $totalproductprice = $productprice * $record['quantity'] - $discountProduct;
            $Prices[]= $totalproductprice;
            $Discounts[]= $discountProduct;
            $prices = $Prices[0];
            $discounts = $Discounts[0];
            
            $grandtotal = $totalproductprice + $totalshipping;
                        

        }
        else
        {
            $grandtotal = $totalproductprice + $totalshipping + $totaltax;
        }

 

It is difficult to read code when your partially still asleep.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.