/**
  * Takes a subscription key and array of subscription details and updates the users subscription details accordingly.
  *
  * @uses wp_parse_args To allow only part of a subscription's details to be updated, like status.
  * @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
  * @param array $new_subscription_details An array of arrays with a subscription key and corresponding 'detail' => 'value' pair. Can alter any of these details:
  *        'start_date'          The date the subscription was activated
  *        'expiry_date'         The date the subscription expires or expired, false if the subscription will never expire
  *        'failed_payments'     The date the subscription's trial expires or expired, false if the subscription has no trial period
  *        'end_date'            The date the subscription ended, false if the subscription has not yet ended
  *        'status'              Subscription status can be: cancelled, active, expired or failed
  *        'completed_payments'  An array of MySQL formatted dates for all payments that have been made on the subscription
  *        'failed_payments'     An integer representing a count of failed payments
  *        'suspension_count'    An integer representing a count of the number of times the subscription has been suspended for this billing period
  * @since 1.4
  */
 public static function update_subscription($subscription_key, $new_subscription_details)
 {
     $subscription = self::get_subscription($subscription_key);
     $item_id = WC_Subscriptions_Order::get_item_id_by_subscription_key($subscription_key);
     $item = WC_Subscriptions_Order::get_item_by_id($item_id);
     if (isset($new_subscription_details['status']) && 'deleted' == $new_subscription_details['status']) {
         woocommerce_delete_order_item($item_id);
     } else {
         $subscription_meta = array('start_date', 'expiry_date', 'trial_expiry_date', 'end_date', 'status', 'failed_payments', 'completed_payments', 'suspension_count');
         foreach ($subscription_meta as $meta_key) {
             if (isset($new_subscription_details[$meta_key]) && $new_subscription_details[$meta_key] != $subscription[$meta_key]) {
                 $subscription[$meta_key] = $new_subscription_details[$meta_key];
                 woocommerce_update_order_item_meta($item_id, '_subscription_' . $meta_key, $new_subscription_details[$meta_key]);
             }
         }
     }
     do_action('updated_users_subscription', $subscription_key, $new_subscription_details);
     return $subscription;
 }
コード例 #2
0
 /**
  * Creates a 2.0 updated version of the "subscriptions_switched" callback for developers to hook onto.
  *
  * The subscription passed to the new `woocommerce_subscriptions_switched_item` callback is strictly the subscription
  * to which the `$new_order_item` belongs to; this may be a new or the original subscription.
  *
  * @since 2.0.5
  * @param WC_Order $order
  */
 public static function maybe_add_switched_callback($order)
 {
     if (wcs_order_contains_switch($order)) {
         $subscriptions = wcs_get_subscriptions_for_order($order);
         foreach ($subscriptions as $subscription) {
             foreach ($subscription->get_items() as $new_order_item) {
                 if (isset($new_order_item['switched_subscription_item_id'])) {
                     $product_id = wcs_get_canonical_product_id($new_order_item);
                     // we need to check if the switch order contains the line item that has just been switched so that we don't call the hook on items that were previously switched in another order
                     foreach ($order->get_items() as $order_item) {
                         if (wcs_get_canonical_product_id($order_item) == $product_id) {
                             do_action('woocommerce_subscriptions_switched_item', $subscription, $new_order_item, WC_Subscriptions_Order::get_item_by_id($new_order_item['switched_subscription_item_id']));
                             break;
                         }
                     }
                 }
             }
         }
     }
 }