/**
  * Filter the "Orders" list to show only orders associated with a specific subscription.
  *
  * @param string $where
  * @param string $request
  * @return string
  * @since 2.0
  */
 public static function filter_orders($where)
 {
     global $typenow, $wpdb;
     if (is_admin() && 'shop_order' == $typenow) {
         $related_orders = array();
         if (isset($_GET['_subscription_related_orders']) && $_GET['_subscription_related_orders'] > 0) {
             $subscription_id = absint($_GET['_subscription_related_orders']);
             $subscription = wcs_get_subscription($subscription_id);
             if (!wcs_is_subscription($subscription)) {
                 // translators: placeholder is a number
                 wcs_add_admin_notice(sprintf(__('We can\'t find a subscription with ID #%d. Perhaps it was deleted?', 'woocommerce-subscriptions'), $subscription_id), 'error');
                 $where .= " AND {$wpdb->posts}.ID = 0";
             } else {
                 self::$found_related_orders = true;
                 $where .= sprintf(" AND {$wpdb->posts}.ID IN (%s)", implode(',', array_map('absint', array_unique($subscription->get_related_orders('ids')))));
             }
         }
     }
     return $where;
 }
 /**
  * When adding new downloadable content to a subscription product, check if we don't
  * want to automatically add the new downloadable files to the subscription or initial and renewal orders.
  *
  * @param bool $grant_access
  * @param string $download_id
  * @param int $product_id
  * @param WC_Order $order
  * @return bool
  * @since 2.0
  */
 public static function maybe_revoke_immediate_access($grant_access, $download_id, $product_id, $order)
 {
     if ('yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_drip_downloadable_content_on_renewal', 'no') && (wcs_is_subscription($order->id) || wcs_order_contains_subscription($order, 'any'))) {
         $grant_access = false;
     }
     return $grant_access;
 }
 /**
  * Displays the renewal orders in the Related Orders meta box.
  *
  * @param object $post A WordPress post
  * @since 2.0
  */
 public static function output_rows($post)
 {
     $subscriptions = array();
     $orders = array();
     // On the subscription page, just show related orders
     if (wcs_is_subscription($post->ID)) {
         $subscriptions[] = wcs_get_subscription($post->ID);
     } elseif (wcs_order_contains_subscription($post->ID, array('parent', 'renewal'))) {
         $subscriptions = wcs_get_subscriptions_for_order($post->ID, array('order_type' => array('parent', 'renewal')));
     }
     // First, display all the subscriptions
     foreach ($subscriptions as $subscription) {
         $subscription->relationship = __('Subscription', 'woocommerce-subscriptions');
         $orders[] = $subscription;
     }
     //Resubscribed
     $initial_subscriptions = array();
     if (wcs_is_subscription($post->ID)) {
         $initial_subscriptions = wcs_get_subscriptions_for_resubscribe_order($post->ID);
         $resubscribed_subscriptions = get_posts(array('meta_key' => '_subscription_resubscribe', 'meta_value' => $post->ID, 'post_type' => 'shop_subscription', 'post_status' => 'any', 'posts_per_page' => -1));
         foreach ($resubscribed_subscriptions as $subscription) {
             $subscription = wcs_get_subscription($subscription);
             $subscription->relationship = _x('Resubscribed Subscription', 'relation to order', 'woocommerce-subscriptions');
             $orders[] = $subscription;
         }
     } else {
         if (wcs_order_contains_subscription($post->ID, array('resubscribe'))) {
             $initial_subscriptions = wcs_get_subscriptions_for_order($post->ID, array('order_type' => array('resubscribe')));
         }
     }
     foreach ($initial_subscriptions as $subscription) {
         $subscription->relationship = _x('Initial Subscription', 'relation to order', 'woocommerce-subscriptions');
         $orders[] = $subscription;
     }
     // Now, if we're on a single subscription or renewal order's page, display the parent orders
     if (1 == count($subscriptions)) {
         foreach ($subscriptions as $subscription) {
             if (false !== $subscription->order) {
                 $subscription->order->relationship = _x('Parent Order', 'relation to order', 'woocommerce-subscriptions');
                 $orders[] = $subscription->order;
             }
         }
     }
     // Finally, display the renewal orders
     foreach ($subscriptions as $subscription) {
         foreach ($subscription->get_related_orders('all', 'renewal') as $order) {
             $order->relationship = _x('Renewal Order', 'relation to order', 'woocommerce-subscriptions');
             $orders[] = $order;
         }
     }
     $orders = apply_filters('woocommerce_subscriptions_admin_related_orders_to_display', $orders, $subscriptions, $post);
     foreach ($orders as $order) {
         if ($order->id == $post->ID) {
             continue;
         }
         include 'views/html-related-orders-row.php';
     }
 }
 /**
  * Process the remove or re-add a line item from a subscription request.
  *
  * @since 2.0
  */
 public static function maybe_remove_or_add_item_to_subscription()
 {
     if (isset($_GET['subscription_id']) && (isset($_GET['remove_item']) || isset($_GET['undo_remove_item'])) && isset($_GET['_wpnonce'])) {
         $subscription = wcs_is_subscription($_GET['subscription_id']) ? wcs_get_subscription($_GET['subscription_id']) : false;
         $undo_request = isset($_GET['undo_remove_item']) ? true : false;
         $item_id = $undo_request ? $_GET['undo_remove_item'] : $_GET['remove_item'];
         if (false === $subscription) {
             wc_add_notice(sprintf(_x('Subscription #%d does not exist.', 'hash before subscription ID', 'woocommerce-subscriptions'), $_GET['subscription_id']), 'error');
             wp_safe_redirect(wc_get_page_permalink('myaccount'));
             exit;
         }
         if (self::validate_remove_items_request($subscription, $item_id, $undo_request)) {
             if ($undo_request) {
                 // handle undo request
                 $removed_item = WC()->session->get('removed_subscription_items', array());
                 if (!empty($removed_item[$item_id]) && $subscription->id == $removed_item[$item_id]) {
                     // restore the item
                     wc_update_order_item($item_id, array('order_item_type' => 'line_item'));
                     unset($removed_item[$item_id]);
                     WC()->session->set('removed_subscription_items', $removed_item);
                     // restore download permissions for this item
                     $line_items = $subscription->get_items();
                     $line_item = $line_items[$item_id];
                     $_product = $subscription->get_product_from_item($line_item);
                     $product_id = wcs_get_canonical_product_id($line_item);
                     if ($_product && $_product->exists() && $_product->is_downloadable()) {
                         $downloads = $_product->get_files();
                         foreach (array_keys($downloads) as $download_id) {
                             wc_downloadable_file_permission($download_id, $product_id, $subscription, $line_item['qty']);
                         }
                     }
                     // translators: 1$: product name, 2$: product id
                     $subscription->add_order_note(sprintf(_x('Customer added "%1$s" (Product ID: #%2$d) via the My Account page.', 'used in order note', 'woocommerce-subscriptions'), wcs_get_line_item_name($line_item), $product_id));
                 } else {
                     wc_add_notice(__('Your request to undo your previous action was unsuccessful.', 'woocommerce-subscriptions'));
                 }
             } else {
                 // handle remove item requests
                 WC()->session->set('removed_subscription_items', array($item_id => $subscription->id));
                 // remove download access for the item
                 $line_items = $subscription->get_items();
                 $line_item = $line_items[$item_id];
                 $product_id = wcs_get_canonical_product_id($line_item);
                 WCS_Download_Handler::revoke_downloadable_file_permission($product_id, $subscription->id, $subscription->get_user_id());
                 // remove the line item from subscription but preserve its data in the DB
                 wc_update_order_item($item_id, array('order_item_type' => 'line_item_removed'));
                 // translators: 1$: product name, 2$: product id
                 $subscription->add_order_note(sprintf(_x('Customer removed "%1$s" (Product ID: #%2$d) via the My Account page.', 'used in order note', 'woocommerce-subscriptions'), wcs_get_line_item_name($line_item), $product_id));
                 // translators: placeholders are 1$: item name, and, 2$: opening and, 3$: closing link tags
                 wc_add_notice(sprintf(__('You have successfully removed "%1$s" from your subscription. %2$sUndo?%3$s', 'woocommerce-subscriptions'), $line_item['name'], '<a href="' . esc_url(self::get_undo_remove_url($subscription->id, $item_id, $subscription->get_view_order_url())) . '" >', '</a>'));
             }
         }
         $subscription->calculate_totals();
         wp_safe_redirect($subscription->get_view_order_url());
         exit;
     }
 }
/**
 * Main function for returning subscriptions. Wrapper for the wc_get_order() method.
 *
 * @since  2.0
 * @param  mixed $the_subscription Post object or post ID of the order.
 * @return WC_Subscription
 */
function wcs_get_subscription($the_subscription)
{
    if (is_object($the_subscription) && wcs_is_subscription($the_subscription)) {
        $the_subscription = $the_subscription->id;
    }
    $subscription = WC()->order_factory->get_order($the_subscription);
    if (!wcs_is_subscription($subscription)) {
        $subscription = false;
    }
    return apply_filters('wcs_get_subscription', $subscription);
}
 /**
  * Process payment
  *
  * @param int $order_id
  */
 public function process_payment($order_id)
 {
     if (!$this->order_contains_subscription($order_id) && !wcs_is_subscription($order_id)) {
         return parent::process_payment($order_id);
     }
     $amazon_billing_agreement_id = isset($_POST['amazon_billing_agreement_id']) ? wc_clean($_POST['amazon_billing_agreement_id']) : '';
     try {
         if (!$amazon_billing_agreement_id) {
             throw new Exception(__('An Amazon payment method was not chosen.', 'woocommerce-gateway-amazon-payments-advanced'));
         }
         $order = new WC_Order($order_id);
         $order_total = $order->get_total();
         $this->log(__FUNCTION__, "Info: Beginning processing of payment for (subscription) order {$order_id} for the amount of {$order_total} {$order->get_order_currency()}.");
         // Set the Billing Agreement Details
         $this->set_billing_agreement_details($order, $amazon_billing_agreement_id);
         // Confirm the Billing Agreement
         $this->confirm_billing_agreement($order_id, $amazon_billing_agreement_id);
         // Get the Billing Agreement Details, with FULL address (now that we've confirmed)
         $result = $this->get_billing_agreement_details($order_id, $amazon_billing_agreement_id);
         // Store the subscription destination
         $this->store_subscription_destination($order_id, $result);
         // Store Billing Agreement ID on the order and it's subscriptions
         $result = update_post_meta($order_id, 'amazon_billing_agreement_id', $amazon_billing_agreement_id);
         if ($result) {
             $this->log(__FUNCTION__, "Info: Successfully stored billing agreement in meta for order {$order_id}.");
         } else {
             $this->log(__FUNCTION__, "Error: Failed to store billing agreement in meta for order {$order_id}.");
         }
         $subscriptions = wcs_get_subscriptions_for_order($order_id);
         foreach ($subscriptions as $subscription) {
             $result = update_post_meta($subscription->id, 'amazon_billing_agreement_id', $amazon_billing_agreement_id);
             if ($result) {
                 $this->log(__FUNCTION__, "Info: Successfully stored billing agreement in meta for subscription {$subscription->id} (parent order {$order_id}).");
             } else {
                 $this->log(__FUNCTION__, "Error: Failed to store billing agreement in meta for subscription {$subscription->id} (parent order {$order_id}).");
             }
         }
         // Authorize/Capture initial payment, if initial payment required
         if ($order_total > 0) {
             return $this->authorize_payment($order, $amazon_billing_agreement_id);
         }
         // No payment needed now, free trial or coupon used - mark order as complete
         $order->payment_complete();
         $this->log(__FUNCTION__, "Info: Zero-total initial payment for (subscription) order {$order_id}. Payment marked as complete.");
         // Remove items from cart
         WC()->cart->empty_cart();
         // Return thank you page redirect
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } catch (Exception $e) {
         $this->log(__FUNCTION__, "Error: Exception encountered: {$e->getMessage()}");
         wc_add_notice(sprintf(__('Error: %s', 'woocommerce-gateway-amazon-payments-advanced'), $e->getMessage()), 'error');
         return;
     }
 }
 /**
  * Setup payload for subscription webhook delivery.
  *
  * @since 2.0
  */
 public static function create_payload($payload, $resource, $resource_id, $id)
 {
     if ('subscription' == $resource && empty($payload) && wcs_is_subscription($resource_id)) {
         $webhook = new WC_Webhook($id);
         $event = $webhook->get_event();
         $current_user = get_current_user_id();
         wp_set_current_user($webhook->get_user_id());
         WC()->api->WC_API_Subscriptions->register_routes(array());
         $payload = WC()->api->WC_API_Subscriptions->get_subscription($resource_id);
         wp_set_current_user($current_user);
     }
     return $payload;
 }
 /**
  * Checks if the user's current request to change the status of their subscription is valid.
  *
  * @since 2.0
  */
 public static function validate_request($user_id, $subscription, $new_status, $wpnonce = '')
 {
     $subscription = !is_object($subscription) ? wcs_get_subscription($subscription) : $subscription;
     if (!wcs_is_subscription($subscription)) {
         WC_Subscriptions::add_notice(__('That subscription does not exist. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
         return false;
     } elseif (!empty($wpnonce) && wp_verify_nonce($wpnonce, $subscription->id) === false) {
         WC_Subscriptions::add_notice(__('Security error. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
         return false;
     } elseif (!user_can($user_id, 'edit_shop_subscription_status', $subscription->id)) {
         WC_Subscriptions::add_notice(__('That doesn\'t appear to be one of your subscriptions.', 'woocommerce-subscriptions'), 'error');
         return false;
     } elseif (!$subscription->can_be_updated_to($new_status)) {
         WC_Subscriptions::add_notice(sprintf(__('That subscription can not be changed to %s. Please contact us if you need assistance.', 'woocommerce-subscriptions'), $new_status), 'error');
         return false;
     }
     return true;
 }
 /**
  * Displays the renewal orders in the Related Orders meta box.
  *
  * @param object $post A WordPress post
  * @since 2.0
  */
 public static function output_rows($post)
 {
     $subscriptions = array();
     $orders = array();
     // On the subscription page, just show related orders
     if (wcs_is_subscription($post->ID)) {
         $subscriptions[] = wcs_get_subscription($post->ID);
     } elseif (wcs_order_contains_subscription($post->ID, array('parent', 'renewal'))) {
         $subscriptions = wcs_get_subscriptions_for_order($post->ID, array('order_type' => array('parent', 'renewal')));
     }
     // First, display all the subscriptions
     foreach ($subscriptions as $subscription) {
         $subscription->relationship = _x('Subscription', 'relation to order', 'woocommerce-subscriptions');
         $orders[] = $subscription;
     }
     // Now, if we're on a single subscription or renewal order's page, display the parent orders
     if (1 == count($subscriptions)) {
         foreach ($subscriptions as $subscription) {
             if (false !== $subscription->order) {
                 $subscription->order->relationship = _x('Parent Order', 'relation to order', 'woocommerce-subscriptions');
                 $orders[] = $subscription->order;
             }
         }
     }
     // Finally, display the renewal orders
     foreach ($subscriptions as $subscription) {
         foreach ($subscription->get_related_orders('all', 'renewal') as $order) {
             $order->relationship = _x('Renewal Order', 'relation to order', 'woocommerce-subscriptions');
             $orders[] = $order;
         }
     }
     foreach ($orders as $order) {
         if ($order->id == $post->ID) {
             continue;
         }
         include 'views/html-related-orders-row.php';
     }
 }
Example #10
0
 /**
  * Does some housekeeping. Fires after the items have been passed through the get items from session filter. Because
  * that filter is not good for removing cart items, we need to work around that by doing it later, in the cart
  * loaded from session action.
  *
  * This checks cart items whether underlying subscriptions / renewal orders they depend exist. If not, they are
  * removed from the cart.
  *
  * @param $cart WC_Cart the one we got from session
  */
 public function cart_items_loaded_from_session($cart)
 {
     $removed_count_subscription = $removed_count_order = 0;
     foreach ($cart->cart_contents as $key => $item) {
         if (isset($item[$this->cart_item_key]['subscription_id']) && !wcs_is_subscription($item[$this->cart_item_key]['subscription_id'])) {
             $cart->remove_cart_item($key);
             $removed_count_subscription++;
             continue;
         }
         if (isset($item[$this->cart_item_key]['renewal_order_id']) && !'shop_order' == get_post_type($item[$this->cart_item_key]['renewal_order_id'])) {
             $cart->remove_cart_item($key);
             $removed_count_order++;
             continue;
         }
     }
     if ($removed_count_subscription) {
         $error_message = esc_html(_n('We couldn\'t find the original subscription for an item in your cart. The item was removed.', 'We couldn\'t find the original subscriptions for items in your cart. The items were removed.', $removed_count_subscription, 'woocommerce-subscriptions'));
         if (!wc_has_notice($error_message, 'notice')) {
             wc_add_notice($error_message, 'notice');
         }
     }
     if ($removed_count_order) {
         $error_message = esc_html(_n('We couldn\'t find the original renewal order for an item in your cart. The item was removed.', 'We couldn\'t find the original renewal orders for items in your cart. The items were removed.', $removed_count_order, 'woocommerce-subscriptions'));
         if (!wc_has_notice($error_message, 'notice')) {
             wc_add_notice($error_message, 'notice');
         }
     }
 }
 /**
  * Get original products for a renewal order - so that we can ensure renewal coupons are only applied to those
  *
  * @param  object $subscription subscription
  * @return array $product_ids an array of product ids on a subscription renewal order
  * @since 2.0.10
  */
 protected function get_products($subscription)
 {
     $product_ids = array();
     if (wcs_is_subscription($subscription)) {
         foreach ($subscription->get_items() as $item) {
             $product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
             if (!empty($product_id)) {
                 $product_ids[] = $product_id;
             }
         }
     }
     return $product_ids;
 }
 /**
  * Override the default PayPal standard args in WooCommerce for subscription purchases when
  * automatic payments are enabled and when the recurring order totals is over $0.00 (because
  * PayPal doesn't support subscriptions with a $0 recurring total, we need to circumvent it and
  * manage it entirely ourselves.)
  *
  * @since 2.0
  */
 public static function get_paypal_args($paypal_args, $order)
 {
     if (wcs_order_contains_subscription($order, array('parent', 'renewal', 'resubscribe', 'switch')) || wcs_is_subscription($order)) {
         if (self::are_reference_transactions_enabled()) {
             $paypal_args = self::get_api()->get_paypal_args($paypal_args, $order);
         } else {
             $paypal_args = WCS_PayPal_Standard_Request::get_paypal_args($paypal_args, $order);
         }
     }
     return $paypal_args;
 }
 /**
  * Make sure certain totals are set to 0 when the request is to change the payment method without charging anything.
  *
  * @since 1.4
  */
 public static function maybe_zero_total($total, $subscription)
 {
     global $wp;
     if (!empty($_POST['_wcsnonce']) && wp_verify_nonce($_POST['_wcsnonce'], 'wcs_change_payment_method') && isset($_POST['woocommerce_change_payment']) && $subscription->order_key == $_GET['key'] && $subscription->id == absint($_POST['woocommerce_change_payment'])) {
         $total = 0;
     } elseif (!self::$is_request_to_change_payment && isset($wp->query_vars['order-pay']) && wcs_is_subscription(absint($wp->query_vars['order-pay']))) {
         // if the request to pay for the order belongs to a subscription but there's no GET params for changing payment method, the receipt page is being used to collect credit card details so we still need to $0 the total
         $total = 0;
     }
     return $total;
 }
/**
 * Get the subscription to which a renewal order relates.
 *
 * @param WC_Order|int $order The WC_Order object or ID of a WC_Order order.
 * @since 2.0
 */
function wcs_get_subscriptions_for_renewal_order($order)
{
    if (!is_object($order)) {
        $order = wc_get_order($order);
    }
    $subscriptions = array();
    $subscription_ids = get_post_meta($order->id, '_subscription_renewal', false);
    foreach ($subscription_ids as $subscription_id) {
        if (wcs_is_subscription($subscription_id)) {
            $subscriptions[$subscription_id] = wcs_get_subscription($subscription_id);
        }
    }
    return apply_filters('wcs_subscriptions_for_renewal_order', $subscriptions, $order);
}
 /**
  * Is $order_id a subscription?
  * @param  int  $order_id
  * @return boolean
  */
 protected function is_subscription($order_id)
 {
     return function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id));
 }
 /**
  * Process the payment
  *
  * @param  int $order_id
  * @return array
  */
 public function process_payment($order_id)
 {
     // Processing subscription
     if ($this->order_contains_subscription($order_id) || function_exists('wcs_is_subscription') && wcs_is_subscription($order_id)) {
         return $this->process_subscription($order_id);
         // Processing pre-order
     } elseif ($this->order_contains_pre_order($order_id)) {
         return $this->process_pre_order($order_id);
         // Processing regular product
     } else {
         return parent::process_payment($order_id);
     }
 }
Example #17
0
		/**
		 * Check if the order has a subscription (either according to Subscriptions 1.5 or 2.0)
		 *
		 * @param string $order_id The ID of the order to check
		 * @return mixed Either 1 (Subscriptions 1.5), 2 (Subscriptions 2) or false (no order)
		 */
		function order_has_subscription($order_id) {
			// Subscriptions not loaded
			if (!class_exists('WC_Subscriptions_Order')) return false;

			// Subscriptions v2.0
			if (function_exists('wcs_order_contains_subscription')) {
				if (wcs_order_contains_subscription($order_id) || wcs_order_contains_renewal( $order_id ) || ( function_exists( 'wcs_is_subscription' ) && wcs_is_subscription( $order_id ))) {
					return 2;
				} else {
					return false;
				}
			}
			
			// Subscriptions v1.5
			if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
				return 1;
			}

			return false;
		}
 /**
  * Filters WC_Subscriptions_Order::get_sign_up_fee() to make sure the sign-up fee for a subscription product
  * that is synchronised is returned correctly.
  *
  * @param float The initial sign-up fee charged when the subscription product in the order was first purchased, if any.
  * @param mixed $order A WC_Order object or the ID of the order which the subscription was purchased in.
  * @param int $product_id The post ID of the subscription WC_Product object purchased in the order. Defaults to the ID of the first product purchased in the order.
  * @return float The initial sign-up fee charged when the subscription product in the order was first purchased, if any.
  * @since 2.0
  */
 public static function get_synced_sign_up_fee($sign_up_fee, $subscription, $product_id)
 {
     if (wcs_is_subscription($subscription) && self::subscription_contains_synced_product($subscription) && count(wcs_get_line_items_with_a_trial($subscription->id)) < 0) {
         $sign_up_fee = max($subscription->get_total_initial_payment() - $subscription->get_total(), 0);
     }
     return $sign_up_fee;
 }
 /**
  * Check if the cart includes any items which are to switch an existing subscription's item.
  *
  * @return bool|array Returns all the items that are for a switching or false if none of the items in the cart are a switch request.
  * @since 2.0
  */
 public static function cart_contains_switches()
 {
     $subscription_switches = false;
     if (is_admin() && (!defined('DOING_AJAX') || false == DOING_AJAX)) {
         return $subscription_switches;
     }
     if (isset(WC()->cart)) {
         // We use WC()->cart->cart_contents instead of WC()->cart->get_cart() to prevent recursion caused when get_cart_from_session() too early is called ref: https://github.com/woothemes/woocommerce/commit/1f3365f2066b1e9d7e84aca7b1d7e89a6989c213
         foreach (WC()->cart->cart_contents as $cart_item_key => $cart_item) {
             if (isset($cart_item['subscription_switch'])) {
                 if (wcs_is_subscription($cart_item['subscription_switch']['subscription_id'])) {
                     $subscription_switches[$cart_item_key] = $cart_item['subscription_switch'];
                 } else {
                     WC()->cart->remove_cart_item($cart_item_key);
                     WC_Subscriptions::add_notice(__('Your cart contained an invalid subscription switch request. It has been removed.', 'woocommerce-subscriptions'), 'error');
                 }
             }
         }
     }
     return $subscription_switches;
 }
 /**
  * Add order note to subscription to record the renewal order
  *
  * @param WC_Order|int $renewal_order
  * @param WC_Subscription|int $subscription
  * @since 2.0
  */
 public static function add_order_note($renewal_order, $subscription)
 {
     if (!is_object($subscription)) {
         $subscription = wcs_get_subscription($subscription);
     }
     if (!is_object($renewal_order)) {
         $renewal_order = wc_get_order($renewal_order);
     }
     if (is_a($renewal_order, 'WC_Order') && wcs_is_subscription($subscription)) {
         // translators: placeholder is order number, hash before order number
         $order_number = sprintf(__('#%s', 'woocommerce-subscriptions'), $renewal_order->get_order_number());
         // translators: placeholder is order ID
         $subscription->add_order_note(sprintf(__('Order %s created to record renewal.', 'woocommerce-subscriptions'), sprintf('<a href="%s">%s</a> ', esc_url(wcs_get_edit_post_link($renewal_order->id)), $order_number)));
     }
     return $renewal_order;
 }
Example #21
0
 /**
  * The '_switched_subscription_key' and '_switched_subscription_new_order' post meta values are no longer used to relate orders
  * and switched subscriptions, instead, we need to set a '_subscription_switch' value on the switch order and depreacted the old
  * meta keys by prefixing them with '_wcs_migrated'.
  *
  * Subscriptions also sets a '_switched_subscription_item_id' value on the new line item of for the switched item and a item meta
  * value of '_switched_subscription_new_item_id' on the old line item on the subscription, but the old switching process didn't
  * change order items, it just created a new order with the new item, so we won't bother setting this as it is purely for record
  * keeping.
  *
  * @param WC_Subscription $new_subscription A subscription object
  * @param WC_Order $switch_order The original order used to purchase the subscription
  * @param int $subscription_item_id The order item ID of the item added to the subscription by self::add_product()
  * @return null
  * @since 2.0
  */
 private static function migrate_switch_meta($new_subscription, $switch_order, $subscription_item_id)
 {
     global $wpdb;
     // If the order doesn't contain a switch, we don't need to do anything
     if ('' == get_post_meta($switch_order->id, '_switched_subscription_key', true)) {
         return;
     }
     $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET `meta_key` = concat( '_wcs_migrated', `meta_key` )\n\t\t\tWHERE `post_id` = %d AND `meta_key` IN ('_switched_subscription_first_payment_timestamp','_switched_subscription_key')", $switch_order->id));
     // Select the orders which had the items which were switched by this order
     $previous_order_id = get_posts(array('post_type' => 'shop_order', 'post_status' => 'any', 'fields' => 'ids', 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_switched_subscription_new_order', 'value' => $switch_order->id))));
     if (!empty($previous_order_id)) {
         $previous_order_id = $previous_order_id[0];
         $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET `meta_key` = concat( '_wcs_migrated', `meta_key` )\n\t\t\t\tWHERE `post_id` = %d AND `meta_key` = '_switched_subscription_new_order'", $previous_order_id));
         // Because self::get_subscriptions() orders by order ID, it's safe to use wcs_get_subscriptions_for_order() here because the subscription in the new format will have been created for the original order (because its ID will be < the switch order's ID)
         $old_subscriptions = wcs_get_subscriptions_for_order($previous_order_id);
         $old_subscription = array_shift($old_subscriptions);
         // there can be only one
         if (wcs_is_subscription($old_subscription)) {
             // Link the old subscription's ID to the switch order using the new switch meta key
             update_post_meta($switch_order->id, '_subscription_switch', $old_subscription->id);
             // Now store the new/old item IDs for record keeping
             foreach ($old_subscription->get_items() as $item_id => $item) {
                 wc_add_order_item_meta($item_id, '_switched_subscription_new_item_id', $subscription_item_id, true);
                 wc_add_order_item_meta($subscription_item_id, '_switched_subscription_item_id', $item_id, true);
             }
             WCS_Upgrade_Logger::add(sprintf('For subscription %d: migrated switch data for subscription %d purchased in order %d', $new_subscription->id, $old_subscription->id, $previous_order_id));
         }
     }
 }
 /**
  * Add scripts
  */
 public function scripts()
 {
     $enqueue_scripts = is_cart() || is_checkout() || is_checkout_pay_page();
     if (!apply_filters('woocommerce_amazon_pa_enqueue_scripts', $enqueue_scripts)) {
         return;
     }
     $type = 'yes' == $this->settings['enable_login_app'] ? 'app' : 'standard';
     wp_enqueue_style('amazon_payments_advanced', plugins_url('assets/css/style.css', __FILE__));
     wp_enqueue_script('amazon_payments_advanced_widgets', WC_Amazon_Payments_Advanced_API::get_widgets_url(), array(), '1.0', true);
     wp_enqueue_script('amazon_payments_advanced', plugins_url('assets/js/amazon-' . $type . '-widgets.js', __FILE__), array(), '1.0', true);
     $redirect_page = is_cart() ? add_query_arg('amazon_payments_advanced', 'true', get_permalink(woocommerce_get_page_id('checkout'))) : add_query_arg('amazon_payments_advanced', 'true');
     $params = array('seller_id' => $this->settings['seller_id'], 'reference_id' => $this->reference_id, 'redirect' => esc_url_raw($redirect_page), 'is_checkout_pay_page' => is_checkout_pay_page(), 'is_checkout' => is_checkout(), 'access_token' => $this->access_token);
     if ('yes' == $this->settings['enable_login_app']) {
         $params['button_type'] = 'LwA';
         $params['button_color'] = 'Gold';
         $params['button_size'] = 'small';
         $params['checkout_url'] = esc_url_raw(get_permalink(woocommerce_get_page_id('checkout')));
     }
     if (class_exists('WC_Subscriptions_Cart')) {
         $cart_contains_subscription = WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_renewal();
         $change_payment_for_subscription = isset($_GET['change_payment_method']) && wcs_is_subscription(absint($_GET['change_payment_method']));
         $params['is_recurring'] = $cart_contains_subscription || $change_payment_for_subscription;
     }
     $params = array_map('esc_js', apply_filters('woocommerce_amazon_pa_widgets_params', $params));
     wp_localize_script('amazon_payments_advanced', 'amazon_payments_advanced_params', $params);
 }
 /**
  * Process the payment.
  *
  * @param  int $order_id
  * @return array
  */
 public function process_payment($order_id)
 {
     $cart_token = isset($_POST['simplify_token']) ? wc_clean($_POST['simplify_token']) : '';
     $order = wc_get_order($order_id);
     // Processing subscription
     if ('standard' == $this->mode && ($this->order_contains_subscription($order->id) || function_exists('wcs_is_subscription') && wcs_is_subscription($order_id))) {
         return $this->process_subscription($order, $cart_token);
         // Processing pre-order
     } elseif ('standard' == $this->mode && $this->order_contains_pre_order($order->id)) {
         return $this->process_pre_order($order, $cart_token);
         // Processing regular product
     } else {
         return parent::process_payment($order_id);
     }
 }
 /**
  * Removes order related emails from the available actions.
  *
  * @param array $available_emails
  * @since 2.0
  */
 public static function remove_order_email_actions($email_actions)
 {
     global $theorder;
     if (wcs_is_subscription($theorder)) {
         $email_actions = array();
     }
     return $email_actions;
 }
 /**
  * Prints link to the PayPal's profile related to the provided subscription
  *
  * @param WC_Subscription $subscription
  */
 public static function profile_link($subscription)
 {
     if (wcs_is_subscription($subscription) && 'paypal' == $subscription->payment_method) {
         $paypal_profile_id = wcs_get_paypal_id($subscription);
         if (!empty($paypal_profile_id)) {
             $url = '';
             if (false === wcs_is_paypal_profile_a($paypal_profile_id, 'billing_agreement')) {
                 // Standard subscription
                 $url = 'https://www.paypal.com/?cmd=_profile-recurring-payments&encrypted_profile_id=' . $paypal_profile_id;
             } else {
                 if (wcs_is_paypal_profile_a($paypal_profile_id, 'billing_agreement')) {
                     // Reference Transaction subscription
                     $url = 'https://www.paypal.com/?cmd=_profile-merchant-pull&encrypted_profile_id=' . $paypal_profile_id . '&mp_id=' . $paypal_profile_id . '&return_to=merchant&flag_flow=merchant';
                 }
             }
             echo '<div class="address">';
             echo '<p class="paypal_subscription_info"><strong>';
             echo esc_html(__('PayPal Subscription ID:', 'woocommerce-subscriptions'));
             echo '</strong>';
             if (!empty($url)) {
                 echo '<a href="' . esc_url($url) . '" target="_blank">' . esc_html($paypal_profile_id) . '</a>';
             } else {
                 echo esc_html($paypal_profile_id);
             }
             echo '</p></div>';
         }
     }
 }
 /**
  * Process the payment
  *
  * @param  int $order_id
  * @return array
  */
 public function process_payment($order_id, $retry = true)
 {
     // Processing subscription
     if (function_exists('wcs_order_contains_subscription') && (wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id))) {
         return $this->process_subscription($order_id, $retry);
         // Processing pre-order
     } elseif (class_exists('WC_Pre_Orders_Order') && WC_Pre_Orders_Order::order_contains_pre_order($order_id)) {
         return $this->process_pre_order($order_id, $retry);
         // Processing regular product
     } else {
         return parent::process_payment($order_id, $retry);
     }
 }