Opencart calculation when applying coupon value?

990 views Asked by At

I am using opencart version 2.3.0.2 ..

where i m getting all values except when i coupon value the tax values getting wrong and displaying values also getting wrong.

I want each product unit tax before applying coupon and unit-discount after applying coupon value.

I have coded something below but not able to get correct values with unit discount.. by using below code i m getting total discount amount , i want individual discount tax and discount amount when applying coupon value

$this->load->model('extension/total/coupon');

            $coupon_info = $this->model_extension_total_coupon->getCoupon($this->session->data['coupon']);
            if(isset($coupon_info) && !empty($coupon_info))
            {
            foreach($product as $taxpro) {


                if($coupon_info['type'] == 'F') 
                        {
                            $discount = $coupon_info['discount'] * ($taxpro['total'] / $sub_total);
                        } else
                        if ($coupon_info['type'] == 'P') 
                        {
                            $discount = $taxpro['total'] / 100 * $coupon_info['discount'];
                        }

            $tax_rates = $this->tax->getRates($taxpro['total'] - ($taxpro['total'] - $discount), $taxpro['tax_class_id']);  
            foreach ($tax_rates as $tax_rate) 
                            {
                                echo "<pre>"; print_r($total_data['taxes'][$tax_rate['tax_rate_id']]);echo "<pre>";
                                if ($tax_rate['type'] == 'P') 
                                {

                                    $total_data['taxes'][$tax_rate['tax_rate_id']] -= $tax_rate['amount'];
                                }



                                 echo "<pre>"; print_r($total_data['taxes'][$tax_rate['tax_rate_id']]);
                            }


             foreach($tax_rates as $tax_coupon)
             {
                 $protax = $tax_coupon['amount'];

                 $protax_unit = round($protax*$taxpro['quantity'],2);
                 $total_unit_tax += $protax_unit;

                 $coupon_tax += $tax_coupon['amount'];
             }
            }

           }
1

There are 1 answers

0
Jesper Johansson On

You will need to save all products with taxes. I have omitted a lot of code to make it more clear on how to think

Example:

<?php 
$prepareDiscount = array();
foreach ($products as $product){
$prepareDiscount[$taxRateForProduct] = $productTotal;

}

Then you will need to iterate over the prepareDiscounts array:

$total = $totalfromOrder;
$coupon_info = $this->model_extension_total_coupon->getCoupon($this->session->data['coupon']);

foreach($prepareDiscount as $taxRate => $value){
$discountPercentForRate = $value/$total; 
$discountForRate = $discountPercentForRate * $coupon_info['discount'];
// Now you have the taxRate And the part of the discount which have that rate
}

This is an example code. If you are interested in more in depth just check how payment gateways did it. The key to get all this working is just to prepare the discounts when iterating over the products.