/**
  * Customise which actions are shown against a subscriptions order on the My Account page.
  *
  * @since 1.3
  */
 public static function filter_woocommerce_my_account_my_orders_actions($actions, $order)
 {
     if (WC_Subscriptions_Order::order_contains_subscription($order) || WC_Subscriptions_Renewal_Order::is_renewal($order)) {
         unset($actions['cancel']);
         if (is_numeric(get_post_meta($order->id, '_failed_order_replaced_by', true))) {
             unset($actions['pay']);
         }
         $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order);
         $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
         $first_order_item = reset($order_items);
         $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
         $subscription_key = WC_Subscriptions_Manager::get_subscription_key($original_order->id, $product_id);
         $subscription = WC_Subscriptions_Manager::get_users_subscription($original_order->customer_user, $subscription_key);
         if (empty($subscription) || !in_array($subscription['status'], array('on-hold', 'pending'))) {
             unset($actions['pay']);
         }
     }
     return $actions;
 }
Ejemplo n.º 2
0
 /**
  * Returns the string key for a subscription purchased in an order specified by $order_id
  * 
  * @param order_id int The ID of the order in which the subscription was purchased. 
  * @param product_id int The ID of the subscription product.
  * @return string The key representing the given subscription.
  * @since 1.0
  */
 public static function get_subscription_key($order_id, $product_id = '')
 {
     // If we have a child renewal order, we need the parent order's ID
     if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'child'))) {
         $order_id = WC_Subscriptions_Renewal_Order::get_parent_order_id($order_id);
     }
     if (empty($product_id)) {
         $order = new WC_Order($order_id);
         $order_items = WC_Subscriptions_Order::get_recurring_items($order);
         $first_order_item = reset($order_items);
         $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
     }
     $subscription_key = $order_id . '_' . $product_id;
     return apply_filters('woocommerce_subscription_key', $subscription_key, $order_id, $product_id);
 }
 /**
  * Returns the string key for a subscription purchased in an order specified by $order_id
  * 
  * @param order_id int The ID of the order in which the subscription was purchased. 
  * @param product_id int The ID of the subscription product.
  * @return string The key representing the given subscription.
  * @since 1.0
  */
 public static function get_subscription_key($order_id, $product_id = '')
 {
     // If we have a child renewal order, we need the parent order's ID
     if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, 'child')) {
         $order_id = WC_Subscriptions_Renewal_Order::get_parent_order_id($order_id);
     }
     if (empty($product_id)) {
         $order = new WC_Order($order_id);
         $order_items = $order->get_items();
         $product_id = $order_items[0]['id'];
     }
     $subscription_key = $order_id . '_' . $product_id;
     return apply_filters('woocommerce_subscription_key', $subscription_key, $order_id, $product_id);
 }
 /**
  * If viewing a renewal order on the the Edit Order screen, set the available email actions for the order to use
  * renewal order emails, not core WooCommerce order emails.
  *
  * @param int $user_id The ID of the user who the subscription belongs to
  * @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
  * @return void
  */
 public static function renewal_order_emails_available($available_emails)
 {
     global $theorder;
     if (WC_Subscriptions_Renewal_Order::is_renewal($theorder->id, array('order_role' => 'child'))) {
         $available_emails = array('new_renewal_order', 'customer_processing_renewal_order', 'customer_completed_renewal_order', 'customer_renewal_invoice');
     }
     return $available_emails;
 }
Ejemplo n.º 5
0
 /**
  * Outputs the contents of the "Renewal Orders" meta box.
  *
  * @param object $post Current post data.
  */
 public static function renewal_orders_meta_box($post)
 {
     $order = new WC_Order(absint($post->ID));
     if (WC_Subscriptions_Renewal_Order::is_renewal($order, array('order_role' => 'child'))) {
         $parent_id = WC_Subscriptions_Renewal_Order::get_parent_order_id($order);
     } else {
         if (WC_Subscriptions_Order::order_contains_subscription($order)) {
             $parent_id = $post->ID;
         }
     }
     //Find any renewal orders associated with this order.
     $items = get_posts(array('post_type' => $post->post_type, 'post_parent' => $parent_id, 'numberposts' => -1));
     if (WC_Subscriptions_Renewal_Order::is_renewal($order, array('order_role' => 'child'))) {
         $parent_order = new WC_Order($parent_id);
         printf('<p>%1$s <a href="%2$s">%3$s</a></p>', __('Initial Order:', WC_Subscriptions::$text_domain), get_edit_post_link($parent_id), $parent_order->get_order_number());
     }
     if (empty($items)) {
         printf(' <p class="renewal-subtitle">%s</p>', __('No renewal payments yet.', WC_Subscriptions::$text_domain));
     } else {
         printf('<p class="renewal-subtitle">%s</p>', __('Renewal Orders:', WC_Subscriptions::$text_domain));
         echo '<ul class="renewal-orders">';
         foreach ($items as $item) {
             $renewal_order = new WC_Order($item->ID);
             if ($item->ID == $post->ID) {
                 printf('<li><strong>%s</strong></li>', $renewal_order->get_order_number());
             } else {
                 printf('<li><a href="%1$s">%2$s</a></li>', get_edit_post_link($item->ID), $renewal_order->get_order_number());
             }
         }
         echo '</ul>';
     }
 }
Ejemplo n.º 6
0
 /**
  * Registers the "Renewal Orders" meta box for the "Edit Order" page.
  */
 public static function add_related_orders_meta_box()
 {
     global $current_screen, $post_id;
     // Only display the meta box if an order relates to a subscription
     if ('shop_order' == $current_screen->id && (WC_Subscriptions_Renewal_Order::is_renewal($post_id, array('order_role' => 'child')) || WC_Subscriptions_Order::order_contains_subscription($post_id))) {
         add_meta_box('subscription_renewal_orders', __('Related Subscription Orders', WC_Subscriptions::$text_domain), __CLASS__ . '::related_orders_meta_box', 'shop_order');
     }
 }
 /**
  * Check if a given order is a subscription renewal order
  *
  * @param WC_Order|int $order The WC_Order object or ID of a WC_Order order.
  * @deprecated 1.2
  * @since 1.0
  */
 public static function is_renewal($order)
 {
     _deprecated_function(__METHOD__, '1.2', 'WC_Subscriptions_Renewal_Order::is_renewal( $order )');
     return WC_Subscriptions_Renewal_Order::is_renewal($order);
 }
 /**
  * Check if a payment is being made on a failed renewal order from 'My Account'. If so,
  * redirect the order into a cart/checkout payment flow.
  *
  * @since 1.3
  */
 public static function before_woocommerce_pay()
 {
     global $woocommerce;
     if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) {
         // Pay for existing order
         $order_key = urldecode($_GET['order']);
         $order_id = absint($_GET['order_id']);
         $order = new WC_Order($order_id);
         $failed_order_replaced_by = get_post_meta($order_id, '_failed_order_replaced_by', true);
         if (is_numeric($failed_order_replaced_by)) {
             $woocommerce->add_error(sprintf(__('Sorry, this failed order has already been paid. See order %s.', WC_Subscriptions::$text_domain), $failed_order_replaced_by));
             wp_safe_redirect(get_permalink(woocommerce_get_page_id('myaccount')));
             exit;
         }
         if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed')) && WC_Subscriptions_Renewal_Order::is_renewal($order)) {
             // If order being paid is a parent order, get the original order, else query parent_order
             if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'parent'))) {
                 $role = 'parent';
                 $original_order = new WC_Order($order->order_custom_fields['_original_order'][0]);
             } elseif (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'child'))) {
                 $role = 'child';
                 $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order_id);
             }
             $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
             $first_order_item = reset($order_items);
             $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
             $product = get_product($product_id);
             // Make sure we don't actually need the variation ID
             if ($product->is_type(array('variable-subscription'))) {
                 $item = WC_Subscriptions_Order::get_item_by_product_id($original_order, $product_id);
                 $variation_id = $item['variation_id'];
                 $variation = get_product($variation_id);
                 $variation_data = $variation->get_variation_attributes();
             } elseif ($product->is_type(array('subscription_variation'))) {
                 // Handle existing renewal orders incorrectly using variation_id as the product_id
                 $product_id = $product->id;
                 $variation_id = $product->get_variation_id();
                 $variation_data = $product->get_variation_attributes();
             } else {
                 $variation_id = '';
                 $variation_data = array();
             }
             $woocommerce->cart->empty_cart(true);
             $woocommerce->cart->add_to_cart($product_id, 1, $variation_id, $variation_data, array('subscription_renewal' => array('original_order' => $original_order->id, 'failed_order' => $order_id, 'role' => $role)));
             wp_safe_redirect($woocommerce->cart->get_checkout_url());
             exit;
         }
     }
 }
 /**
  * Check if a payment is being made on a failed renewal order from 'My Account'. If so,
  * redirect the order into a cart/checkout payment flow.
  *
  * @since 1.3
  */
 public static function before_woocommerce_pay()
 {
     global $woocommerce, $wp;
     if (isset($_GET['pay_for_order']) && (isset($_GET['order']) && isset($_GET['order_id']) || isset($_GET['key']) && isset($wp->query_vars['order-pay']))) {
         // Pay for existing order
         $order_key = isset($_GET['key']) ? $_GET['key'] : $_GET['order'];
         // WC 2.1 compatibility
         $order_id = isset($wp->query_vars['order-pay']) ? $wp->query_vars['order-pay'] : absint($_GET['order_id']);
         $order = new WC_Order($order_id);
         $failed_order_replaced_by = get_post_meta($order_id, '_failed_order_replaced_by', true);
         if (is_numeric($failed_order_replaced_by)) {
             WC_Subscriptions::add_notice(sprintf(__('Sorry, this failed order has already been paid. See order %s.', 'woocommerce-subscriptions'), $failed_order_replaced_by), 'error');
             wp_safe_redirect(get_permalink(woocommerce_get_page_id('myaccount')));
             exit;
         }
         if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed')) && WC_Subscriptions_Renewal_Order::is_renewal($order)) {
             // If order being paid is a parent order, get the original order, else query parent_order
             if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'parent'))) {
                 $role = 'parent';
                 $original_order = new WC_Order(WC_Subscriptions_Order::get_meta($order, 'original_order', false));
             } elseif (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'child'))) {
                 $role = 'child';
                 $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order_id);
             }
             $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
             $first_order_item = reset($order_items);
             $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
             $product = get_product($product_id);
             $variation_id = '';
             $variation_data = array();
             // Display error message for deleted products
             if (false === $product) {
                 WC_Subscriptions::add_notice(self::$product_deleted_error_message, 'error');
                 // Make sure we don't actually need the variation ID
             } elseif ($product->is_type(array('variable-subscription'))) {
                 $item = WC_Subscriptions_Order::get_item_by_product_id($original_order, $product_id);
                 $variation_id = $item['variation_id'];
                 $variation = get_product($variation_id);
                 // Display error message for deleted product variations
                 if (false === $variation) {
                     WC_Subscriptions::add_notice(self::$product_deleted_error_message, 'error');
                     $variation_data = array();
                 } else {
                     $variation_data = $variation->get_variation_attributes();
                 }
             } elseif ($product->is_type(array('subscription_variation'))) {
                 // Handle existing renewal orders incorrectly using variation_id as the product_id
                 $product_id = $product->id;
                 $variation_id = $product->get_variation_id();
                 $variation_data = $product->get_variation_attributes();
             }
             $woocommerce->cart->empty_cart(true);
             $woocommerce->cart->add_to_cart($product_id, 1, $variation_id, $variation_data, array('subscription_renewal' => array('original_order' => $original_order->id, 'failed_order' => $order_id, 'role' => $role)));
             wp_safe_redirect($woocommerce->cart->get_checkout_url());
             exit;
         }
     }
 }
 /**
  * Check if a given order is a subscription renewal order
  * 
  * @param $order WC_Order | int The WC_Order object or ID of a WC_Order order.
  * @deprecated 1.2
  * @since 1.0
  */
 public static function is_renewal($order)
 {
     _deprecated_function(__CLASS__ . '::' . __FUNCTION__, '1.2', 'WC_Subscriptions_Renewal_Order::is_renewal( $order )');
     return WC_Subscriptions_Renewal_Order::is_renewal($order);
 }
 /**
  * Registers the "Renewal Orders" meta box for the "Edit Order" page.
  */
 public static function add_meta_boxes()
 {
     global $current_screen, $post_id;
     // Only display the meta box if an order relates to a subscription
     if ('shop_order' == $current_screen->id) {
         $order_contains_subscription = WC_Subscriptions_Order::order_contains_subscription($post_id);
         if ($order_contains_subscription || WC_Subscriptions_Renewal_Order::is_renewal($post_id, array('order_role' => 'child'))) {
             add_meta_box('subscription_renewal_orders', __('Related Subscription Orders', 'woocommerce-subscriptions'), __CLASS__ . '::related_orders_meta_box', 'shop_order', 'normal', 'default');
         }
         if ($order_contains_subscription || 'add' == $current_screen->action) {
             if (!WC_Subscriptions::is_woocommerce_pre_2_2()) {
                 add_meta_box('woocommerce-order-totals', __('Recurring Totals', 'woocommerce-subscriptions'), __CLASS__ . '::recurring_totals_meta_box', 'shop_order', 'side', 'high');
             } else {
                 // WC 2.1 compatibility
                 add_filter('woocommerce_admin_order_totals_after_shipping', 'WC_Subscriptions_Order::recurring_order_totals_meta_box_section', 100, 1);
             }
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * Activates a Klarna order for V2 API
  *
  * @since  2.0
  **/
 function activate_order($orderid)
 {
     $order = wc_get_order($orderid);
     if (get_post_meta($orderid, '_klarna_order_reservation', true) && get_post_meta($orderid, '_billing_country', true)) {
         // Check if this is a subscription order
         if (class_exists('WC_Subscriptions_Renewal_Order') && WC_Subscriptions_Renewal_Order::is_renewal($order)) {
             if (!get_post_meta($orderid, '_klarna_order_reservation_recurring', true)) {
                 return;
             }
         }
         $rno = get_post_meta($orderid, '_klarna_order_reservation', true);
         $country = get_post_meta($orderid, '_billing_country', true);
         $payment_method = get_post_meta($orderid, '_payment_method', true);
         $klarna = new Klarna();
         $this->configure_klarna($klarna, $country, $payment_method);
         try {
             $result = $klarna->activate($rno, null, KlarnaFlags::RSRV_SEND_BY_EMAIL);
             $risk = $result[0];
             // returns 'ok' or 'no_risk'
             $invNo = $result[1];
             // returns invoice number
             $order->add_order_note(sprintf(__('Klarna order activated. Invoice number %s - risk status %s.', 'woocommerce-gateway-klarna'), $invNo, $risk));
             update_post_meta($orderid, '_klarna_order_activated', time());
             update_post_meta($orderid, '_klarna_invoice_number', $invNo);
             update_post_meta($orderid, '_transaction_id', $invNo);
         } catch (Exception $e) {
             $order->add_order_note(sprintf(__('Klarna order activation failed. Error code %s. Error message %s', 'woocommerce-gateway-klarna'), $e->getCode(), utf8_encode($e->getMessage())));
         }
     }
 }
 /**
  * Check if order contains subscriptions.
  *
  * @param  int $order_id
  * @return bool
  */
 protected function order_contains_subscription($order_id)
 {
     return class_exists('WC_Subscriptions_Order') && (WC_Subscriptions_Order::order_contains_subscription($order_id) || WC_Subscriptions_Renewal_Order::is_renewal($order_id));
 }