/** * This function will find the coupon based on restricted location * by the address. * * @param ISC_QUOTE_ADDRESS_SHIPPING $shippingAddress The shipping address * @param int $couponId The coupon id * @return boolean Return true if the shipping location applied to the coupon. Otherwise, return false. */ protected function _isCouponValidByLocation($shippingAddress, $couponId) { static $validCouponLocationCache = array(); $address = $shippingAddress->getAsArray(); // Do we have a cached result use that $cacheId = md5(strtolower(serialize($address))); if(isset($validCouponLocationCache[$cacheId])) { return $validCouponLocationCache[$cacheId]; } if (!$address['shipzip']) { $validCouponLocationCache[$cacheId] = false; throw new ISC_QUOTE_EXCEPTION(GetLang('CouponLocationNotSpecified'), ISC_QUOTE_EXCEPTION::COUPON_LOCATION_DOES_NOT_SPECIFIED); } else { $address = $shippingAddress->getAsArray(); // Zip Code restriction check if($address['shipzip']) { $couponExist = false; $query = " SELECT c.couponid, cl.value_id, cl.value FROM [|PREFIX|]coupon_locations cl INNER JOIN [|PREFIX|]coupons c ON (c.couponid=cl.coupon_id) WHERE c.couponenabled='1' AND cl.selected_type='zip' AND cl.coupon_id='".(int)$couponId."' AND cl.country_id='".(int)$address['shipcountryid']."' AND '".$GLOBALS['ISC_CLASS_DB']->Quote($address['shipzip'])."' REGEXP REPLACE(REPLACE(CONCAT('^', cl.value, '$'), '*', '.{1,}'), '?', '.') "; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while($coupon = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { if($coupon['value'] == $address['shipzip']) { $couponExist = true; continue; } else { // Score the characters in the string $score = (substr_count($coupon['value'], '*')*10)+(substr_count($coupon['couponid'], '?')); // A lower score means a stronger match, so we use that zone ID if(!isset($lastScore) || $score < $lastScore) { $couponExist = true; $lastScore = $score; } } } if ($couponExist) { $validCouponLocationCache[$cacheId] = $couponExist; return $couponExist; } } // State & Country restriction check $query = " SELECT c.couponid FROM [|PREFIX|]coupon_locations cl INNER JOIN [|PREFIX|]coupons c ON (c.couponid=cl.coupon_id) WHERE c.couponenabled='1' AND cl.selected_type='state' AND cl.country_id='".(int)$address['shipcountryid']."' AND cl.coupon_id='".(int)$couponId."' AND (cl.value_id='".(int)$address['shipstateid']."' OR cl.value_id='0') ORDER BY cl.value_id DESC LIMIT 1 "; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); $coupon = $GLOBALS['ISC_CLASS_DB']->Fetch($result); if(isset($coupon['couponid'])) { $validCouponLocationCache[$cacheId] = true; return true; } // Country level restriction check $query = " SELECT c.couponid FROM [|PREFIX|]coupon_locations cl INNER JOIN [|PREFIX|]coupons c ON (c.couponid=cl.coupon_id) WHERE c.couponenabled='1' AND cl.selected_type='country' AND cl.value_id='".(int)$address['shipcountryid']."' AND cl.coupon_id='".(int)$couponId."' LIMIT 1 "; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); $coupon = $GLOBALS['ISC_CLASS_DB']->Fetch($result); if(isset($coupon['couponid'])) { $validCouponLocationCache[$cacheId] = true; return true; } } $validCouponLocationCache[$cacheId] = false; return false; }