예제 #1
0
/**
 * Checks if the user can be granted the permission to remove a line item from the subscription.
 *
 * @param WC_Subscription $subscription An instance of a WC_Subscription object
 * @since 2.0
 */
function wcs_can_items_be_removed($subscription)
{
    $allow_remove = false;
    if (sizeof($subscription->get_items()) > 1 && $subscription->payment_method_supports('subscription_amount_changes') && $subscription->has_status(array('active', 'on-hold', 'pending'))) {
        $allow_remove = true;
    }
    return apply_filters('wcs_can_items_be_removed', $allow_remove, $subscription);
}
 /**
  * If the subscription has expired since upgrading and the end date is not the original expiration date,
  * we need to unexpire it, which in the case of a previously active subscription means activate it, and
  * in any other case, leave it as on-hold (a cancelled subscription wouldn't have been expired, so the
  * status must be on-hold or active).
  *
  * @param  WC_Subscription $subscription data about the subscription
  * @return bool true if the trial date was repaired, otherwise false
  */
 protected static function maybe_repair_status($subscription, $former_order_item_meta, $dates_to_update)
 {
     if ($subscription->has_status('expired') && 'expired' != $former_order_item_meta['_wcs_migrated_subscription_status'][0] && isset($dates_to_update['end'])) {
         try {
             // we need to bypass the update_status() method here because normally an expired subscription can't have it's status changed, we also don't want normal status change hooks to be fired
             wp_update_post(array('ID' => $subscription->id, 'post_status' => 'wc-on-hold'));
             // if the payment method doesn't support date changes, we still want to reactivate the subscription but we also need to process a special failed payment at the next renewal to fix up the payment method so we'll set a special flag in post meta to handle that
             if (!$subscription->payment_method_supports('subscription_date_changes') && $subscription->get_total() > 0) {
                 update_post_meta($subscription->id, '_wcs_repaired_2_0_2_needs_failed_payment', 'true');
                 WCS_Upgrade_Logger::add(sprintf('For subscription %d: payment method does not support "subscription_date_changes" and total > 0, setting "_wcs_repaired_2_0_2_needs_failed_payment" post meta flag.', $subscription->id));
             }
             if ('active' == $former_order_item_meta['_wcs_migrated_subscription_status'][0] && $subscription->can_be_updated_to('active')) {
                 $subscription->update_status('active');
             }
             WCS_Upgrade_Logger::add(sprintf('For subscription %d: repaired status. Status was "expired", it is now "%s".', $subscription->id, $subscription->get_status()));
             $repair_status = true;
         } catch (Exception $e) {
             WCS_Upgrade_Logger::add(sprintf('!!! For subscription %d: unable to repair status, exception "%s"', $subscription->id, $e->getMessage()));
             $repair_status = false;
         }
     } else {
         WCS_Upgrade_Logger::add(sprintf('For subscription %d: no need to repair status, current status: %s; former status: %s.', $subscription->id, $subscription->get_status(), $former_order_item_meta['_wcs_migrated_subscription_status'][0]));
         $repair_status = false;
     }
     return $repair_status;
 }
 /**
  * Check if a given item on a subscription can be switched.
  *
  * For an item to be switchable, switching must be enabled, and the item must be for a variable subscription or
  * part of a grouped product (at the time the check is made, not at the time the subscription was purchased)
  *
  * The subscription must also be active or on-hold and use manual renewals or use a payment method which supports cancellation.
  *
  * @param array $item An order item on the subscription
  * @param WC_Subscription $subscription An instance of WC_Subscription
  * @since 2.0
  */
 public static function can_item_be_switched($item, $subscription = null)
 {
     $product_id = wcs_get_canonical_product_id($item);
     if ('line_item' == $item['type'] && wcs_is_product_switchable_type($product_id)) {
         $is_product_switchable = true;
     } else {
         $is_product_switchable = false;
     }
     if ($subscription->has_status('active') && 0 !== $subscription->get_date('last_payment')) {
         $is_subscription_switchable = true;
     } else {
         $is_subscription_switchable = false;
     }
     if ($subscription->payment_method_supports('subscription_amount_changes') && $subscription->payment_method_supports('subscription_date_changes')) {
         $can_subscription_be_updated = true;
     } else {
         $can_subscription_be_updated = false;
     }
     if ($is_product_switchable && $is_subscription_switchable && $can_subscription_be_updated) {
         $item_can_be_switch = true;
     } else {
         $item_can_be_switch = false;
     }
     return apply_filters('woocommerce_subscriptions_can_item_be_switched', $item_can_be_switch, $item, $subscription);
 }
 /**
  * Validate the incoming request to either remove an item or add and item back to a subscription that was previously removed.
  * Add an descriptive notice to the page whether or not the request was validated or not.
  *
  * @since 2.0
  * @param WC_Subscription $subscription
  * @param int $order_item_id
  * @param bool $undo_request bool
  * @return bool
  */
 private static function validate_remove_items_request($subscription, $order_item_id, $undo_request = false)
 {
     $subscription_items = $subscription->get_items();
     $response = false;
     if (!wp_verify_nonce($_GET['_wpnonce'], $_GET['subscription_id'])) {
         wc_add_notice(__('Security error. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
     } elseif (!current_user_can('edit_shop_subscription_line_items', $subscription->id)) {
         wc_add_notice(__('You cannot modify a subscription that does not belong to you.', 'woocommerce-subscriptions'), 'error');
     } elseif (!$undo_request && !isset($subscription_items[$order_item_id])) {
         // only need to validate the order item id when removing
         wc_add_notice(__('You cannot remove an item that does not exist. ', 'woocommerce-subscriptions'), 'error');
     } elseif (!$subscription->payment_method_supports('subscription_amount_changes')) {
         wc_add_notice(__('The item was not removed because this Subscription\'s payment method does not support removing an item.', 'woocommerce-subscriptions'));
     } else {
         $response = true;
     }
     return $response;
 }