本文整理汇总了PHP中WC_Subscriptions_Order类的典型用法代码示例。如果您正苦于以下问题:PHP WC_Subscriptions_Order类的具体用法?PHP WC_Subscriptions_Order怎么用?PHP WC_Subscriptions_Order使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WC_Subscriptions_Order类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: add_order_meta
/**
* When a new order is inserted, add the subscriptions period to the order.
*
* It's important that the period is tied to the order so that changing the products
* period does not change the past.
*
* @since 1.0
*/
public static function add_order_meta($order_id)
{
global $woocommerce;
if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
$order = new WC_Order($order_id);
$order_subscription_periods = array();
$order_subscription_intervals = array();
$order_subscription_lengths = array();
$order_subscription_trial_lengths = array();
foreach ($order->get_items() as $item) {
$period = WC_Subscriptions_Product::get_period($item['id']);
if (!empty($period)) {
$order_subscription_periods[$item['id']] = $period;
}
$interval = WC_Subscriptions_Product::get_interval($item['id']);
if (!empty($interval)) {
$order_subscription_intervals[$item['id']] = $interval;
}
$length = WC_Subscriptions_Product::get_length($item['id']);
if (!empty($length)) {
$order_subscription_lengths[$item['id']] = $length;
}
$trial_length = WC_Subscriptions_Product::get_trial_length($item['id']);
if (!empty($trial_length)) {
$order_subscription_trial_lengths[$item['id']] = $trial_length;
}
}
update_post_meta($order_id, '_order_subscription_periods', $order_subscription_periods);
update_post_meta($order_id, '_order_subscription_intervals', $order_subscription_intervals);
update_post_meta($order_id, '_order_subscription_lengths', $order_subscription_lengths);
update_post_meta($order_id, '_order_subscription_trial_lengths', $order_subscription_trial_lengths);
// Store sign-up fee details
foreach (WC_Subscriptions_Cart::get_sign_up_fee_fields() as $field_name) {
update_post_meta($order_id, "_{$field_name}", $woocommerce->cart->{$field_name});
}
// Prepare sign up fee taxes to store in same format as order taxes
$sign_up_fee_taxes = array();
foreach (array_keys($woocommerce->cart->sign_up_fee_taxes) as $key) {
$is_compound = $woocommerce->cart->tax->is_compound($key) ? 1 : 0;
$sign_up_fee_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($key), 'compound' => $is_compound, 'cart_tax' => number_format($woocommerce->cart->sign_up_fee_taxes[$key], 2, '.', ''));
}
update_post_meta($order_id, '_sign_up_fee_taxes', $sign_up_fee_taxes);
}
}
示例2: maybe_remove_customer_processing_order
/**
* Removes a couple of notifications that are less relevant for Subscription orders.
*
* @since 1.0
*/
public static function maybe_remove_customer_processing_order($order_id)
{
if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
remove_action('woocommerce_order_status_pending_to_processing_notification', array(self::$woocommerce_email, 'customer_processing_order'));
remove_action('woocommerce_order_status_pending_to_on-hold_notification', array(self::$woocommerce_email, 'customer_processing_order'));
}
}
示例3: add_order_meta
/**
* When a new order is inserted, add the subscriptions period to the order.
*
* It's important that the period is tied to the order so that changing the products
* period does not change the past.
*
* @since 1.0
*/
public static function add_order_meta($order_id, $posted)
{
global $woocommerce;
if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
// This works because the 'new_order_item' runs before the 'woocommerce_checkout_update_order_meta' hook
// Set the recurring totals so totals display correctly on order page
update_post_meta($order_id, '_order_recurring_discount_cart', WC_Subscriptions_Cart::get_recurring_discount_cart());
update_post_meta($order_id, '_order_recurring_discount_total', WC_Subscriptions_Cart::get_recurring_discount_total());
update_post_meta($order_id, '_order_recurring_shipping_tax_total', WC_Subscriptions_Cart::get_recurring_shipping_tax_total());
update_post_meta($order_id, '_order_recurring_tax_total', WC_Subscriptions_Cart::get_recurring_total_tax());
update_post_meta($order_id, '_order_recurring_total', WC_Subscriptions_Cart::get_recurring_total());
// Get recurring taxes into same format as _order_taxes
$order_recurring_taxes = array();
foreach (WC_Subscriptions_Cart::get_recurring_taxes() as $tax_key => $tax_amount) {
$is_compound = $woocommerce->cart->tax->is_compound($tax_key) ? 1 : 0;
if (isset($woocommerce->cart->taxes[$tax_key])) {
$cart_tax = $tax_amount;
$shipping_tax = 0;
} else {
$cart_tax = 0;
$shipping_tax = $tax_amount;
}
$order_recurring_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($tax_key), 'compound' => $is_compound, 'cart_tax' => woocommerce_format_total($cart_tax), 'shipping_tax' => woocommerce_format_total($shipping_tax));
}
update_post_meta($order_id, '_order_recurring_taxes', $order_recurring_taxes);
$payment_gateways = $woocommerce->payment_gateways->payment_gateways();
if (!$payment_gateways[$posted['payment_method']]->supports('subscriptions')) {
update_post_meta($order_id, '_wcs_requires_manual_renewal', 'true');
}
}
}
示例4: process_payment
/**
* Process the payment
*/
function process_payment($order_id)
{
global $woocommerce;
if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
$order = new WC_Order($order_id);
$stripe_token = isset($_POST['stripe_token']) ? woocommerce_clean($_POST['stripe_token']) : '';
// Use Stripe CURL API for payment
try {
$post_data = array();
$customer_id = 0;
// Check if paying via customer ID
if (isset($_POST['stripe_customer_id']) && $_POST['stripe_customer_id'] !== 'new' && is_user_logged_in()) {
$customer_ids = get_user_meta(get_current_user_id(), '_stripe_customer_id', false);
if (isset($customer_ids[$_POST['stripe_customer_id']]['customer_id'])) {
$customer_id = $customer_ids[$_POST['stripe_customer_id']]['customer_id'];
} else {
throw new Exception(__('Invalid card.', 'wc_stripe'));
}
} elseif (empty($stripe_token)) {
throw new Exception(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'wc_stripe'));
}
if (method_exists('WC_Subscriptions_Order', 'get_total_initial_payment')) {
$initial_payment = WC_Subscriptions_Order::get_total_initial_payment($order);
} else {
$initial_payment = WC_Subscriptions_Order::get_sign_up_fee($order) + WC_Subscriptions_Order::get_price_per_period($order);
}
$customer_response = $this->add_customer_to_order($order, $customer_id, $stripe_token);
if ($initial_payment > 0) {
$payment_response = $this->process_subscription_payment($order, $initial_payment);
}
if (is_wp_error($customer_response)) {
throw new Exception($customer_response->get_error_message());
} else {
if (isset($payment_response) && is_wp_error($payment_response)) {
throw new Exception($payment_response->get_error_message());
} else {
// Payment complete
$order->payment_complete();
// Remove cart
$woocommerce->cart->empty_cart();
// Activate subscriptions
WC_Subscriptions_Manager::activate_subscriptions_for_order($order);
// Store token
if ($stripe_token) {
update_post_meta($order->id, '_stripe_token', $stripe_token);
}
// Return thank you page redirect
return array('result' => 'success', 'redirect' => $this->get_return_url($order));
}
}
} catch (Exception $e) {
$woocommerce->add_error(__('Error:', 'wc_stripe') . ' "' . $e->getMessage() . '"');
return;
}
} else {
return parent::process_payment($order_id);
}
}
示例5: wcs_get_old_subscription_key
/**
* Get the string key for a subscription used in Subscriptions prior to 2.0.
*
* Previously, a subscription key was made up of the ID of the order used to purchase the subscription, and
* the product to which the subscription relates; however, in Subscriptions 2.0, subscriptions can actually
* relate to multiple products (because they can contain multiple line items) and they also no longer need
* to have an original order associated with them, to make manually adding subscriptions more accurate.
*
* Therefore, although the return value of this method is a string matching the key form used inSubscriptions
* prior to 2.0, the actual value represented is not a perfect analogue. Specifically,
* - if the subscription contains more than one product, only the ID of the first line item will be used in the ID
* - if the subscription does not contain any products, the key still be missing that component of the
* - if the subscription does not have an initial order, then the order ID used will be the WC_Subscription object's ID
*
* @param WC_Subscription $subscription An instance of WC_Subscription
* @return string $subscription_key A subscription key in the deprecated form previously created by @see self::get_subscription_key()
* @since 2.0
*/
function wcs_get_old_subscription_key(WC_Subscription $subscription)
{
// Get an ID to use as the order ID
$order_id = isset($subscription->order->id) ? $subscription->order->id : $subscription->id;
// Get an ID to use as the product ID
$subscription_items = $subscription->get_items();
$first_item = reset($subscription_items);
return $order_id . '_' . WC_Subscriptions_Order::get_items_product_id($first_item);
}
示例6: process_payment
/**
* Process the payment
*
* @param int $order_id
* @return array
*/
public function process_payment($order_id, $retry = true)
{
// Processing subscription
if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
return $this->process_subscription($order_id, $retry);
// Processing pre-order or standard ordre
} else {
return parent::process_payment($order_id, $retry);
}
}
示例7: gateway_scheduled_subscription_payment
/**
* For versions of WooCommerce prior to the existence of the woocommerce_available_gateways,
* hide available gateways with JavaScript.
*
* @since 1.0
*/
public static function gateway_scheduled_subscription_payment($user_id, $subscription_key)
{
$subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
$order = new WC_Order($subscription['order_id']);
$amount_to_charge = WC_Subscriptions_Order::get_price_per_period($order, $subscription['product_id']);
$outstanding_payments = WC_Subscriptions_Order::get_outstanding_balance($order, $subscription['product_id']);
if ($outstanding_payments > 0) {
$amount_to_charge += $outstanding_payments;
}
do_action('scheduled_subscription_payment_' . $order->payment_method, $amount_to_charge, $order, $subscription['product_id']);
}
示例8: change_users_subscription
/**
* Change the status of a subscription and show a notice to the
* user to say when the next payment will now be made on.
*
* @since 1.0.0
* @access public
* @static
* @param $subscription
* @param $order_id
* @param $product_id
* @uses WC_Subscriptions::add_notice
*/
public static function change_users_subscription($subscription, $order_id, $product_id)
{
$subscription = !is_object($subscription) ? wcs_get_subscription($subscription) : $subscription;
$current_next_subscription_payment = WC_Subscriptions_Order::get_next_payment_date($order_id, $product_id, $from_date = '');
// Get the date of the current next subscription.
$next_subscription_payment = WC_Subscriptions_Order::calculate_next_payment_date($order_id, $product_id, $type = 'mysql', $current_next_subscription_payment);
WCSO_Subscriptions_Order::update_next_payment($order_id, $next_subscription_payment);
// Update the next payment on the subscriptions order.
$subscription->add_order_note(__('Subscription was skipped by the subscriber from their subscription page.', 'woocommerce-skip-one'));
// translators: placeholder is next subscription payment
WC_Subscriptions::add_notice(sprintf(__('You next payment subscription will now be on the %s.', 'woocommerce-skip-one'), $next_subscription_payment), 'success');
}
示例9: init
public function init()
{
global $wpdb;
$subscriptions_meta_key = $wpdb->get_blog_prefix() . 'woocommerce_subscriptions';
$order_items_table = $wpdb->get_blog_prefix() . 'woocommerce_order_items';
$order_item_meta_table = $wpdb->get_blog_prefix() . 'woocommerce_order_itemmeta';
// Get the IDs of all users who have a subscription
$users_to_upgrade = get_users(array('meta_key' => $subscriptions_meta_key, 'fields' => 'ID', 'orderby' => 'ID'));
$users_to_upgrade = array_filter($users_to_upgrade, __CLASS__ . '::is_user_upgraded');
foreach ($users_to_upgrade as $user_to_upgrade) {
// Can't use WC_Subscriptions_Manager::get_users_subscriptions() because it relies on the new structure
$users_old_subscriptions = get_user_option($subscriptions_meta_key, $user_to_upgrade);
foreach ($users_old_subscriptions as $subscription_key => $subscription) {
if (!isset($subscription['order_id'])) {
// Subscription created incorrectly with v1.1.2
continue;
}
$order_item_id = WC_Subscriptions_Order::get_item_id_by_subscription_key($subscription_key);
if (empty($order_item_id)) {
// Subscription created incorrectly with v1.1.2
continue;
}
if (!isset($subscription['trial_expiry_date'])) {
$subscription['trial_expiry_date'] = '';
}
// Set defaults
$failed_payments = isset($subscription['failed_payments']) ? $subscription['failed_payments'] : 0;
$completed_payments = isset($subscription['completed_payments']) ? $subscription['completed_payments'] : array();
$suspension_count = isset($subscription['suspension_count']) ? $subscription['suspension_count'] : 0;
$trial_expiry_date = isset($subscription['trial_expiry_date']) ? $subscription['trial_expiry_date'] : '';
$wpdb->query($wpdb->prepare("INSERT INTO {$order_item_meta_table} (order_item_id, meta_key, meta_value)\n\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s),\n\t\t\t\t\t\t(%d,%s,%s)", $order_item_id, '_subscription_status', $subscription['status'], $order_item_id, '_subscription_start_date', $subscription['start_date'], $order_item_id, '_subscription_expiry_date', $subscription['expiry_date'], $order_item_id, '_subscription_end_date', $subscription['end_date'], $order_item_id, '_subscription_trial_expiry_date', $trial_expiry_date, $order_item_id, '_subscription_failed_payments', $failed_payments, $order_item_id, '_subscription_completed_payments', serialize($completed_payments), $order_item_id, '_subscription_suspension_count', $suspension_count));
}
update_option('wcs_1_4_last_upgraded_user_id', $user_to_upgrade);
self::$last_upgraded_user_id = $user_to_upgrade;
}
// Add an underscore prefix to usermeta key to deprecate, but not delete, subscriptions in user meta
$wpdb->update($wpdb->usermeta, array('meta_key' => '_' . $subscriptions_meta_key), array('meta_key' => $subscriptions_meta_key));
// Now set the recurring shipping & payment method on all subscription orders
$wpdb->query("INSERT INTO {$wpdb->postmeta} (`post_id`, `meta_key`, `meta_value`)\n\t\t\tSELECT `post_id`, CONCAT('_recurring',`meta_key`), `meta_value`\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE `meta_key` IN ('_shipping_method','_shipping_method_title','_payment_method','_payment_method_title')\n\t\t\tAND `post_id` IN (\n\t\t\t\tSELECT `post_id` FROM {$wpdb->postmeta} WHERE `meta_key` = '_order_recurring_total'\n\t\t\t)");
// Set the recurring shipping total on all subscription orders
$wpdb->query("INSERT INTO {$wpdb->postmeta} (`post_id`, `meta_key`, `meta_value`)\n\t\t\tSELECT `post_id`, '_order_recurring_shipping_total', `meta_value`\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE `meta_key` = '_order_shipping'\n\t\t\tAND `post_id` IN (\n\t\t\t\tSELECT `post_id` FROM {$wpdb->postmeta} WHERE `meta_key` = '_order_recurring_total'\n\t\t\t)");
// Get the ID of all orders for a subscription with a free trial and no sign-up fee
$order_ids = $wpdb->get_col("SELECT order_items.order_id FROM {$order_items_table} AS order_items\n\t\t\t\tLEFT JOIN {$order_item_meta_table} AS itemmeta USING (order_item_id)\n\t\t\t\tLEFT JOIN {$order_item_meta_table} AS itemmeta2 USING (order_item_id)\n\t\t\tWHERE itemmeta.meta_key = '_subscription_trial_length'\n\t\t\tAND itemmeta.meta_value > 0\n\t\t\tAND itemmeta2.meta_key = '_subscription_sign_up_fee'\n\t\t\tAND itemmeta2.meta_value > 0");
$order_ids = implode(',', array_map('absint', array_unique($order_ids)));
// Now set the order totals to $0 (can't use $wpdb->update as it only allows joining WHERE clauses with AND)
if (!empty($order_ids)) {
$wpdb->query("UPDATE {$wpdb->postmeta}\n\t\t\t\tSET `meta_value` = 0\n\t\t\t\tWHERE `meta_key` IN ( '_order_total', '_order_tax', '_order_shipping_tax', '_order_shipping', '_order_discount', '_cart_discount' )\n\t\t\t\tAND `post_id` IN ( {$order_ids} )");
// Now set the line totals to $0
$wpdb->query("UPDATE {$order_item_meta_table}\n\t\t\t\t SET `meta_value` = 0\n\t\t\t\t WHERE `meta_key` IN ( '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', 'tax_amount', 'shipping_tax_amount' )\n\t\t\t\t AND `order_item_id` IN (\n\t\t\t\t\tSELECT `order_item_id` FROM {$order_items_table}\n\t\t\t\t\tWHERE `order_item_type` IN ('tax','line_item')\n\t\t\t\t\tAND `order_id` IN ( {$order_ids} )\n\t\t\t\t)");
}
update_option('wcs_1_4_upgraded_order_ids', explode(',', $order_ids));
}
示例10: process_subscription
/**
* Process the subscription.
*
* @param WC_Order $order
*
* @return array
*/
protected function process_subscription($order_id)
{
try {
$order = new WC_Order($order_id);
if (!isset($_POST['iugu_token'])) {
if ('yes' == $this->debug) {
$this->log->add($this->id, 'Error doing the subscription for order ' . $order->get_order_number() . ': Missing the "iugu_token".');
}
$error_msg = __('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'iugu-woocommerce');
throw new Exception($error_msg);
}
// Create customer payment method.
$payment_method_id = $this->api->create_customer_payment_method($order, $_POST['iugu_token']);
if (!$payment_method_id) {
if ('yes' == $this->debug) {
$this->log->add($this->id, 'Invalid customer method ID for order ' . $order->get_order_number());
}
$error_msg = __('An error occurred while trying to save your data. Please contact us for get help.', 'iugu-woocommerce');
throw new Exception($error_msg);
}
// Save the payment method ID in order data.
update_post_meta($order->id, '_iugu_customer_payment_method_id', $payment_method_id);
// Try to do an initial payment.
$initial_payment = WC_Subscriptions_Order::get_total_initial_payment($order);
if ($initial_payment > 0) {
$payment_response = $this->process_subscription_payment($order, $initial_payment);
}
if (isset($payment_response) && is_wp_error($payment_response)) {
throw new Exception($payment_response->get_error_message());
} else {
// Remove cart
$this->api->empty_card();
// Return thank you page redirect
return array('result' => 'success', 'redirect' => $this->get_return_url($order));
}
} catch (Exception $e) {
$this->api->add_error('<strong>' . esc_attr($this->title) . '</strong>: ' . $e->getMessage());
return array('result' => 'fail', 'redirect' => '');
}
}
示例11: process_subscription
/**
* Process the subscription.
*
* @param WC_Order $order
*
* @return array
*/
protected function process_subscription($order_id)
{
try {
$order = new WC_Order($order_id);
// Try to do an initial payment.
$initial_payment = WC_Subscriptions_Order::get_total_initial_payment($order);
if ($initial_payment > 0) {
$payment_response = $this->process_subscription_payment($order, $initial_payment);
}
if (isset($payment_response) && is_wp_error($payment_response)) {
throw new Exception($payment_response->get_error_message());
} else {
// Remove cart
$this->api->empty_card();
// Return thank you page redirect
return array('result' => 'success', 'redirect' => $this->get_return_url($order));
}
} catch (Exception $e) {
$this->api->add_error('<strong>' . esc_attr($this->title) . '</strong>: ' . $e->getMessage());
return array('result' => 'fail', 'redirect' => '');
}
}
示例12: request_access_code
public function request_access_code($order, $method = 'ProcessPayment', $trx_type = 'Purchase')
{
$customer_ip = get_post_meta($order->id, '_customer_ip_address', true);
// Check if order is for a subscription, if it is check for fee and charge that
if (class_exists('WC_Subscriptions_Order') && WC_Subscriptions_Order::order_contains_subscription($order->id)) {
if (0 == WC_Subscriptions_Order::get_total_initial_payment($order)) {
$method = 'CreateTokenCustomer';
} else {
$method = 'TokenPayment';
}
$trx_type = 'Recurring';
$order_total = WC_Subscriptions_Order::get_total_initial_payment($order) * 100;
} else {
$order_total = $order->get_total() * 100.0;
}
// set up request object
$request = array('Method' => $method, 'TransactionType' => $trx_type, 'RedirectUrl' => str_replace('https:', 'http:', add_query_arg(array('wc-api' => 'WC_Gateway_EWAY', 'order_id' => $order->id, 'order_key' => $order->order_key, 'sig_key' => md5($order->order_key . 'WOO' . $order->id)), home_url('/'))), 'IPAddress' => $customer_ip, 'DeviceID' => '0b38ae7c3c5b466f8b234a8955f62bdd', 'PartnerID' => '0b38ae7c3c5b466f8b234a8955f62bdd', 'Payment' => array('TotalAmount' => $order_total, 'CurrencyCode' => get_woocommerce_currency(), 'InvoiceDescription' => apply_filters('woocommerce_eway_description', '', $order), 'InvoiceNumber' => ltrim($order->get_order_number(), _x('#', 'hash before order number', 'woocommerce')), 'InvoiceReference' => $order->id), 'Customer' => array('FirstName' => $order->billing_first_name, 'LastName' => $order->billing_last_name, 'CompanyName' => substr($order->billing_company, 0, 50), 'Street1' => $order->billing_address_1, 'Street2' => $order->billing_address_2, 'City' => $order->billing_city, 'State' => $order->billing_state, 'PostalCode' => $order->billing_postcode, 'Country' => $order->billing_country, 'Email' => $order->billing_email, 'Phone' => $order->billing_phone));
// Add customer ID if logged in
if (is_user_logged_in()) {
$request['Options'][] = array('customerID' => get_current_user_id());
}
return $this->perform_request('/CreateAccessCode.json', json_encode($request));
}
示例13: maybe_set_apporitioned_totals
/**
* Set the subscription prices to be used in calculating totals by @see WC_Subscriptions_Cart::calculate_subscription_totals()
*
* @since 1.4
*/
public static function maybe_set_apporitioned_totals($total)
{
global $woocommerce;
if (false === self::cart_contains_subscription_switch()) {
return $total;
}
// Maybe charge an initial amount to account for upgrading from a cheaper subscription
$apportion_recurring_price = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_recurring_price', 'no');
$apportion_sign_up_fee = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_sign_up_fee', 'no');
$apportion_length = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_length', 'no');
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
if (!isset($cart_item['subscription_switch']['subscription_key'])) {
continue;
}
$old_subscription_key = $cart_item['subscription_switch']['subscription_key'];
$item_data = $cart_item['data'];
$product_id = empty($cart_item['variation_id']) ? $cart_item['product_id'] : $cart_item['variation_id'];
$product = get_product($product_id);
// Set the date on which the first payment for the new subscription should be charged
$woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $cart_item['subscription_switch']['next_payment_timestamp'];
$is_virtual_product = 'no' != $item_data->virtual ? true : false;
// Add any extra sign up fees required to switch to the new subscription
if ('yes' == $apportion_sign_up_fee) {
$old_subscription = WC_Subscriptions_Manager::get_subscription($old_subscription_key);
$sign_up_fee_due = $product->subscription_sign_up_fee;
if (self::order_contains_subscription_switch($old_subscription['order_id'])) {
$old_order = new WC_Order($old_subscription['order_id']);
$sign_up_fee_paid = $old_order->get_total();
} else {
$sign_up_fee_paid = WC_Subscriptions_Order::get_item_sign_up_fee($old_subscription['order_id'], $old_subscription['product_id']);
}
$woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = max($sign_up_fee_due - $sign_up_fee_paid, 0);
} elseif ('no' == $apportion_sign_up_fee) {
// $0 the initial sign-up fee
$woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = 0;
}
// Now lets see if we should add an apportioned amount to the sign-up fee (for upgrades) or extend the next payment date (for downgrades)
if ('yes' == $apportion_recurring_price || 'virtual' == $apportion_recurring_price && $is_virtual_product) {
// Get the current subscription
$old_subscription = isset($old_subscription) ? $old_subscription : WC_Subscriptions_Manager::get_subscription($old_subscription_key);
// Get the current subscription's original order
$old_order = isset($old_order) ? $old_order : new WC_Order($old_subscription['order_id']);
// Get the current subscription's last payment date
$last_payment_timestamp = WC_Subscriptions_Manager::get_last_payment_date($old_subscription_key, get_current_user_id(), 'timestamp');
$days_since_last_payment = round((gmdate('U') - $last_payment_timestamp) / (60 * 60 * 24));
// Get the current subscription's next payment date
$next_payment_timestamp = $cart_item['subscription_switch']['next_payment_timestamp'];
$days_until_next_payment = round(($next_payment_timestamp - gmdate('U')) / (60 * 60 * 24));
// Find the number of days between the two
$days_in_old_cycle = $days_until_next_payment + $days_since_last_payment;
// Find the $price per day for the old subscription's recurring total
$old_recurring_total = WC_Subscriptions_Order::get_item_recurring_amount($old_subscription['order_id'], $old_subscription['product_id']);
$old_price_per_day = $old_recurring_total / $days_in_old_cycle;
// Find the price per day for the new subscription's recurring total
// If the subscription uses the same billing interval & cycle as the old subscription,
if ($item_data->subscription_period == $old_subscription['period'] && $item_data->subscription_period_interval == $old_subscription['interval']) {
$days_in_new_cycle = $days_in_old_cycle;
// Use $days_in_old_cycle to make sure they're consistent
$new_price_per_day = $product->subscription_price / $days_in_old_cycle;
} else {
// We need to figure out the price per day for the new subscription based on its billing schedule
switch ($item_data->subscription_period) {
case 'day':
$days_in_new_cycle = $item_data->subscription_period_interval;
break;
case 'week':
$days_in_new_cycle = $item_data->subscription_period_interval * 7;
break;
case 'month':
$days_in_new_cycle = $item_data->subscription_period_interval * 30.4375;
// Average days per month over 4 year period
break;
case 'year':
$days_in_new_cycle = $item_data->subscription_period_interval * 365.25;
// Average days per year over 4 year period
break;
}
$new_price_per_day = $product->subscription_price / $days_in_new_cycle;
}
// If the customer is upgrading, we may need to add a gap payment to the sign-up fee or to reduce the pre-paid period (or both)
if ($old_price_per_day < $new_price_per_day) {
// The new subscription may be more expensive, but it's also on a shorter billing cycle, so reduce the next pre-paid term
if ($days_in_old_cycle > $days_in_new_cycle) {
// Find out how many days at the new price per day the customer would receive for the total amount already paid
// (e.g. if the customer paid $10 / month previously, and was switching to a $5 / week subscription, she has pre-paid 14 days at the new price)
$pre_paid_days = 0;
do {
$pre_paid_days++;
$new_total_paid = $pre_paid_days * $new_price_per_day;
} while ($new_total_paid < $old_recurring_total);
// If the total amount the customer has paid entitles her to more days at the new price than she has received, there is no gap payment, just shorten the pre-paid term the appropriate number of days
if ($days_since_last_payment < $pre_paid_days) {
$woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $last_payment_timestamp + $pre_paid_days * 60 * 60 * 24;
// If the total amount the customer has paid entitles her to the same or less days at the new price then start the new subscription from today
} else {
//.........这里部分代码省略.........
示例14: do_subscriptions_shortcode
/**
* Callback for the [subscriptions] shortcode that displays subscription names for a particular user.
*
* @param array $attributes Shortcode attributes.
* @return string
*/
public static function do_subscriptions_shortcode($attributes)
{
$attributes = wp_parse_args($attributes, array('user_id' => 0, 'status' => 'active'));
$status = $attributes['status'];
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions($attributes['user_id']);
if (empty($subscriptions)) {
return '<ul class="user-subscriptions no-user-subscriptions">
<li>No subscriptions found.</li>
</ul>';
}
$list = '<ul class="user-subscriptions">';
foreach ($subscriptions as $subscription) {
if ($subscription['status'] == $status || $status == 'all') {
$list .= sprintf('<li>%s</li>', WC_Subscriptions_Order::get_item_name($subscription['order_id'], $subscription['product_id']));
}
}
$list .= '</ul>';
return $list;
}
示例15: maybe_put_subscription_on_hold
/**
* If a gateway doesn't manage payment schedules, then we should suspend the subscription until it is paid (i.e. for manual payments
* or token gateways like Stripe). If the gateway does manage the scheduling, then we shouldn't suspend the subscription because a
* gateway may use batch processing on the time payments are charged and a subscription could end up being incorrectly suspended.
*
* @param $user_id int The id of the user whose subscription should be put on-hold.
* @param $subscription_key string A subscription key of the form created by @see self::get_subscription_key()
* @since 1.2.5
*/
public static function maybe_put_subscription_on_hold($user_id, $subscription_key)
{
global $woocommerce;
$subscription = self::get_users_subscription($user_id, $subscription_key);
if (empty($subscription) || $subscription['status'] == 'on-hold') {
return false;
}
$order = new WC_Order($subscription['order_id']);
$payment_gateways = $woocommerce->payment_gateways->payment_gateways();
$order_uses_manual_payments = WC_Subscriptions_Order::requires_manual_renewal($order) ? true : false;
// If the subscription is using manual payments, the gateway isn't active or it manages scheduled payments
if ($order_uses_manual_payments || !isset($payment_gateways[$order->payment_method]) || !$payment_gateways[$order->payment_method]->supports('gateway_scheduled_payments')) {
self::put_subscription_on_hold($user_id, $subscription_key);
}
}