/** * Filter the WC_Subscription::get_related_orders() method to include switch orders. * * @since 2.0 */ public static function add_related_orders($related_orders, $subscription, $return_fields, $order_type) { if (in_array($order_type, array('all', 'switch'))) { $switch_orders = wcs_get_switch_orders_for_subscription($subscription->id); if ('all' == $return_fields) { $related_orders += $switch_orders; } else { foreach ($switch_orders as $order_id => $order) { $related_orders[$order_id] = $order_id; } } // This will change the ordering to be by ID instead of the default of date krsort($related_orders); } return $related_orders; }
/** * Gets the most recent order that relates to a subscription, including renewal orders and the initial order (if any). * * @param string $return_fields The columns to return, either 'all' or 'ids' * @param array $order_types Can include any combination of 'parent', 'renewal', 'switch' or 'any' which will return the latest renewal order of any type. Defaults to 'parent' and 'renewal'. * @since 2.0 */ public function get_last_order($return_fields = 'ids', $order_types = array('parent', 'renewal')) { $return_fields = 'ids' == $return_fields ? $return_fields : 'all'; $order_types = 'any' == $order_types ? array('parent', 'renewal', 'switch') : $order_types; $related_orders = array(); foreach ($order_types as $order_type) { switch ($order_type) { case 'parent': if (false !== $this->order) { $related_orders[] = $this->order->id; } break; case 'renewal': $related_orders = array_merge($related_orders, WC_Subscriptions::$cache->cache_and_get('wcs-related-orders-to-' . $this->id, array($this, 'get_related_orders_query'), array($this->id))); break; case 'switch': $related_orders = array_merge($related_orders, array_keys(wcs_get_switch_orders_for_subscription($this->id))); break; default: break; } } if (empty($related_orders)) { $last_order = false; } else { $last_order = max($related_orders); if ('all' == $return_fields) { if (false !== $this->order && $last_order == $this->order->id) { $last_order = $this->order; } else { $last_order = wc_get_order($last_order); } } } return apply_filters('woocommerce_subscription_last_order', $last_order, $this); }