/** * Customise which actions are shown against a subscriptions order on the My Account page. * * @since 1.3 */ public static function filter_woocommerce_my_account_my_orders_actions($actions, $order) { if (WC_Subscriptions_Order::order_contains_subscription($order) || WC_Subscriptions_Renewal_Order::is_renewal($order)) { unset($actions['cancel']); if (is_numeric(get_post_meta($order->id, '_failed_order_replaced_by', true))) { unset($actions['pay']); } $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order); $order_items = WC_Subscriptions_Order::get_recurring_items($original_order); $first_order_item = reset($order_items); $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item); $subscription_key = WC_Subscriptions_Manager::get_subscription_key($original_order->id, $product_id); $subscription = WC_Subscriptions_Manager::get_users_subscription($original_order->customer_user, $subscription_key); if (empty($subscription) || !in_array($subscription['status'], array('on-hold', 'pending'))) { unset($actions['pay']); } } return $actions; }
/** * When an order is added or updated from the admin interface, check if a new subscription product * has been manually added to the order, and if one has, create a new subscription. * * @param $post_id int The ID of the post which is the WC_Order object. * @param $post Object The post object of the order. * @since 1.1 */ public static function maybe_manually_change_subscriptions($post_id, $post) { $order = new WC_Order($post_id); // Check if all the subscription products on the order have associated subscriptions on the user's account, and if not, add a new one foreach ($_POST['item_id'] as $item_id) { if (!WC_Subscriptions_Product::is_subscription($item_id)) { continue; } $subscription_key = WC_Subscriptions_Manager::get_subscription_key($post_id, $item_id); $subscription = array(); // If order customer changed, move the subscription from the old customer's account to the new customer if (!empty($order->customer_user) && $order->customer_user != (int) $_POST['customer_user']) { $subscription = WC_Subscriptions_Manager::remove_users_subscription($order->customer_user, $subscription_key); $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions((int) $_POST['customer_user']); if (!empty($subscription)) { $subscriptions[$subscription_key] = $subscription; WC_Subscriptions_Manager::update_users_subscriptions((int) $_POST['customer_user'], $subscriptions); } } // In case it's a new order or the customer has changed $order->customer_user = $order->user_id = (int) $_POST['customer_user']; $subscription = WC_Subscriptions_Manager::get_users_subscription($order->customer_user, $subscription_key); if (empty($subscription)) { // Add a new subscription // The order doesn't may not exist yet, so we need to set a few things ourselves $order->order_key = uniqid('order_'); add_post_meta($post_id, '_order_key', $order->order_key, true); WC_Subscriptions_Manager::create_pending_subscription_for_order($order, $item_id); // Add the subscription meta for this item to the order $functions_and_meta = array('get_period' => '_order_subscription_periods', 'get_interval' => '_order_subscription_intervals', 'get_length' => '_order_subscription_lengths'); foreach ($functions_and_meta as $function_name => $meta_key) { $subscription_meta = self::get_meta($order, $meta_key, array()); $subscription_meta[$item_id] = WC_Subscriptions_Product::$function_name($item_id); update_post_meta($order->id, $meta_key, $subscription_meta); } // Set the subscription's status if it should be something other than pending switch ($order->status) { case 'completed': case 'processing': WC_Subscriptions_Manager::activate_subscription($order->customer_user, $subscription_key); break; case 'refunded': case 'cancelled': WC_Subscriptions_Manager::cancel_subscription($order->customer_user, $subscription_key); break; case 'failed': WC_Subscriptions_Manager::failed_subscription_signup($order->customer_user, $subscription_key); break; } } } }
/** * Runs when an subscription is re-activated after suspension. * @since 1.3.3 * @access public * @param integer $user_id User ID * @param integer $subscription_key Subscription Unique Key * @return void */ public function sensei_woocommerce_reactivate_subscription($user_id, $subscription_key) { $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key); $order = new WC_Order($subscription['order_id']); $user = get_user_by('id', $order->user_id); $order_user = array(); $order_user['ID'] = $user->ID; $order_user['user_login'] = $user->user_login; $order_user['user_email'] = $user->user_email; $order_user['user_url'] = $user->user_url; $courses = $this->post_types->course->get_product_courses($subscription['product_id']); foreach ($courses as $course_item) { $update_course = $this->woocommerce_course_update($course_item->ID, $order_user); } // End For Loop }
function pmprowoo_cancelled_subscription($user_id, $subscription_key) { global $pmprowoo_product_levels; //don't bother if array is empty if (empty($pmprowoo_product_levels)) { return; } /* does this order contain a membership product? */ $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key); if (isset($subscription['product_id']) && isset($subscription['order_id'])) { $product_id = $subscription['product_id']; $order_id = $subscription['order_id']; //membership product ids $product_ids = array_keys($pmprowoo_product_levels); //get order $order = new WC_Order($order_id); //does the order have a user id and some products? if (!empty($order->customer_user) && !empty($product_id)) { //is there a membership level for this product? if (in_array($product_id, $product_ids)) { //add the user to the level pmpro_changeMembershipLevel(0, $order->customer_user); } } } }
/** * Checks if the current request is by a user to change the status of their subscription, and if it is * validate the subscription cancellation request and maybe processes the cancellation. * * @since 1.2 */ public static function maybe_create_renewal_order_for_user() { global $woocommerce; if (isset($_GET['renew_subscription']) && isset($_GET['_wpnonce'])) { $user_id = get_current_user_id(); $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $_GET['renew_subscription']); $redirect_to = get_permalink(woocommerce_get_page_id('myaccount')); if (wp_verify_nonce($_GET['_wpnonce'], __FILE__) === false) { $woocommerce->add_error(__('There was an error with the renewal request. Please try again.', WC_Subscriptions::$text_domain)); } elseif (empty($subscription)) { $woocommerce->add_error(__('That doesn\'t appear to be one of your subscriptions.', WC_Subscriptions::$text_domain)); } elseif (!self::can_subscription_be_renewed($_GET['renew_subscription'], $user_id)) { $woocommerce->add_error(__('That subscription can not be renewed. Please contact us if you need assistance.', WC_Subscriptions::$text_domain)); } else { $order = new WC_Order($subscription['order_id']); // Don't email requesting payment when the customer requested the renewal remove_action('woocommerce_subscriptions_renewal_order_created', __CLASS__ . '::maybe_send_customer_renewal_order_email', 10, 1); $renewal_order_id = self::generate_renewal_order($order, $subscription['product_id']); $renewal_order = new WC_Order($renewal_order_id); $order->add_order_note(sprintf(__('Subscriber generated renewal order %s', WC_Subscriptions::$text_domain), $renewal_order->get_order_number())); $woocommerce->add_message(__('Renew your subscription.', WC_Subscriptions::$text_domain)); $redirect_to = $renewal_order->get_checkout_payment_url(); } wp_safe_redirect($redirect_to); exit; } }
/** * Fire a gateway specific hook when a subscription expires. * * @since 1.0 */ public static function trigger_gateway_subscription_expired_hook($user_id, $subscription_key) { $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key); $order = new WC_Order($subscription['order_id']); do_action('subscription_expired_' . $order->payment_method, $order, $subscription['product_id']); }
/** * Trigger a hook when a subscription suspended due to a failed renewal payment is reactivated * * @since 1.3 */ public static function trigger_processed_failed_renewal_order_payment_hook($user_id, $subscription_key) { $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key); $original_order = new WC_Order($subscription['order_id']); do_action('woocommerce_subscriptions_processed_failed_renewal_order_payment', $subscription_key, $original_order); }
/** * Checks if an order contains an in active subscription and if it does, denies download acces * to files purchased on the order. * * @return bool False if the order contains a subscription that has expired or is cancelled/on-hold, otherwise, the original value of $download_permitted * @since 1.3 */ public static function is_download_permitted($download_permitted, $order) { if (self::order_contains_subscription($order)) { foreach ($order->get_items() as $order_item) { $subscription_key = WC_Subscriptions_Manager::get_subscription_key($order->id, self::get_items_product_id($order_item)); $subscription = WC_Subscriptions_Manager::get_users_subscription($order->customer_user, $subscription_key); if (!isset($subscription['status']) || 'active' !== $subscription['status']) { $download_permitted = false; break; } } } return $download_permitted; }
/** * Same as when a subscription is cancelled. * @param int $user_id * @param string $subscription_key */ public static function subscription_expired($user_id, $subscription_key) { $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key); if (isset($subscription['product_id']) && isset($subscription['order_id'])) { $product_id = $subscription['product_id']; $order_id = $subscription['order_id']; $groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true); if (isset($groups_product_groups[$order_id]) && isset($groups_product_groups[$order_id][$product_id]) && isset($groups_product_groups[$order_id][$product_id]['groups'])) { foreach ($groups_product_groups[$order_id][$product_id]['groups'] as $group_id) { Groups_User_Group::delete($user_id, $group_id); } } } }
/** * When a subscriber's billing or shipping address is successfully updated, check if the subscriber * has also requested to update the addresses on existing subscriptions and if so, go ahead and update * the addresses on the initial order for each subscription. * * @param $user_id int The ID of a user who own's the subscription (and address) * @since 1.3 */ public static function maybe_update_subscription_addresses($user_id) { global $woocommerce; if (!WC_Subscriptions_Manager::user_has_subscription($user_id) || !isset($_GET['address'])) { return; } $load_address = isset($_GET['address']) ? esc_attr($_GET['address']) : ''; $load_address = $load_address == 'billing' || $load_address == 'shipping' ? $load_address : ''; $address_fields = $woocommerce->countries->get_address_fields(esc_attr($_POST[$load_address . '_country']), $load_address . '_'); if (isset($_POST['update_all_subscriptions_addresses'])) { $users_subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id); foreach ($users_subscriptions as $subscription) { self::maybe_update_order_address($subscription, $address_fields); } } elseif (isset($_POST['update_subscription_address'])) { $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $_POST['update_subscription_address']); // Update the address only if the user actually owns the subscription if (!empty($subscription)) { self::maybe_update_order_address($subscription, $address_fields); } } }
public function skip_sending_if_status_changed($skip, $email, $email_order) { global $wpdb; if (isset($email_order->meta) && !empty($email_order->meta)) { $meta = maybe_unserialize($email_order->meta); if (isset($meta['subs_key'])) { $delete = false; $subscription = WC_Subscriptions_Manager::get_users_subscription($email_order->user_id, $meta['subs_key']); if ($subscription) { if (($email->interval_type == 'subs_suspended' || $email->interval_type == 'subs_expired') && $subscription['status'] != 'on-hold') { $delete = true; $skip = true; } elseif (($email->interval_type == 'subs_activated' || $email->interval_type == 'subs_renewed' || $email->interval_type == 'subs_reactivated') && $subscription['status'] != 'active') { $delete = true; $skip = true; } elseif ($email->interval_type == 'subs_cancelled' && $subscription['status'] != 'cancelled') { $delete = true; $skip = true; } if ($delete) { $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}followup_email_orders WHERE id = %d", $email_order->id)); } } // if ($subscription) } // if ( isset($meta['subs_key']) ) } // if ( isset($email_order->meta) && !empty($email_order->meta) ) return $skip; }