/** * Apply coupon */ public function executeApplyCoupon() { $form_id = $this->getParameter('form_id'); $ab_coupon = $this->getParameter('ab_coupon'); $response = null; if (get_option('ab_settings_coupons') and $form_id) { $userData = new AB_UserBookingData($form_id); $userData->load(); if ($userData->hasData()) { $price = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT price FROM ab_staff_service WHERE staff_id = %d AND service_id = %d', $userData->getStaffId(), $userData->getServiceId())); if ($ab_coupon === '') { $userData->setCoupon(NULL); $response = array('status' => 'reset', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $discount = $this->getWpdb()->get_var($this->getWpdb()->prepare('SELECT `discount` FROM `ab_coupons` WHERE UPPER(`code`) = %s AND `used` = 0', strtoupper($ab_coupon))); if ($discount) { $userData->setCoupon($ab_coupon); $price -= $price * $discount / 100; $response = array('status' => 'success', 'text' => $this->_prepareInfoText(4, $userData, $price)); } else { $userData->setCoupon(NULL); $response = array('status' => 'error', 'error' => __('* This coupon code is invalid or has been used', 'ab'), 'text' => $this->_prepareInfoText(4, $userData, $price)); } } } } // Output JSON response. if ($response === null) { $response = array('status' => 'no-data'); } header('Content-Type: application/json'); echo json_encode($response); exit(0); }