users) > 0) { if (!isset($user_ID) || $user_ID == null) { return false; } $sql = $wpdb->prepare("select user_nicename from $wpdb->users where ID = %d", $user_ID); $row = $wpdb->get_row($sql); if (!in_array($row->user_nicename, $promo->users)) { return false; } } $rtn = apply_filters('yak-allowed-promo', $promo); return $rtn != null; } } if (!function_exists('yak_calc_discount_price')) { /** * Calculate a price discount, or return 0 if not a pricing promotion */ function yak_calc_price_discount($product_id, $item_quantity, $price, $total_items, $total_price, $promo=null) { if ($promo != null && ($promo->products == null || count($promo->products) == 0 || in_array($product_id, $promo->products))) { if (yak_str_contains($promo->promo_type, 'pricing_perc')) { return ($promo->value / 100.0) * $price; } else if (yak_str_contains($promo->promo_type, 'pricing_val')) { if ($total_price > 0) { $item_perc_of_total = $price / $total_price; return $promo->value * $item_perc_of_total; } } } return 0; } } if (!function_exists('yak_calc_discount_shipping')) { /** * Calculate a shipping discount, or return 0 if not a shipping discount */ function yak_calc_shipping_discount($shipping_cost, $promo=null, $items) { if ($promo != null && ($promo->products == null || yak_contains_product($items, $promo->products))) { if (yak_str_contains($promo->promo_type, 'shipping_perc')) { return ($promo->value / 100.0) * $shipping_cost; } else if (yak_str_contains($promo->promo_type, 'shipping_val')) { return min($promo->value, $shipping_cost); } } return 0; } } if (!function_exists('yak_get_promotion')) { function yak_get_promotion($code) { // find by code $promos = yak_get_promotions($code, true); if (sizeof($promos) >= 1) { return $promos[0]; } // find by coupon $set = yak_get_coupon_by_code($code); if ($coupon_set != null && $set->used_datetime == null) { $promos = yak_get_promotions($set->coupon_set, true); if (sizeof($promos) >= 1) { return $promos[0]; } } // no promo found return new YakPromotion('', null, null, null, 0, '', ''); } } if (!function_exists('yak_get_promotion_by_threshold')) { function yak_get_promotion_by_threshold($price_threshold, $qty_threshold = null) { $promos = yak_get_promotions(null, true, $price_threshold, null); if (sizeof($promos) >= 1) { return $promos[0]; } if ($qty_threshold != null) { $promos = yak_get_promotions(null, true, null, $qty_threshold); } if (sizeof($promos) >= 1) { return $promos[0]; } else { return null; } } } if (!function_exists('yak_get_promotions')) { function yak_get_promotions($code = null, $valid = false, $price_threshold = null, $qty_threshold = null, $types = null) { global $wpdb, $promo_table, $promo_users_table, $coupon_table; $args = array(); $sql = "select promo_id, code, promo_type, description, threshold, value, expiry_date, products from $promo_table where 1 = 1 "; // search by code if (!empty($code)) { $sql .= "and ((code = %s "; $sql .= "and promo_type in ('shipping_perc', 'shipping_val', 'pricing_perc', 'pricing_val')) "; $args[] = $code; $sql .= "or code in (select coupon_set from $coupon_table where coupon_code = %s and used_datetime is null)) "; $args[] = $code; } // or search by price threshold else if (!empty($price_threshold)) { $sql .= "and (threshold is not null and %f >= threshold "; $sql .= "and promo_type in ('shipping_perc_threshold', 'shipping_val_threshold', 'pricing_perc_threshold', 'pricing_val_threshold')) "; $args[] = $price_threshold; } // or search by qty threshold else if (!empty($qty_threshold)) { $sql .= "and (threshold is not null and %d >= threshold "; $sql .= "and promo_type in ('pricing_perc_qty_threshold', 'pricing_val_qty_threshold', 'shipping_perc_qty_threshold', 'shipping_val_qty_threshold')) "; $args[] = $qty_threshold; } // check for validity if ($valid) { $sql .= "and (expiry_date is null or expiry_date >= current_date) "; } if ($types != null) { $sql .= "and promo_type in ("; $count = count($types); for ($i = 0; $i < $count; $i++) { $sql .= '"' . $types[$i] . '"'; if ($i < $count - 1) { $sql .= ','; } } $sql .= ") "; } // final sql if (!empty($price_threshold) || !empty($qty_threshold)) { $sql .= "order by threshold desc limit 1"; } else { $sql .= "order by promo_id asc"; } $sql = $wpdb->prepare($sql, $args); $results = $wpdb->get_results($sql); $promos = array(); foreach ($results as $result) { $sql = $wpdb->prepare("select user_nicename from $wpdb->users u where exists (select 1 from $promo_users_table pu where pu.promo_id = %d and pu.user_id = u.ID)", $result->promo_id); $rows = $wpdb->get_results($sql); $users = array(); foreach ($rows as $row) { $users[] = $row->user_nicename; } if (!empty($result->products)) { $products = explode(',', $result->products); } else { $products = null; } $promo = new YakPromotion($result->promo_id, $result->code, $result->promo_type, $result->threshold, $result->value, $result->description, $result->expiry_date, $users, $products); $promos[] = $promo; } return $promos; } } ?>