/**
  * Hooked to the users table to display a check mark if a given user has an active subscription.
  *
  * @param string $value The string to output in the column specified with $column_name
  * @param string $column_name The string key for the current column in an admin table
  * @param int $user_id The ID of the user to which this row relates
  * @return string $value A check mark if the column is the active_subscriber column and the user has an active subscription.
  * @since 1.0
  */
 public static function user_column_values($value, $column_name, $user_id)
 {
     if ('woocommerce_active_subscriber' == $column_name) {
         if (wcs_user_has_subscription($user_id, '', 'active')) {
             $value = '<div class="active-subscriber"></div>';
         } else {
             $value = '<div class="inactive-subscriber">-</div>';
         }
     }
     return $value;
 }
Ejemplo n.º 2
0
	<link itemprop="availability" href="http://schema.org/OutOfStock">
<?php 
} else {
    ?>

	<link itemprop="availability" href="http://schema.org/InStock">

	<?php 
    do_action('woocommerce_before_add_to_cart_form');
    ?>

	<?php 
    if (!$product->is_purchasable() && 0 != $user_id && 'no' != $product->limit_subscriptions && ('active' == $product->limit_subscriptions && wcs_user_has_subscription($user_id, $product->id, 'on-hold') || ($user_has_subscription = wcs_user_has_subscription($user_id, $product->id, $product->limit_subscriptions)))) {
        ?>
		<?php 
        if ('any' == $product->limit_subscriptions && $user_has_subscription && !wcs_user_has_subscription($user_id, $product->id, 'active') && !wcs_user_has_subscription($user_id, $product->id, 'on-hold')) {
            // customer has an inactive subscription, maybe offer the renewal button
            ?>
			<?php 
            $resubscribe_link = wcs_get_users_resubscribe_link_for_product($product->id);
            ?>
			<?php 
            if (!empty($resubscribe_link)) {
                ?>
				<a href="<?php 
                echo esc_url($resubscribe_link);
                ?>
" class="button product-resubscribe-link"><?php 
                esc_html_e('Resubscribe', 'woocommerce-subscriptions');
                ?>
</a>
 /**
  * If a product is being marked as not purchasable because it is limited and the customer has a subscription,
  * but the current request is to switch the subscription, then mark it as purchasable.
  *
  * @since 1.4.4
  * @return bool
  */
 public static function is_purchasable($is_purchasable, $product)
 {
     $product_key = !empty($product->variation_id) ? $product->variation_id : $product->id;
     if (!isset(self::$is_purchasable_cache[$product_key])) {
         if (false === $is_purchasable && wcs_is_product_switchable_type($product) && WC_Subscriptions_Product::is_subscription($product->id) && 'no' != $product->limit_subscriptions && is_user_logged_in() && wcs_user_has_subscription(0, $product->id, $product->limit_subscriptions)) {
             // Adding to cart from the product page
             if (isset($_GET['switch-subscription'])) {
                 $is_purchasable = true;
                 // Validating when restring cart from session
             } elseif (self::cart_contains_switches()) {
                 $is_purchasable = true;
                 // Restoring cart from session, so need to check the cart in the session (self::cart_contains_subscription_switch() only checks the cart)
             } elseif (isset(WC()->session->cart)) {
                 foreach (WC()->session->cart as $cart_item_key => $cart_item) {
                     if ($product->id == $cart_item['product_id'] && isset($cart_item['subscription_switch'])) {
                         $is_purchasable = true;
                         break;
                     }
                 }
             }
         }
         self::$is_purchasable_cache[$product_key] = $is_purchasable;
     }
     return self::$is_purchasable_cache[$product_key];
 }
Ejemplo n.º 4
0
/**
 * Give a user the Subscription's default subscriber's inactive role if they do not have an active subscription
 *
 * @since 2.0
 */
function wcs_maybe_make_user_inactive($user_id)
{
    if (!wcs_user_has_subscription($user_id, '', 'active')) {
        wcs_update_users_role($user_id, 'default_inactive_role');
    }
}
 /**
  * If a product is being marked as not purchasable because it is limited and the customer has a subscription,
  * but the current request is to resubscribe to the subscription, then mark it as purchasable.
  *
  * @since 2.0
  * @return bool
  */
 public static function is_purchasable($is_purchasable, $product)
 {
     global $wp;
     if (!isset(self::$is_purchasable_cache[$product->id])) {
         self::$is_purchasable_cache[$product->id] = $is_purchasable;
         if (self::is_subscription($product->id) && 'no' != $product->limit_subscriptions && !wcs_is_order_received_page() && !wcs_is_paypal_api_page()) {
             if (('active' == $product->limit_subscriptions && wcs_user_has_subscription(0, $product->id, 'on-hold') || wcs_user_has_subscription(0, $product->id, $product->limit_subscriptions)) && !self::order_awaiting_payment_for_product($product->id)) {
                 self::$is_purchasable_cache[$product->id] = false;
             }
         }
     }
     return self::$is_purchasable_cache[$product->id];
 }
/**
 * Check if a user can resubscribe to an expired or cancelled subscription by creating a
 * new subscription with the same terms.
 *
 * For it to be possible to resubscribe to a subscription, the user specified with $user_id must
 * and the subscription must:
 * 1. be be inactive (expired or cancelled)
 * 2. had at least one payment, to avoid circumventing sign-up fees
 * 3. its parent order must not have already been superseded by a new order (to prevent
 *    displaying "Resubscribe" links on subscriptions that have already been renewed)
 * 4. the products to which the subscription relates must not have been deleted
 * 5. have a recurring amount greater than $0, to avoid allowing resubscribes to subscriptions
 *    where the entire cost is charged in a sign-up fee
 *
 * @param  int | WC_Subscription $subscription Post ID of a 'shop_subscription' post, or instance of a WC_Subscription object
 * @param  int The ID of a user
 * @return bool
 * @since  2.0
 */
function wcs_can_user_resubscribe_to($subscription, $user_id = '')
{
    if (!is_object($subscription)) {
        $subscription = wcs_get_subscription($subscription);
    }
    if (empty($user_id)) {
        $user_id = get_current_user_id();
    }
    if (empty($subscription)) {
        $can_user_resubscribe = false;
    } elseif (!user_can($user_id, 'subscribe_again', $subscription->id)) {
        $can_user_resubscribe = false;
    } elseif (!$subscription->has_status(array('cancelled', 'expired', 'trash'))) {
        $can_user_resubscribe = false;
    } elseif ($subscription->get_total() <= 0) {
        $can_user_resubscribe = false;
    } else {
        $resubscribe_orders = get_posts(array('meta_query' => array(array('key' => '_subscription_resubscribe', 'compare' => '=', 'value' => $subscription->id, 'type' => 'numeric')), 'post_type' => 'shop_order', 'post_status' => 'any'));
        // Make sure all line items still exist
        $all_line_items_exist = true;
        // Check if product in subscription is limited
        $has_active_limited_subscription = false;
        foreach ($subscription->get_items() as $line_item) {
            $product = !empty($line_item['variation_id']) ? wc_get_product($line_item['variation_id']) : wc_get_product($line_item['product_id']);
            if (false === $product) {
                $all_line_items_exist = false;
                break;
            }
            if ('active' == $product->limit_subscriptions && (wcs_user_has_subscription($user_id, $product->id, 'on-hold') || wcs_user_has_subscription($user_id, $product->id, 'active'))) {
                $has_active_limited_subscription = true;
                break;
            }
        }
        if (empty($resubscribe_orders) && $subscription->get_completed_payment_count() > 0 && true === $all_line_items_exist && false === $has_active_limited_subscription) {
            $can_user_resubscribe = true;
        } else {
            $can_user_resubscribe = false;
        }
    }
    return apply_filters('wcs_can_user_resubscribe_to_subscription', $can_user_resubscribe, $subscription, $user_id);
}
 /**
  * Update user informations from My Account form
  */
 public function maybe_update_user_informations($user_id, $address_type)
 {
     if (!wcs_user_has_subscription($user_id) || wc_notice_count('error') > 0 || empty($_POST['_wcsnonce']) || !wp_verify_nonce($_POST['_wcsnonce'], 'wcs_edit_address')) {
         return;
     }
     if ('billing' !== $address_type) {
         return;
     }
     $user_code = get_user_meta($user_id, 'vindi_user_code', true);
     $address_fields = WC()->countries->get_address_fields(esc_attr($_POST[$address_type . '_country']), $address_type . '_');
     $address = array();
     foreach ($address_fields as $key => $field) {
         if (isset($_POST[$key])) {
             $address[str_replace($address_type . '_', '', $key)] = woocommerce_clean($_POST[$key]);
         }
     }
     if (!$this->settings->api->update_user_billing_informations($user_code, $address)) {
         return;
     }
 }
 /**
  * If a product is being marked as not purchasable because it is limited and the customer has a subscription,
  * but the current request is to resubscribe to the subscription, then mark it as purchasable.
  *
  * @since 2.0
  * @return bool
  */
 public static function is_purchasable($is_purchasable, $product)
 {
     global $wp;
     if (self::is_subscription($product->id) && 'no' != $product->limit_subscriptions && is_user_logged_in() && ('active' == $product->limit_subscriptions && wcs_user_has_subscription(0, $product->id, 'on-hold') || wcs_user_has_subscription(0, $product->id, $product->limit_subscriptions)) && false === strpos($_SERVER['REQUEST_URI'], 'order-received') && false === strpos($_SERVER['REQUEST_URI'], 'wc-api/wcs_paypal')) {
         // we can't use is_order_received_page() becuase get_cart_from_session() is called before the query vars are setup
         $is_purchasable = false;
         if (!empty(WC()->session->order_awaiting_payment) || isset($_GET['pay_for_order'])) {
             $order_id = !empty(WC()->session->order_awaiting_payment) ? WC()->session->order_awaiting_payment : $wp->query_vars['order-pay'];
             $order = wc_get_order(absint($order_id));
             if (is_object($order) && $order->has_status(array('pending', 'failed'))) {
                 foreach ($order->get_items() as $item) {
                     if ($item['product_id'] == $product->id || $item['variation_id'] == $product->id) {
                         $subscriptions = wcs_get_subscriptions(array('order_id' => $order->id, 'product_id' => $product->id));
                         if (!empty($subscriptions)) {
                             $subscription = array_pop($subscriptions);
                             if ($subscription->has_status(array('pending', 'on-hold'))) {
                                 $is_purchasable = true;
                             }
                         }
                         break;
                     }
                 }
             }
         }
     }
     return $is_purchasable;
 }
 /**
  * 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 int $user_id The ID of a user who own's the subscription (and address)
  * @since 1.3
  */
 public static function maybe_update_subscription_addresses($user_id, $address_type)
 {
     if (!wcs_user_has_subscription($user_id) || wc_notice_count('error') > 0 || empty($_POST['_wcsnonce']) || !wp_verify_nonce($_POST['_wcsnonce'], 'wcs_edit_address')) {
         return;
     }
     $address_type = 'billing' == $address_type || 'shipping' == $address_type ? $address_type : '';
     $address_fields = WC()->countries->get_address_fields(esc_attr($_POST[$address_type . '_country']), $address_type . '_');
     $address = array();
     foreach ($address_fields as $key => $field) {
         if (isset($_POST[$key])) {
             $address[str_replace($address_type . '_', '', $key)] = woocommerce_clean($_POST[$key]);
         }
     }
     if (isset($_POST['update_all_subscriptions_addresses'])) {
         $users_subscriptions = wcs_get_users_subscriptions($user_id);
         foreach ($users_subscriptions as $subscription) {
             if ($subscription->has_status(array('active', 'on-hold'))) {
                 $subscription->set_address($address, $address_type);
             }
         }
     } elseif (isset($_POST['update_subscription_address'])) {
         $subscription = wcs_get_subscription(intval($_POST['update_subscription_address']));
         // Update the address only if the user actually owns the subscription
         if (!empty($subscription)) {
             $subscription->set_address($address, $address_type);
         }
         wp_safe_redirect($subscription->get_view_order_url());
         exit;
     }
 }
 /**
  * Check if a user has a subscription, optionally specified with $product_id.
  *
  * @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
  * @param product_id int (optional) The ID of a subscription product.
  * @param status string (optional) A subscription status to check against. For example, for a $status of 'active', a subscriber must have an active subscription for a return value of true.
  * @return bool True if the user has the subscription (or any subscription if no subscription specified), otherwise false.
  * @version 1.3.5
  */
 public static function user_has_subscription($user_id = 0, $product_id = '', $status = 'any')
 {
     _deprecated_function(__METHOD__, '2.0', 'wcs_user_has_subscription()');
     return wcs_user_has_subscription($user_id, $product_id, $status);
 }
Ejemplo n.º 11
-1
 /**
  * Compare the user's subscriptions end date with the date
  * the user was added to the course. If the user was added after
  * the subscription ended they were manually added and this will return
  * true.
  *
  * Important to note that all subscriptions for the user is compared.
  *
  * @since 1.9.0
  *
  * @param $user_id
  * @param $product_id
  * @param $course_id
  *
  * @return bool
  */
 public static function was_user_added_without_subscription($user_id, $product_id, $course_id)
 {
     $course_start_date = '';
     $subscription_start_date = '';
     $is_a_subscription = '';
     $was_user_added_without_subscription = true;
     // if user is not on the course they were not added
     if (!Sensei_Utils::user_started_course($course_id, $user_id)) {
         return false;
     }
     // if user doesn't have a subscription and is taking the course
     // they were added manually
     if (!wcs_user_has_subscription($user_id, $product_id) && Sensei_Utils::user_started_course($course_id, get_current_user_id())) {
         return true;
     }
     $course_status = Sensei_Utils::user_course_status($course_id, $user_id);
     // comparing dates setup data
     $course_start_date = date_create($course_status->comment_date);
     $subscriptions = wcs_get_users_subscriptions($user_id);
     // comparing every subscription
     foreach ($subscriptions as $subscription) {
         // for the following statuses we know the user was not added
         // manually
         $status = $subscription->get_status();
         if (in_array($status, array('pending-canceled', 'active', 'on-hold', 'pending'))) {
             continue;
         }
         $current_subscription_start_date = date_create($subscription->modified_date);
         // is the last updated subscription date newer than course start date
         if ($current_subscription_start_date > $course_start_date) {
             return false;
         }
     }
     return $was_user_added_without_subscription;
 }
 /**
  * If a product is being marked as not purchasable because it is limited and the customer has a subscription,
  * but the current request is to resubscribe to the subscription, then mark it as purchasable.
  *
  * @since 2.0
  * @return bool
  */
 public static function is_purchasable($is_purchasable, $product)
 {
     if (self::is_subscription($product->id) && 'no' != $product->limit_subscriptions && is_user_logged_in() && ('active' == $product->limit_subscriptions && wcs_user_has_subscription(0, $product->id, 'on-hold') || wcs_user_has_subscription(0, $product->id, $product->limit_subscriptions)) && false === strpos($_SERVER['REQUEST_URI'], 'order-received')) {
         // we can't use is_order_received_page() becuase get_cart_from_session() is called before the query vars are setup
         $is_purchasable = false;
     }
     return $is_purchasable;
 }