コード例 #1
0
/**
 * A general purpose function for grabbing an array of subscriptions in form of post_id => WC_Subscription
 *
 * The $args parameter is based on the parameter of the same name used by the core WordPress @see get_posts() function.
 * It can be used to choose which subscriptions should be returned by the function, how many subscriptions should be returned
 * and in what order those subscriptions should be returned.
 *
 * @param array $args A set of name value pairs to determine the return value.
 *		'subscriptions_per_page' The number of subscriptions to return. Set to -1 for unlimited. Default 10.
 *		'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'.
 * @return array Subscription details in post_id => WC_Subscription form.
 * @since  2.0
 */
function wcs_get_subscriptions($args)
{
    global $wpdb;
    $args = wp_parse_args($args, array('subscriptions_per_page' => 10, 'paged' => 1, 'offset' => 0, 'orderby' => 'start_date', 'order' => 'DESC', 'customer_id' => 0, 'product_id' => 0, 'variation_id' => 0, 'order_id' => 0, 'subscription_status' => 'any', 'meta_query_relation' => 'AND'));
    // if order_id is not a shop_order
    if (0 !== $args['order_id'] && 'shop_order' !== get_post_type($args['order_id'])) {
        return array();
    }
    // Make sure status starts with 'wc-'
    if (!in_array($args['subscription_status'], array('any', 'trash'))) {
        $args['subscription_status'] = wcs_sanitize_subscription_status_key($args['subscription_status']);
    }
    // Prepare the args for WP_Query
    $query_args = array('post_type' => 'shop_subscription', 'post_status' => $args['subscription_status'], 'posts_per_page' => $args['subscriptions_per_page'], 'paged' => $args['paged'], 'offset' => $args['offset'], 'order' => $args['order'], 'fields' => 'ids', 'meta_query' => array());
    // Maybe only get subscriptions created by a certain order
    if (0 != $args['order_id'] && is_numeric($args['order_id'])) {
        $query_args['post_parent'] = $args['order_id'];
    }
    // Map subscription specific orderby values to internal/WordPress keys
    switch ($args['orderby']) {
        case 'status':
            $query_args['orderby'] = 'post_status';
            break;
        case 'start_date':
            $query_args['orderby'] = 'date';
            break;
        case 'trial_end_date':
        case 'end_date':
            // We need to orderby post meta value: http://www.paulund.co.uk/order-meta-query
            $query_args = array_merge($query_args, array('orderby' => 'meta_value', 'meta_key' => wcs_get_date_meta_key($args['orderby']), 'meta_type' => 'DATETIME'));
            $query_args['meta_query'][] = array('key' => wcs_get_date_meta_key($args['orderby']), 'value' => 'EXISTS', 'type' => 'DATETIME');
            break;
        default:
            $query_args['orderby'] = $args['orderby'];
            break;
    }
    // Maybe filter to a specific user
    if (0 != $args['customer_id'] && is_numeric($args['customer_id'])) {
        $query_args['meta_query'][] = array('key' => '_customer_user', 'value' => $args['customer_id'], 'type' => 'numeric', 'compare' => is_array($args['customer_id']) ? 'IN' : '=');
    }
    // We need to restrict subscriptions to those which contain a certain product/variation
    if (0 != $args['product_id'] && is_numeric($args['product_id']) || 0 != $args['variation_id'] && is_numeric($args['variation_id'])) {
        $query_args['post__in'] = wcs_get_subscriptions_for_product(array($args['product_id'], $args['variation_id']));
    }
    if (!empty($query_args['meta_query'])) {
        $query_args['meta_query']['relation'] = $args['meta_query_relation'];
    }
    $query_args = apply_filters('woocommerce_get_subscriptions_query_args', $query_args, $args);
    $subscription_post_ids = get_posts($query_args);
    $subscriptions = array();
    foreach ($subscription_post_ids as $post_id) {
        $subscriptions[$post_id] = wcs_get_subscription($post_id);
    }
    return apply_filters('woocommerce_got_subscriptions', $subscriptions, $args);
}
コード例 #2
0
 /**
  * Remove a date from a subscription.
  *
  * @param string $date_type 'trial_end', 'next_payment' or 'end'. The 'start' and 'last_payment' date types will throw an exception.
  */
 public function delete_date($date_type)
 {
     // Accept dates with a '_date' suffix, like 'next_payment_date' or 'start_date'
     $date_type = str_replace('_date', '', $date_type);
     // Make sure some dates are before next payment date
     if (in_array($date_type, array('start', 'last_payment'))) {
         switch ($date_type) {
             case 'start':
                 $message = __('The start date of a subscription can not be deleted, only updated.', 'woocommerce-subscriptions');
                 break;
             case 'last_payment':
                 $message = __('The last payment date of a subscription can not be deleted. You must delete the order.', 'woocommerce-subscriptions');
                 break;
         }
         throw new Exception($message);
     }
     $this->schedule->{$date_type} = 0;
     update_post_meta($this->id, wcs_get_date_meta_key($date_type), $this->schedule->{$date_type});
     do_action('woocommerce_subscription_date_deleted', $this, $date_type);
 }