function create_subscription($sub_id, $gateway = 'admin') { if (!$this->active_member()) { $this->toggle_activation(); } $subscription = new M_Subscription($sub_id); $levels = $subscription->get_levels(); if (function_exists('is_multisite') && is_multisite()) { global $blog_id; } if (!empty($levels)) { foreach ($levels as $key => $level) { if ($level->level_order == 1) { $this->add_subscription($sub_id, $level->level_id, $level->level_order, $gateway); // Check for a coupon transient if (function_exists('is_multisite') && is_multisite()) { $trans = get_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id); } else { $trans = get_transient('m_coupon_' . $this->ID . '_' . $sub_id); } // If there is a coupon transient do our coupon count magic if ($trans && is_array($trans)) { $code = strtoupper($code); $coupon = new M_Coupon($trans['code']); $price = $coupon->apply_price($level->level_price); $coupon_data = $coupon->get_coupon(true); $coupon_data['ID'] = $coupon_data['id']; unset($coupon_data['id']); // Ok they for sure used the coupon so lets delete the transient if (function_exists('is_multisite') && is_multisite()) { delete_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id); } else { delete_transient('m_coupon_' . $this->ID . '_' . $sub_id); } // Now lets just add a use to the coupon it self and daz it... $new_total = (int) $coupon_data['coupon_used'] + 1; $coupon_data['coupon_used'] = $new_total; $coupon->increment_coupon_used(); } break; } } return true; } else { return false; } }
function apply_coupon_pricing($coupon_code = false, $pricing = false) { if ($coupon_code === false || $pricing === false) { return false; } $coupon_code = strtoupper($coupon_code); $coupon = new M_Coupon($coupon_code); $coupon_data = $coupon->get_coupon(true); if ((int) $coupon_data['coupon_used'] >= (int) $coupon_data['coupon_uses'] || strtotime($coupon_data['coupon_enddate']) < time()) { $this->coupon_label = __('The coupon you supplied is either invalid or expired.', 'membership'); return $pricing; } // We should always have a user_id at this point so we are going to // create a transient to help us log when a coupon is used. $user = wp_get_current_user(); $trans = array('code' => $coupon_code, 'user_id' => $user->ID, 'sub_id' => $this->id, 'prices_w_coupon' => array()); foreach ($pricing as $key => $value) { // For every possible price they could have paid we put the total into the transient to check if the coupon was set and never used $pricing[$key]['amount'] = $coupon->apply_price($value['amount']); $trans['prices_w_coupon'][$key] = $coupon->apply_price($value['amount']); $this->coupon_label = $coupon->coupon_label; } if (function_exists('is_multisite') && is_multisite()) { global $blog_id; } // Check if a transient already exists and delete it if it does if (function_exists('is_multisite') && is_multisite()) { if (get_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id)) { delete_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id); } } else { if (get_transient('m_coupon_' . $user->ID . '_' . $this->id)) { delete_transient('m_coupon_' . $user->ID . '_' . $this->id); } } // Create transient for 1 hour. This means the user has 1 hour to redeem the coupon after its been applied before it goes back into the pool. // If you want to use a different time limit use the filter below $time = apply_filters('membership_apply_coupon_redemption_time', 60 * 60); if (function_exists('is_multisite') && is_multisite()) { set_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id, $trans, $time); } else { set_transient('m_coupon_' . $user->ID . '_' . $this->id, $trans, $time); } return apply_filters('membership_apply_coupon_pricingarray', $pricing, $coupon_code); }