Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. private function _applyDiscounts() {
  2.  
  3.         if (isset($this->basket['coupons']) && count($this->basket['coupons'])>0) {
  4.             $subtotal = $tax_total = 0;
  5.             $coupon = false;
  6.  
  7.             // COUPONS FIRST!!
  8.             foreach ($this->basket['coupons'] as $key => $data) {
  9.        
  10.                 if (!$data['gc']) {
  11.                    
  12.                     $coupon = true;
  13.  
  14.                     if (!empty($data['product'])) {
  15.                         $products = unserialize($data['product']);
  16.                         $incexc = array_shift($products);
  17.                         $product_count = count($products);
  18.                     } else {
  19.                         $product_count = 0;
  20.                     }
  21.            
  22.                     foreach ($this->basket['contents'] as $hash => $item) {
  23.                         if ($product_count==0 || $incexc == 'include' && in_array($item['id'], $products) || $incexc == 'exclude' && !in_array($item['id'], $products)) {
  24.                             if($item['total_price_each']>0) {
  25.                                 $subtotal += ($item['total_price_each'] * $item['quantity']);
  26.                             }
  27.                             if($item['tax_each']['amount']>0) {
  28.                                 $tax_total += $item['tax_each']['amount'];
  29.                             }
  30.                         } elseif($item['total_price_each']>0) { // excluded items CAN be used against gift certificates!!
  31.                             $excluded_products[$hash] = $item;
  32.                         }
  33.                     }
  34.            
  35.                     if($data['shipping'] && $this->basket['shipping']['value']>0) {
  36.                         $subtotal += $this->basket['shipping']['value'];
  37.                         if($this->basket['shipping']['tax']['amount']>0){
  38.                             $tax_total += $this->basket['shipping']['tax']['amount'];
  39.                         }
  40.                     } elseif($this->basket['shipping']['value']>0) {
  41.                         $excluded_shipping = $this->basket['shipping'];
  42.                     }
  43.  
  44.                     $ave_tax_rate = ($tax_total / $subtotal);
  45.  
  46.                     $discount = ($data['type']=='percent') ? $subtotal*($data['value']/100) : $data['value'];
  47.  
  48.                     if($discount<$subtotal){
  49.                         $subtotal -= $discount;
  50.                         $this->_discount = $discount;
  51.                         $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$discount);
  52.                     } elseif($discount>=$subtotal) {
  53.                         $this->_discount = $subtotal;
  54.                         $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$subtotal);
  55.                         $subtotal = 0;
  56.                         if((!is_array($excluded_products) && !is_array($excluded_shipping))) {
  57.                             $GLOBALS['tax']->adjustTax(0);
  58.                             foreach($this->basket['coupons'] as $key => $data) {
  59.                                 if($data['gc']) unset($this->basket['coupons'][$key]); 
  60.                             }
  61.                             $this->save();
  62.                             return true; // nothing else to check.. return
  63.                         }
  64.                     }  
  65.                 }
  66.             }
  67.            
  68.             if(!$coupon) {
  69.                 foreach ($this->basket['contents'] as $hash => $item) {
  70.                     if($item['total_price_each']>0) {
  71.                         $subtotal += ($item['total_price_each'] * $item['quantity']);
  72.                     }
  73.                     if($item['tax_each']['amount']>0) {
  74.                         $tax_total += $item['tax_each']['amount'];
  75.                     }
  76.                 }
  77.        
  78.                 if($this->basket['shipping']['value']>0) {
  79.                     $subtotal += $this->basket['shipping']['value'];
  80.                     if($this->basket['shipping']['tax']['amount']>0){
  81.                         $tax_total += $this->basket['shipping']['tax']['amount'];
  82.                     }
  83.                 }
  84.            
  85.                 $ave_tax_rate = ($tax_total / $subtotal);
  86.            
  87.             } else {
  88.                 if((is_array($excluded_products) || is_array($excluded_shipping))) {
  89.                     $excluded_subtotal = $excluded_tax_total = 0;
  90.                     if(is_array($excluded_products)) {
  91.                         foreach ($excluded_products as $hash => $item) {
  92.                             if($item['total_price_each']>0) {
  93.                                 $excluded_subtotal += ($item['total_price_each'] * $item['quantity']);
  94.                             }
  95.                             if($item['tax_each']['amount']>0) {
  96.                                 $excluded_tax_total += $item['tax_each']['amount'];
  97.                             }
  98.                         }
  99.                     }
  100.                     if(is_array($excluded_shipping) && $excluded_shipping['value']>0) {
  101.                         $excluded_subtotal += $excluded_shipping['value'];
  102.                         if($excluded_shipping['tax']['amount']>0){
  103.                             $excluded_tax_total += $excluded_shipping['tax']['amount'];
  104.                         }
  105.                     }
  106.                     if($excluded_tax_total>0) {
  107.                         $excluded_ave_tax_rate = ($excluded_tax_total / $excluded_subtotal);
  108.                         $ave_tax_rate = ($ave_tax_rate + $excluded_ave_tax_rate) / 2;
  109.                     }
  110.                     $subtotal += $excluded_subtotal;
  111.                 }
  112.  
  113.             }          
  114.            
  115.             // GIFT CERTS SECOND!!
  116.             foreach ($this->basket['coupons'] as $key => $data) {
  117.                 if($data['gc'] && $subtotal==0) {
  118.                     // Gift cert not needed so remove
  119.                     unset($this->basket['coupons'][$key]);
  120.                 } elseif ($data['gc'] && $subtotal>0) {
  121.  
  122.                     $discount   = $data['value'];
  123.  
  124.                     if($discount<$subtotal){
  125.                         $subtotal -= $discount;
  126.                         $this->_discount += $discount;
  127.                         $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$discount);
  128.                         $remainder = 0;
  129.                     } elseif($discount>=$subtotal) {
  130.                         $remainder = $discount - $subtotal;
  131.                         $this->basket['coupons'][$key]['value_display'] = sprintf('%.2F',$subtotal);
  132.                         $this->_discount += $subtotal;
  133.                         $subtotal = 0;
  134.                     }
  135.                     $this->basket['coupons'][$key]['remainder'] = $remainder;  
  136.                 }
  137.             }
  138.             $tax = ($subtotal>0) ? ($subtotal*$ave_tax_rate) : 0;
  139.             $GLOBALS['tax']->adjustTax($tax);
  140.  
  141.             foreach ($GLOBALS['hooks']->load('class.cart.apply_discounts') as $hook) include $hook;
  142.  
  143.             $this->save();
  144.             return true;
  145.         }
  146.  
  147.         return false;
  148.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement