Ejemplo n.º 1
4
/**
 * A wrapper for @see wcs_get_subscriptions() which accepts simply an order ID
 *
 * @param int|WC_Order $order_id The post_id of a shop_order post or an instance of a WC_Order object
 * @param array $args A set of name value pairs to filter the returned value.
 *		'subscriptions_per_page' The number of subscriptions to return. Default set to -1 to return all.
 *		'offset' An optional number of subscription to displace or pass over. Default 0.
 *		'orderby' The field which the subscriptions should be ordered by. Can be 'start_date', 'trial_end_date', 'end_date', 'status' or 'order_id'. Defaults to 'start_date'.
 *		'order' The order of the values returned. Can be 'ASC' or 'DESC'. Defaults to 'DESC'
 *		'customer_id' The user ID of a customer on the site.
 *		'product_id' The post ID of a WC_Product_Subscription, WC_Product_Variable_Subscription or WC_Product_Subscription_Variation object
 *		'order_id' The post ID of a shop_order post/WC_Order object which was used to create the subscription
 *		'subscription_status' Any valid subscription status. Can be 'any', 'active', 'cancelled', 'suspended', 'expired', 'pending' or 'trash'. Defaults to 'any'.
 *		'order_type' Get subscriptions for the any order type in this array. Can include 'any', 'parent', 'renewal' or 'switch', defaults to parent.
 * @return array Subscription details in post_id => WC_Subscription form.
 * @since  2.0
 */
function wcs_get_subscriptions_for_order($order_id, $args = array())
{
    if (is_object($order_id)) {
        $order_id = $order_id->id;
    }
    $args = wp_parse_args($args, array('order_id' => $order_id, 'subscriptions_per_page' => -1, 'order_type' => array('parent', 'switch')));
    // Accept either an array or string (to make it more convenient for singular types, like 'parent' or 'any')
    if (!is_array($args['order_type'])) {
        $args['order_type'] = array($args['order_type']);
    }
    $subscriptions = array();
    $get_all = in_array('any', $args['order_type']) ? true : false;
    if ($order_id && in_array('parent', $args['order_type']) || $get_all) {
        $subscriptions = wcs_get_subscriptions($args);
    }
    if (wcs_order_contains_resubscribe($order_id) && (in_array('resubscribe', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_resubscribe_order($order_id);
    }
    if (wcs_order_contains_renewal($order_id) && (in_array('renewal', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_renewal_order($order_id);
    }
    if (wcs_order_contains_switch($order_id) && (in_array('switch', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_switch_order($order_id);
    }
    return $subscriptions;
}
 /**
  * Uses the details of an order to create a pending subscription on the customers account
  * for a subscription product, as specified with $product_id.
  *
  * @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
  * @param int $product_id The ID of the subscription product on the order, if a variation, it must be the variation's ID.
  * @param array $args An array of name => value pairs to customise the details of the subscription, including:
  * 			'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
  * 			'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
  * @since 1.1
  */
 public static function create_pending_subscription_for_order($order, $product_id, $args = array())
 {
     _deprecated_function(__METHOD__, '2.0', 'wcs_create_subscription()');
     if (!is_object($order)) {
         $order = new WC_Order($order);
     }
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return;
     }
     $args = wp_parse_args($args, array('start_date' => get_gmt_from_date($order->order_date), 'expiry_date' => ''));
     $billing_period = WC_Subscriptions_Product::get_period($product_id);
     $billing_interval = WC_Subscriptions_Product::get_interval($product_id);
     // Support passing timestamps
     $args['start_date'] = is_numeric($args['start_date']) ? date('Y-m-d H:i:s', $args['start_date']) : $args['start_date'];
     $product = wc_get_product($product_id);
     // Check if there is already a subscription for this product and order
     $subscriptions = wcs_get_subscriptions(array('order_id' => $order->id, 'product_id' => $product_id));
     if (!empty($subscriptions)) {
         $subscription = array_pop($subscriptions);
         // Make sure the subscription is pending and start date is set correctly
         wp_update_post(array('ID' => $subscription->id, 'post_status' => 'wc-' . apply_filters('woocommerce_default_subscription_status', 'pending'), 'post_date' => get_date_from_gmt($args['start_date'])));
     } else {
         $subscription = wcs_create_subscription(array('start_date' => get_date_from_gmt($args['start_date']), 'order_id' => $order->id, 'customer_id' => $order->get_user_id(), 'billing_period' => $billing_period, 'billing_interval' => $billing_interval, 'customer_note' => $order->customer_note));
         if (is_wp_error($subscription)) {
             throw new Exception(__('Error: Unable to create subscription. Please try again.', 'woocommerce-subscriptions'));
         }
         $item_id = $subscription->add_product($product, 1, array('variation' => method_exists($product, 'get_variation_attributes') ? $product->get_variation_attributes() : array(), 'totals' => array('subtotal' => $product->get_price(), 'subtotal_tax' => 0, 'total' => $product->get_price(), 'tax' => 0, 'tax_data' => array('subtotal' => array(), 'total' => array()))));
         if (!$item_id) {
             throw new Exception(__('Error: Unable to add product to created subscription. Please try again.', 'woocommerce-subscriptions'));
         }
     }
     // Make sure some of the meta is copied form the order rather than the store's defaults
     update_post_meta($subscription->id, '_order_currency', $order->order_currency);
     update_post_meta($subscription->id, '_prices_include_tax', $order->prices_include_tax);
     // Adding a new subscription so set the expiry date/time from the order date
     if (!empty($args['expiry_date'])) {
         if (is_numeric($args['expiry_date'])) {
             $args['expiry_date'] = date('Y-m-d H:i:s', $args['expiry_date']);
         }
         $expiration = $args['expiry_date'];
     } else {
         $expiration = WC_Subscriptions_Product::get_expiration_date($product_id, $args['start_date']);
     }
     // Adding a new subscription so set the expiry date/time from the order date
     $trial_expiration = WC_Subscriptions_Product::get_trial_expiration_date($product_id, $args['start_date']);
     $dates_to_update = array();
     if ($trial_expiration > 0) {
         $dates_to_update['trial_end'] = $trial_expiration;
     }
     if ($expiration > 0) {
         $dates_to_update['end'] = $expiration;
     }
     if (!empty($dates_to_update)) {
         $subscription->update_dates($dates_to_update);
     }
     // Set the recurring totals on the subscription
     $subscription->set_total(0, 'tax');
     $subscription->set_total($product->get_price(), 'total');
     $subscription->add_order_note(__('Pending subscription created.', 'woocommerce-subscriptions'));
     do_action('pending_subscription_created_for_order', $order, $product_id);
 }
 /**
  * Get subscription by order_id and product_id
  *
  * Compatibility method for supporting both Subscriptions 2.0
  * and earlier
  *
  * @since 1.0.0
  * @param int $order_id
  * @param int $product_id
  * @return mixed Subscription array (pre 2.0), object (2.0 onwards) or null if not found
  */
 private function get_order_product_subscription($order_id, $product_id)
 {
     if ($this->is_subscriptions_gte_2_0()) {
         $subscriptions = wcs_get_subscriptions(array('order_id' => $order_id, 'product_id' => $product_id));
         $subscription = reset($subscriptions);
     } else {
         $subscription_key = WC_Subscriptions_Manager::get_subscription_key($order_id, $product_id);
         $subscription = WC_Subscriptions_Manager::get_subscription($subscription_key);
     }
     return $subscription;
 }
Ejemplo n.º 4
0
 /**
  * Takes an array of filter params and returns the number of subscriptions which match those params.
  *
  * @since 1.4
  * @deprecated 2.0
  */
 public static function get_subscription_count($args = array())
 {
     _deprecated_function(__METHOD__, '2.0');
     $args['subscriptions_per_page'] = -1;
     $subscription_count = 0;
     if ((!isset($args['subscription_status']) || in_array($args['subscription_status'], array('all', 'any'))) && (isset($args['include_trashed']) && true === $args['include_trashed'])) {
         $args['subscription_status'] = 'trash';
         $subscription_count += count(wcs_get_subscriptions($args));
         $args['subscription_status'] = 'any';
     }
     $subscription_count += count(wcs_get_subscriptions($args));
     return apply_filters('woocommerce_get_subscription_count', $subscription_count, $args);
 }
 /**
  * Check if the current session has an order awaiting payment for a subscription to a specific product line item.
  *
  * @return 2.0.13
  * @return bool
  **/
 protected static function order_awaiting_payment_for_product($product_id)
 {
     global $wp;
     if (!isset(self::$order_awaiting_payment_for_product[$product_id])) {
         self::$order_awaiting_payment_for_product[$product_id] = 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'))) {
                                 self::$order_awaiting_payment_for_product[$product_id] = true;
                             }
                         }
                         break;
                     }
                 }
             }
         }
     }
     return self::$order_awaiting_payment_for_product[$product_id];
 }
 /**
  * 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;
 }
 /**
  * @return bool
  */
 public function ndv_auto_handle_subscription()
 {
     // Get order pending.
     $records = wcs_get_subscriptions(array('subscription_status' => 'pending-cancel', 'subscriptions_per_page' => 100, 'orderby' => 'order_total', 'order' => 'ASC'));
     if (count($records)) {
         foreach ($records as $sub) {
             if ($sub->get_total() == 0) {
                 $this->remove_sub($sub);
             }
         }
     }
     $records = wcs_get_subscriptions(array('subscription_status' => 'cancelled', 'subscriptions_per_page' => 100, 'orderby' => 'order_total', 'order' => 'ASC'));
     if (count($records)) {
         foreach ($records as $sub) {
             if ($sub->get_total() == 0) {
                 $this->remove_sub($sub);
             }
         }
     }
     $records = wcs_get_subscriptions(array('subscription_status' => 'on-hold', 'subscriptions_per_page' => 100, 'orderby' => 'order_total', 'order' => 'ASC'));
     if (count($records)) {
         foreach ($records as $sub) {
             if ($sub->get_total() == 0) {
                 $this->remove_sub($sub);
             }
         }
     }
     dd($records);
 }