Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function _applyDiscounts() {
- if (isset($this->basket['coupons']) && count($this->basket['coupons'])>0) {
- $subtotal = $tax_total = 0;
- $coupon = false;
- // COUPONS FIRST!!
- foreach ($this->basket['coupons'] as $key => $data) {
- if (!$data['gc']) {
- $coupon = true;
- if (!empty($data['product'])) {
- $products = unserialize($data['product']);
- $incexc = array_shift($products);
- $product_count = count($products);
- } else {
- $product_count = 0;
- }
- foreach ($this->basket['contents'] as $hash => $item) {
- if ($product_count==0 || $incexc == 'include' && in_array($item['id'], $products) || $incexc == 'exclude' && !in_array($item['id'], $products)) {
- if($item['total_price_each']>0) {
- $subtotal += ($item['total_price_each'] * $item['quantity']);
- }
- if($item['tax_each']['amount']>0) {
- $tax_total += $item['tax_each']['amount'];
- }
- } elseif($item['total_price_each']>0) { // excluded items CAN be used against gift certificates!!
- $excluded_products[$hash] = $item;
- }
- }
- if($data['shipping'] && $this->basket['shipping']['value']>0) {
- $subtotal += $this->basket['shipping']['value'];
- if($this->basket['shipping']['tax']['amount']>0){
- $tax_total += $this->basket['shipping']['tax']['amount'];
- }
- } elseif($this->basket['shipping']['value']>0) {
- $excluded_shipping = $this->basket['shipping'];
- }
- $ave_tax_rate = ($tax_total / $subtotal);
- $discount = ($data['type']=='percent') ? $subtotal*($data['value']/100) : $data['value'];
- if($discount<$subtotal){
- $subtotal -= $discount;
- $this->_discount = $discount;
- $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$discount);
- } elseif($discount>=$subtotal) {
- $this->_discount = $subtotal;
- $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$subtotal);
- $subtotal = 0;
- if((!is_array($excluded_products) && !is_array($excluded_shipping))) {
- $GLOBALS['tax']->adjustTax(0);
- foreach($this->basket['coupons'] as $key => $data) {
- if($data['gc']) unset($this->basket['coupons'][$key]);
- }
- $this->save();
- return true; // nothing else to check.. return
- }
- }
- }
- }
- if(!$coupon) {
- foreach ($this->basket['contents'] as $hash => $item) {
- if($item['total_price_each']>0) {
- $subtotal += ($item['total_price_each'] * $item['quantity']);
- }
- if($item['tax_each']['amount']>0) {
- $tax_total += $item['tax_each']['amount'];
- }
- }
- if($this->basket['shipping']['value']>0) {
- $subtotal += $this->basket['shipping']['value'];
- if($this->basket['shipping']['tax']['amount']>0){
- $tax_total += $this->basket['shipping']['tax']['amount'];
- }
- }
- $ave_tax_rate = ($tax_total / $subtotal);
- } else {
- if((is_array($excluded_products) || is_array($excluded_shipping))) {
- $excluded_subtotal = $excluded_tax_total = 0;
- if(is_array($excluded_products)) {
- foreach ($excluded_products as $hash => $item) {
- if($item['total_price_each']>0) {
- $excluded_subtotal += ($item['total_price_each'] * $item['quantity']);
- }
- if($item['tax_each']['amount']>0) {
- $excluded_tax_total += $item['tax_each']['amount'];
- }
- }
- }
- if(is_array($excluded_shipping) && $excluded_shipping['value']>0) {
- $excluded_subtotal += $excluded_shipping['value'];
- if($excluded_shipping['tax']['amount']>0){
- $excluded_tax_total += $excluded_shipping['tax']['amount'];
- }
- }
- if($excluded_tax_total>0) {
- $excluded_ave_tax_rate = ($excluded_tax_total / $excluded_subtotal);
- $ave_tax_rate = ($ave_tax_rate + $excluded_ave_tax_rate) / 2;
- }
- $subtotal += $excluded_subtotal;
- }
- }
- // GIFT CERTS SECOND!!
- foreach ($this->basket['coupons'] as $key => $data) {
- if($data['gc'] && $subtotal==0) {
- // Gift cert not needed so remove
- unset($this->basket['coupons'][$key]);
- } elseif ($data['gc'] && $subtotal>0) {
- $discount = $data['value'];
- if($discount<$subtotal){
- $subtotal -= $discount;
- $this->_discount += $discount;
- $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$discount);
- $remainder = 0;
- } elseif($discount>=$subtotal) {
- $remainder = $discount - $subtotal;
- $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$subtotal);
- $this->_discount += $subtotal;
- $subtotal = 0;
- }
- $this->basket['coupons'][$key]['remainder'] = $remainder;
- }
- }
- $tax = ($subtotal>0) ? ($subtotal*$ave_tax_rate) : 0;
- $GLOBALS['tax']->adjustTax($tax);
- foreach ($GLOBALS['hooks']->load('class.cart.apply_discounts') as $hook) include $hook;
- $this->save();
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement