本文整理汇总了PHP中WC_Subscriptions_Manager::process_subscriptions_on_checkout方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Subscriptions_Manager::process_subscriptions_on_checkout方法的具体用法?PHP WC_Subscriptions_Manager::process_subscriptions_on_checkout怎么用?PHP WC_Subscriptions_Manager::process_subscriptions_on_checkout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Subscriptions_Manager
的用法示例。
在下文中一共展示了WC_Subscriptions_Manager::process_subscriptions_on_checkout方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generate_renewal_order
/**
* Creates a new order for renewing a subscription product based on the details of a previous order.
*
* @param $order WC_Order | int The WC_Order object or ID of the order for which the a new order should be created.
* @param $meta_key string The ID of the subscription product in the order which needs to be added to the new order.
* @since 1.0
*/
public static function generate_renewal_order($original_order, $product_id)
{
global $wpdb;
if (!is_object($original_order)) {
$original_order = new WC_Order($original_order);
}
// Create the new order
$renewal_order_data = array('post_type' => 'shop_order', 'post_title' => 'Renewal Order – ' . date('F j, Y @ h:i A'), 'post_status' => 'publish', 'ping_status' => 'closed', 'post_excerpt' => $original_order->customer_note, 'post_author' => 1, 'post_password' => uniqid('order_'));
$renewal_order_id = wp_insert_post($renewal_order_data);
// Set the order as pending
wp_set_object_terms($renewal_order_id, 'pending', 'shop_order_status');
// Carry all the post meta from the old order over to the new order
$order_meta_items = $wpdb->get_results("SELECT `meta_key`, `meta_value` FROM {$wpdb->postmeta} WHERE `post_id` = {$original_order->id}", 'ARRAY_A');
foreach ($order_meta_items as $order_meta_item) {
add_post_meta($renewal_order_id, $order_meta_item['meta_key'], maybe_unserialize($order_meta_item['meta_value']), true);
}
$outstanding_balance = self::get_outstanding_balance($original_order, $product_id);
// If there are outstanding payment amounts, add them as a sign-up fee to the order, otherwise set the sign-up fee to 0
if ($outstanding_balance > 0 && 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_add_outstanding_balance')) {
$failed_payment_count = self::get_failed_payment_count($original_order, $product_id);
$sign_up_fee_subtotal = 0;
$sign_up_fee_subtotal_ex_tax = 0;
// Calculate
foreach ($original_order->get_items() as $item) {
if ($item['id'] != $product_id) {
continue;
}
$base_price = WC_Subscriptions_Product::get_price($item['id']) * $item['qty'];
// Row price
if ($original_order->prices_include_tax) {
// Sub total is based on base prices (without discounts or shipping)
$sign_up_fee_subtotal += $base_price * $failed_payment_count;
$sign_up_fee_subtotal_ex_tax += ($base_price - $item['line_tax']) * $failed_payment_count;
} else {
// Sub total is based on base prices (without discounts)
$sign_up_fee_subtotal += ($base_price + $item['line_tax']) * $failed_payment_count;
$sign_up_fee_subtotal_ex_tax += $base_price * $failed_payment_count;
}
}
update_post_meta($renewal_order_id, '_sign_up_fee_total', $outstanding_balance);
update_post_meta($renewal_order_id, '_sign_up_fee_subtotal', $sign_up_fee_subtotal);
update_post_meta($renewal_order_id, '_sign_up_fee_subtotal_ex_tax', $sign_up_fee_subtotal_ex_tax);
$sign_up_fee_taxes = get_post_meta($renewal_order_id, '_sign_up_fee_taxes', true);
foreach ($original_order->get_taxes() as $key => $tax) {
$sign_up_fee_taxes[$key]['cart_tax'] = $tax['cart_tax'] * $failed_payment_count;
$sign_up_fee_taxes[$key]['shipping_tax'] = $tax['shipping_tax'] * $failed_payment_count;
}
update_post_meta($renewal_order_id, '_sign_up_fee_taxes', $sign_up_fee_taxes);
update_post_meta($renewal_order_id, '_sign_up_fee_tax_total', $original_order->get_total_tax() * $failed_payment_count);
update_post_meta($renewal_order_id, '_sign_up_fee_discount_cart', $original_order->cart_discount * $failed_payment_count);
update_post_meta($renewal_order_id, '_sign_up_fee_discount_total', $original_order->order_discount * $failed_payment_count);
} else {
// Remove all sign-up fees
foreach (array('_cart_contents_sign_up_fee_total', '_cart_contents_sign_up_fee_count', '_sign_up_fee_total', '_sign_up_fee_subtotal', '_sign_up_fee_subtotal_ex_tax', '_sign_up_fee_tax_total', '_sign_up_fee_discount_cart', '_sign_up_fee_discount_total') as $meta_key) {
update_post_meta($renewal_order_id, $meta_key, 0);
}
update_post_meta($renewal_order_id, '_sign_up_fee_taxes', array());
}
// Keep a record of the original order's ID on the renewal order
add_post_meta($renewal_order_id, '_original_order', $original_order->id, true);
// Make sure the original order is cancelled and keep a note of the renewal order
if (!in_array($original_order->status, array('cancelled', 'expired', 'failed'))) {
$original_order->cancel_order();
}
$original_order->add_order_note(sprintf(__('Order superseded by renewal order %s.', WC_Subscriptions::$text_domain), $renewal_order_data));
$renewal_order = new WC_Order($renewal_order_id);
WC_Subscriptions_Manager::process_subscriptions_on_checkout($renewal_order_id);
do_action('woocommerce_subscriptions_renewal_order_created', $renewal_order, $original_order, $product_id);
}
示例2: generate_renewal_order
//.........这里部分代码省略.........
$order_meta_query = "SELECT `meta_key`, `meta_value` FROM {$wpdb->postmeta} WHERE `post_id` = {$original_order->id} AND `meta_key` NOT IN ('_paid_date', '_completed_date', '_order_key', '_edit_lock', '_original_order')";
// Superseding existing order so don't carry over payment details
if ('parent' == $new_order_role) {
$order_meta_query .= " AND `meta_key` NOT IN ('_payment_method', '_payment_method_title')";
} else {
$order_meta_query .= " AND `meta_key` NOT LIKE '_order_recurring_%'";
}
// Allow extensions to add/remove order meta
$order_meta_query = apply_filters('woocommerce_subscriptions_renewal_order_meta_query', $order_meta_query, $original_order->id, $renewal_order_id, $new_order_role);
// Carry all the required meta from the old order over to the new order
$order_meta = $wpdb->get_results($order_meta_query, 'ARRAY_A');
$order_meta = apply_filters('woocommerce_subscriptions_renewal_order_meta', $order_meta, $original_order->id, $renewal_order_id, $new_order_role);
foreach ($order_meta as $meta_item) {
add_post_meta($renewal_order_id, $meta_item['meta_key'], maybe_unserialize($meta_item['meta_value']), true);
}
$outstanding_balance = WC_Subscriptions_Order::get_outstanding_balance($original_order, $product_id);
// If there are outstanding payment amounts, add them to the order, otherwise set the order details to the values of the recurring totals
if ($outstanding_balance > 0 && 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_add_outstanding_balance')) {
$failed_payment_multiplier = WC_Subscriptions_Order::get_failed_payment_count($original_order, $product_id);
} else {
$failed_payment_multiplier = 1;
}
// Set order totals based on recurring totals from the original order
$cart_discount = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_discount_cart', true);
$order_discount = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_discount_total', true);
$order_shipping_tax = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_shipping_tax_total', true);
$order_tax = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_tax_total', true);
$order_total = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_total', true);
update_post_meta($renewal_order_id, '_cart_discount', $cart_discount);
update_post_meta($renewal_order_id, '_order_discount', $order_discount);
update_post_meta($renewal_order_id, '_order_shipping_tax', $order_shipping_tax);
update_post_meta($renewal_order_id, '_order_tax', $order_tax);
update_post_meta($renewal_order_id, '_order_total', $order_total);
// Set order taxes based on recurring taxes from the original order
$recurring_order_taxes = get_post_meta($original_order->id, '_order_recurring_taxes', true);
foreach ($recurring_order_taxes as $index => $recurring_order_tax) {
if (isset($recurring_order_tax['cart_tax']) && $recurring_order_tax['cart_tax'] > 0) {
$recurring_order_taxes[$index]['cart_tax'] = $failed_payment_multiplier * $recurring_order_tax['cart_tax'];
} else {
$recurring_order_taxes[$index]['cart_tax'] = 0;
}
if (isset($recurring_order_tax['shipping_tax']) && $recurring_order_tax['shipping_tax'] > 0) {
$recurring_order_taxes[$index]['shipping_tax'] = $failed_payment_multiplier * $recurring_order_tax['shipping_tax'];
} else {
$recurring_order_taxes[$index]['shipping_tax'] = 0;
}
}
update_post_meta($renewal_order_id, '_order_taxes', $recurring_order_taxes);
// Set line totals to be recurring line totals and remove the subscription/recurring related item meta from each order item
$order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
// Allow extensions to add/remove items or item meta
$order_items = apply_filters('woocommerce_subscriptions_renewal_order_items', $order_items, $original_order->id, $renewal_order_id, $product_id, $new_order_role);
foreach ($order_items as $item_index => $order_item) {
$item_meta = new WC_Order_Item_Meta($order_item['item_meta']);
// Remove recurring line items and set item totals based on recurring line totals
foreach ($item_meta->meta as $meta_index => $meta_item) {
switch ($meta_item['meta_name']) {
case '_recurring_line_total':
$order_items[$item_index]['line_total'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_tax':
$order_items[$item_index]['line_tax'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_subtotal':
$order_items[$item_index]['line_subtotal'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_subtotal_tax':
$order_items[$item_index]['line_subtotal_tax'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_total':
case '_recurring_line_tax':
case '_recurring_line_subtotal':
case '_recurring_line_subtotal_tax':
case '_recurring_line_subtotal_tax':
case '_subscription_recurring_amount':
case '_subscription_sign_up_fee':
case '_subscription_period':
case '_subscription_interval':
case '_subscription_length':
case '_subscription_trial_length':
case '_subscription_trial_period':
if ('child' == $new_order_role) {
unset($item_meta->meta[$meta_index]);
}
break;
}
}
if ('child' == $new_order_role) {
$order_items[$item_index]['name'] = sprintf(__('Renewal of "%s" purchased in Order %s', WC_Subscriptions::$text_domain), $order_item['name'], $original_order->get_order_number());
}
$order_items[$item_index]['item_meta'] = $item_meta->meta;
}
// Save the item meta on the new order
update_post_meta($renewal_order_id, '_order_items', $order_items);
// Keep a record of the original order's ID on the renewal order
update_post_meta($renewal_order_id, '_original_order', $original_order->id, true);
$renewal_order = new WC_Order($renewal_order_id);
if ('parent' == $new_order_role) {
WC_Subscriptions_Manager::process_subscriptions_on_checkout($renewal_order_id);
$original_order->add_order_note(sprintf(__('Order superseded by Renewal Order %s.', WC_Subscriptions::$text_domain), $renewal_order->get_order_number()));
}
do_action('woocommerce_subscriptions_renewal_order_created', $renewal_order, $original_order, $product_id, $new_order_role);
return apply_filters('woocommerce_subscriptions_renewal_order_id', $renewal_order_id, $original_order, $product_id, $new_order_role);
}
示例3: generate_renewal_order
//.........这里部分代码省略.........
break;
}
// Copy over line item meta data, with some parent/child role based exceptions for recurring amounts
$copy_to_renewal_item = true;
switch ($meta_key) {
case '_recurring_line_total':
case '_recurring_line_tax':
case '_recurring_line_subtotal':
case '_recurring_line_subtotal_tax':
case '_subscription_recurring_amount':
case '_subscription_sign_up_fee':
case '_subscription_period':
case '_subscription_interval':
case '_subscription_length':
case '_subscription_trial_period':
case '_subscription_end_date':
case '_subscription_expiry_date':
case '_subscription_start_date':
case '_subscription_status':
case '_subscription_completed_payments':
if ('child' == $args['new_order_role']) {
$copy_to_renewal_item = false;
}
break;
case '_subscription_trial_length':
// We never want to duplicate free trials on renewal orders
$copy_to_renewal_item = false;
break;
case '_subscription_suspension_count':
// We want to reset some values for the new order
// We want to reset some values for the new order
case '_subscription_trial_expiry_date':
case '_subscription_failed_payments':
$copy_to_renewal_item = false;
$meta_value = 0;
break;
default:
break;
}
// Copy existing item over to new recurring order item
if ($copy_to_renewal_item) {
woocommerce_add_order_item_meta($recurring_item_id, $meta_key, $meta_value);
}
}
} else {
// WC 1.x order item structure
foreach ($item_meta->meta as $meta_index => $meta_item) {
switch ($meta_item['meta_name']) {
case '_recurring_line_total':
$order_items[$item_index]['line_total'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_tax':
$order_items[$item_index]['line_tax'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_subtotal':
$order_items[$item_index]['line_subtotal'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_subtotal_tax':
$order_items[$item_index]['line_subtotal_tax'] = $failed_payment_multiplier * $meta_item['meta_value'];
case '_recurring_line_total':
case '_recurring_line_tax':
case '_recurring_line_subtotal':
case '_recurring_line_subtotal_tax':
case '_recurring_line_subtotal_tax':
case '_subscription_recurring_amount':
case '_subscription_sign_up_fee':
case '_subscription_period':
case '_subscription_interval':
case '_subscription_length':
case '_subscription_trial_length':
case '_subscription_trial_period':
if ('child' == $args['new_order_role']) {
unset($item_meta->meta[$meta_index]);
}
break;
case '_subscription_trial_length':
// We never want to duplicate free trials on renewal orders
if ('child' == $args['new_order_role']) {
unset($item_meta->meta[$meta_index]);
} else {
$item_meta->meta[$meta_index] = 0;
}
break;
}
if ('child' == $args['new_order_role']) {
$order_items[$item_index]['name'] = sprintf(__('Renewal of "%s" purchased in Order %s', WC_Subscriptions::$text_domain), $order_item['name'], $original_order->get_order_number());
}
$order_items[$item_index]['item_meta'] = $item_meta->meta;
}
// Save the item meta on the new order
update_post_meta($renewal_order_id, '_order_items', $order_items);
}
}
// Keep a record of the original order's ID on the renewal order
update_post_meta($renewal_order_id, '_original_order', $original_order->id, true);
$renewal_order = new WC_Order($renewal_order_id);
if ('parent' == $args['new_order_role']) {
WC_Subscriptions_Manager::process_subscriptions_on_checkout($renewal_order_id);
$original_order->add_order_note(sprintf(__('Order superseded by Renewal Order %s.', WC_Subscriptions::$text_domain), $renewal_order->get_order_number()));
}
do_action('woocommerce_subscriptions_renewal_order_created', $renewal_order, $original_order, $product_id, $args['new_order_role']);
return apply_filters('woocommerce_subscriptions_renewal_order_id', $renewal_order_id, $original_order, $product_id, $args['new_order_role']);
}
示例4: generate_renewal_order
//.........这里部分代码省略.........
// There will only be recurring tax data if the recurring amount is > 0 and we can only retroactively calculate recurring amount from initial amoutn if it is > 0
if ($line_total > 0 && $recurring_line_total > 0) {
// Make sure we account for any sign-up fees by determining what proportion of the initial amount the recurring total represents
$recurring_ratio = $recurring_line_total / $line_total;
$recurring_tax_data = array();
$tax_data_keys = array('total', 'subtotal');
foreach ($tax_data_keys as $tax_data_key) {
foreach ($meta_value[$tax_data_key] as $tax_index => $tax_value) {
// Use total tax amount for both total and subtotal because we don't want any initial discounts to be applied to recurring amounts
$total_tax_amount = $meta_value['total'][$tax_index];
$recurring_tax_data[$tax_data_key][$tax_index] = woocommerce_format_decimal($failed_payment_multiplier * ($recurring_ratio * $total_tax_amount));
}
}
} else {
$recurring_tax_data = array('total' => array(), 'subtotal' => array());
}
woocommerce_update_order_item_meta($recurring_item_id, '_line_tax_data', $recurring_tax_data);
}
break;
case '_recurring_line_tax_data':
$recurring_tax_data = array();
$tax_data_keys = array('total', 'subtotal');
foreach ($tax_data_keys as $tax_data_key) {
foreach ($meta_value[$tax_data_key] as $tax_index => $tax_value) {
$recurring_tax_data[$tax_data_key][$tax_index] = woocommerce_format_decimal($failed_payment_multiplier * $tax_value);
}
}
woocommerce_update_order_item_meta($recurring_item_id, '_line_tax_data', $recurring_tax_data);
break;
default:
break;
}
}
// Copy over line item meta data, with some parent/child role based exceptions for recurring amounts
$copy_to_renewal_item = true;
switch ($meta_key) {
case '_recurring_line_total':
case '_recurring_line_tax':
case '_recurring_line_subtotal':
case '_recurring_line_subtotal_tax':
case '_recurring_line_tax_data':
case '_line_tax_data':
case '_subscription_recurring_amount':
case '_subscription_sign_up_fee':
case '_subscription_period':
case '_subscription_interval':
case '_subscription_length':
case '_subscription_trial_period':
case '_subscription_end_date':
case '_subscription_expiry_date':
case '_subscription_start_date':
case '_subscription_status':
case '_subscription_completed_payments':
if ('child' == $args['new_order_role']) {
$copy_to_renewal_item = false;
}
break;
case '_subscription_trial_length':
// We never want to duplicate free trials on renewal orders
$copy_to_renewal_item = false;
break;
case '_subscription_suspension_count':
// We want to reset some values for the new order
// We want to reset some values for the new order
case '_subscription_trial_expiry_date':
case '_subscription_failed_payments':
$copy_to_renewal_item = false;
$meta_value = 0;
break;
default:
break;
}
// Copy existing item over to new recurring order item
if ($copy_to_renewal_item) {
woocommerce_add_order_item_meta($recurring_item_id, $meta_key, $meta_value);
}
}
}
if (false == $args['checkout_renewal']) {
// Add fees
foreach ($original_order->get_fees() as $item_id => $order_fee) {
if (!isset($order_fee['recurring_line_total'])) {
continue;
}
$item_id = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $order_fee['name'], 'order_item_type' => 'fee'));
woocommerce_add_order_item_meta($item_id, '_tax_class', $order_fee['tax_class']);
woocommerce_add_order_item_meta($item_id, '_line_total', WC_Subscriptions::format_total($order_fee['recurring_line_total']));
woocommerce_add_order_item_meta($item_id, '_line_tax', WC_Subscriptions::format_total($order_fee['recurring_line_tax']));
}
}
// Keep a record of the original order's ID on the renewal order
update_post_meta($renewal_order_id, '_original_order', $original_order->id, true);
$renewal_order = new WC_Order($renewal_order_id);
if ('parent' == $args['new_order_role']) {
WC_Subscriptions_Manager::process_subscriptions_on_checkout($renewal_order_id);
$original_order->add_order_note(sprintf(__('Order superseded by Renewal Order %s.', 'woocommerce-subscriptions'), $renewal_order->get_order_number()));
}
do_action('woocommerce_subscriptions_renewal_order_created', $renewal_order, $original_order, $product_id, $args['new_order_role']);
return apply_filters('woocommerce_subscriptions_renewal_order_id', $renewal_order_id, $original_order, $product_id, $args['new_order_role']);
}