static function get_coupon_value($coupon, $total = false, $cart_items = array(), $customer_email = false, $check_in = null) { if (!$total) { $total = STCart::get_total_with_out_tax(); } if (empty($cart_items)) { $cart_items = STCart::get_items(); } $coupon_object = get_page_by_title($coupon, OBJECT, 'st_coupon_code'); if (!$coupon_object) { return array('status' => 0, 'message' => __('Coupon Not Found', ST_TEXTDOMAIN)); } $discount_value = 0; $coupon_post_id = $coupon_object->ID; $discount_type = get_post_meta($coupon_post_id, 'discount_type', true); //$booking_type_available = get_post_meta($coupon_post_id,'booking_type_available',true); $amount = get_post_meta($coupon_post_id, 'amount', true); $expiry_date = get_post_meta($coupon_post_id, 'expiry_date', true); $minimum_spend = get_post_meta($coupon_post_id, 'minimum_spend', true); $maximum_spend = get_post_meta($coupon_post_id, 'maximum_spend', true); $required_porduct = get_post_meta($coupon_post_id, 'include_products', true); $exclude_products = get_post_meta($coupon_post_id, 'exclude_products', true); $limit_per_coupon = get_post_meta($coupon_post_id, 'limit_per_coupon', true); $limit_per_user = get_post_meta($coupon_post_id, 'limit_per_user', true); //Validate Expiry Date if ($expiry_date) { $datediff = STDate::date_diff2($expiry_date, date('Y-m-d')); if ($datediff <= 1) { return array('status' => 0, 'message' => __('This coupon is expired', ST_TEXTDOMAIN)); } } //Validate Minium Spend if ($minimum_spend) { if ($minimum_spend > $total) { return array('status' => 0, 'message' => sprintf(__('This coupon is only applicable for bills spent over %s', ST_TEXTDOMAIN), TravelHelper::format_money($minimum_spend))); } } //Validate Maximum Spend if ($maximum_spend) { if ($maximum_spend < $total) { return array('status' => 0, 'message' => sprintf(__('This coupon is only applicable for bills spent less than %s', ST_TEXTDOMAIN), TravelHelper::format_money($maximum_spend))); } } //Validate Required Product if ($required_porduct) { $ids = explode(',', $required_porduct); $products_name = array(); $check = array(); foreach ($ids as $key) { if (self::_check_in_items($key, $cart_items)) { $check[] = 1; } $products_name[] = "<a class='dotted_bottom' href='" . get_permalink($key) . "' target='_blank'>" . get_the_title($key) . "</a>"; } if (empty($check)) { return array('status' => 0, 'message' => sprintf(__('This coupon is required %s in the cart', ST_TEXTDOMAIN), implode(', ', $products_name))); } } //Validate Exclude Product if ($exclude_products) { $ids = explode(',', $exclude_products); $products_name = array(); $check = array(); foreach ($ids as $key) { if (self::_check_in_items($key, $cart_items)) { $check[] = 1; } $products_name[] = "<a class='dotted_bottom' href='" . get_permalink($key) . "' target='_blank'>" . get_the_title($key) . "</a>"; } if (!empty($check)) { return array('status' => 0, 'message' => sprintf(__('This coupon is required %s NOT in the cart', ST_TEXTDOMAIN), implode(', ', $products_name))); } } //Validate Used Time $coupon_code = $coupon_object->post_title; if ($limit_per_coupon) { global $wpdb; $query = "\r\n SELECT count({$wpdb->prefix}posts.ID) as total\r\n FROM {$wpdb->prefix}posts\r\n join {$wpdb->prefix}postmeta on {$wpdb->prefix}postmeta.post_id = {$wpdb->prefix}posts.ID and {$wpdb->prefix}postmeta.meta_key = 'coupon_code' \r\n and {$wpdb->prefix}postmeta.meta_value = '{$coupon_code}'\r\n WHERE post_type = 'st_order'"; $result = $wpdb->get_results($query); $count_used = 0; if (!empty($result) and is_array($result)) { foreach ($result as $key => $value) { $count_used += $value->total; } } if ($count_used >= $limit_per_coupon) { return array('status' => 0, 'message' => __('This coupon is reach the limit of usage', ST_TEXTDOMAIN)); } } // Validate Per User if ($limit_per_user and is_user_logged_in()) { /*$q_arg=array( 'post_type'=>'st_order' ); if(is_user_logged_in()){ $q_arg['meta_key']='id_user'; $q_arg['meta_value']=get_current_user_id(); }elseif($customer_email){ $q_arg['meta_key']='st_email'; $q_arg['meta_value']=$customer_email; }*/ $q_arg = array('post_type' => 'st_order', 'meta_query' => array('relation' => 'AND', array('key' => 'coupon_code', 'value' => $coupon_code, 'type' => 'CHAR', 'compare' => '='), array('key' => 'id_user', 'value' => get_current_user_id(), 'compare' => '='))); $query = new WP_Query($q_arg); if ($query->found_posts >= $limit_per_user) { return array('status' => 0, 'message' => __('This coupon is reach the limit of usage per user', ST_TEXTDOMAIN)); } } if ($amount) { switch ($discount_type) { case "cart_percent": if ($amount < 100) { $discount_value = $total / 100 * $amount; } else { $discount_value = $total; } break; // Currently, Only one product in cart, so dont need to use bellow discount type // case "product_amount": // $discount_value=self::_get_product_discount($amount,explode(',',$required_porduct),$cart_items,'amount'); // break; // case "product_percent": // $discount_value=self::_get_product_discount($amount,explode(',',$required_porduct),$cart_items,'percent'); // break; // Default: Card Discount // Currently, Only one product in cart, so dont need to use bellow discount type // case "product_amount": // $discount_value=self::_get_product_discount($amount,explode(',',$required_porduct),$cart_items,'amount'); // break; // case "product_percent": // $discount_value=self::_get_product_discount($amount,explode(',',$required_porduct),$cart_items,'percent'); // break; // Default: Card Discount default: $discount_value = $amount > $total ? $total : $amount; break; } } $result = apply_filters('st_get_coupon_value', array('status' => 1, 'value' => $discount_value, 'coupon' => $coupon, 'total' => $total, 'cart_items' => $cart_items)); return array('status' => $result['status'], 'value' => isset($result['value']) ? $result['value'] : 0, 'message' => isset($result['message']) ? $result['message'] : 0, 'data' => isset($result['data']) ? $result['data'] : 0); }