Skip to content
Permalink
Browse files

Fixed issue when choosing the correct delivery charge condition

  • Loading branch information
sampoyigi committed Sep 15, 2017
1 parent 8cd4ee2 commit ace5f6756dc6f2ae037887e199827c7eca807cfc
Showing with 42 additions and 30 deletions.
  1. +42 −30 system/tastyigniter/libraries/Location.php
@@ -275,43 +275,55 @@ public function minimumOrder($cart_total = '0') {
return $this->deliveryCondition('totals', $cart_total);
}

public function deliveryCondition($sort_by = 'amounts', $cart_total = FALSE) {
$area_id = isset($this->delivery_areas[$this->area_id]) ? $this->area_id : key($this->delivery_areas);
public function deliveryCondition($sort_by = 'amounts', $cart_total = FALSE)
{
$area_id = isset($this->delivery_areas[$this->area_id]) ? $this->area_id : key($this->delivery_areas);

$condition = array();
if (isset($this->delivery_areas[$area_id]['charge'], $this->delivery_areas[$area_id]['charge'][$sort_by])) {
$charge = $this->delivery_areas[$area_id]['charge'];
$condition = [];
if (isset($this->delivery_areas[$area_id]['charge'], $this->delivery_areas[$area_id]['charge'][$sort_by])) {
$charge = $this->delivery_areas[$area_id]['charge'];

asort($charge[$sort_by]);
asort($charge[$sort_by]);

// if a condition on all orders exist, use it
if ($key = array_search('all', $charge['conditions'])) {
$charge[$sort_by] = array($key => $charge[$sort_by][$key]);
}
$minimumTotal = min($charge['totals']);

$count = 1;
foreach ($charge[$sort_by] as $key => $amt) {
$con = isset($charge['conditions'][$key]) ? $charge['conditions'][$key] : 'above';
$total = isset($charge['totals'][$key]) ? $charge['totals'][$key] : 0;

if ($cart_total === FALSE) {
$condition[] = isset($this->delivery_areas[$area_id]['condition'][$key]) ? $this->delivery_areas[$area_id]['condition'][$key] : '';
} else if (empty($cart_total) OR ($con === 'above' AND $cart_total >= $total)) {
return $charge[$sort_by][$key];
} else if ($sort_by == 'totals' AND $count == count($charge[$sort_by])) {
return min($charge[$sort_by]);
}
// Overall summary requested
if ($cart_total === FALSE) {
$condition = isset($this->delivery_areas[$area_id]['condition'])
? $this->delivery_areas[$area_id]['condition'] : [];

if ($count === count($charge[$sort_by]) AND min($charge['totals']) > 0) {
$condition[] = "0|below|".min($charge['totals']);
}
if ($minimumTotal > 0) {
$condition[] = "0|below|".$minimumTotal;
}

$count++;
}
}
return $condition;
}

return ($cart_total === FALSE) ? $condition : NULL;
}
// check overall minimum total is greater than cart total
if ($minimumTotal > $cart_total) {
return $sort_by == 'amounts' ? 0 : $minimumTotal;
}

$conditions = array_keys($charge[$sort_by]);
foreach ($conditions as $index => $key) {
// if ONLY condition on all orders exist, use it
if (in_array('all', $charge['conditions']) AND count($conditions) == 1) {
return $charge[$sort_by][$key];
}

$nextKey = isset($conditions[$index + 1]) ? $conditions[$index + 1] : 0;
$total = isset($charge['totals'][$key]) ? $charge['totals'][$key] : 0;
$nextTotal = isset($charge['totals'][$nextKey]) ? $charge['totals'][$nextKey] : 0;
$amount = isset($charge[$sort_by][$key]) ? $charge[$sort_by][$key] : 0;

if ($total < $cart_total AND ($nextTotal == 0 OR $nextTotal > $cart_total)) {
return $amount;
}
}
}

return ($cart_total === FALSE) ? $condition : null;
}

public function getReservationInterval() {
return (!empty($this->local_info['reservation_time_interval'])) ? $this->local_info['reservation_time_interval'] : $this->CI->config->item('reservation_time_interval');

0 comments on commit ace5f67

Please sign in to comment.
You can’t perform that action at this time.