-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-subscriptions-manual-repair.php
executable file
·157 lines (134 loc) · 6 KB
/
woocommerce-subscriptions-manual-repair.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Plugin Name: WooCommerce Subscriptions - Recalculate subscription totals
* Plugin URI: https://github.com/SatelliteWP/woocommerce-subscriptions-recalculate-totals
* Description: In some cases, if the tax settings change after some subscripitons have been created, their totals need to be recalculated in order to include the proper taxes. This plugin recalculates all the subscripitons totals.
* Author: Prospress Inc.
* Version: 1.1
* Author URI: http://prospress.com
*
* GitHub Plugin URI: SatelliteWP/woocommerce-subscriptions-recalculate-totals
* GitHub Branch: master
*
* Copyright 2017 Prospress, Inc. (email : [email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions
* @author Prospress Inc.
* @since 1.0
*/
function wcs_recalculate_totals() {
// If requested, backup and reset the "wcs_subscriptions_with_totals_updated" option
if ( isset( $_GET['reset-updated-subs-option'] ) ) {
$old_option = get_option( 'wcs_subscriptions_with_totals_updated', array() );
update_option( '_old_wcs_subscriptions_with_totals_updated', $old_option, false );
update_option( 'wcs_subscriptions_with_totals_updated', '', false );
return;
}
// Use a URL param to act as a flag for when to run the fixes - avoids running fixes in multiple requests at the same time
if ( ! isset( $_GET['wcs-recalculate-totals'] ) ) {
return;
}
$checked_subscriptions = get_option( 'wcs_subscriptions_with_totals_updated', array() );
$subscriptions_to_check = get_posts( array(
'fields' => 'ids',
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'shop_subscription',
'posts_per_page' => 50,
'post__not_in' => $checked_subscriptions,
'post_status' => array(
'wc-active',
'wc-on-hold',
),
) );
$logger = new WC_Logger();
$logger->add( 'wcs-recalculate-totals', '----------- Initiating Recalculate subscription totals Fixer -----------' );
$logger->add( 'wcs-recalculate-totals', 'Checked subscriptions = ' . var_export( $checked_subscriptions, true ) );
$logger->add( 'wcs-recalculate-totals', 'Subscriptions to check = ' . var_export( $subscriptions_to_check, true ) );
foreach ( $subscriptions_to_check as $subscription_id ) {
$logger->add( 'wcs-recalculate-totals', '------ Checking subscription with ID = ' . var_export( $subscription_id, true ) );
$subscription = wcs_get_subscription( $subscription_id );
$order = wc_get_order( $subscription_id );
$order_data = $order->get_data();
$products_data = array();
if ( $subscription ) {
if ( isset( $_GET['readd'] ) ) {
// Backup line items (product_id => quantity)
foreach ( $subscription->get_items() as $item_id => $item ) {
$qtty = $item['quantity'];
$product_id = $item['product_id'];
if ( isset( $item['variation_id']) && $item['variation_id'] != '' ) {
$product_id = $item['variation_id'];
}
$products_data[$product_id] = $qtty;
}
$logger->add( 'wcs-recalculate-totals', '* Saved line items ' . var_export( $products_data, true ) );
// Delete all order items
$subscription->remove_order_items( 'line_item' );
$logger->add( 'wcs-recalculate-totals', '* Removed order items' );
// Add the saved order items
foreach ( $products_data as $product_id => $qty ) {
if ( $qty > 0 ) {
$product = wc_get_product( $product_id );
$subscription->add_product( $product, $qty );
$logger->add( 'wcs-recalculate-totals', "* Added $qty units of product #$product_id" );
}
}
// Update taxes and totals
$subscription->update_taxes();
$calc_total = $subscription->calculate_totals();
// Add an order note
$order->add_order_note( "Order totals recalculated automatically (woocommerce-subscriptions-recalculate-totals)" );
$logger->add( 'wcs-recalculate-totals', "* Recalculated totals" );
}
else {
// Update totals
$calc_total = $subscription->calculate_totals();
}
}
$checked_subscriptions[] = $subscription_id;
// Update the record on each iteration in case we can't make it through 50 subscriptions in one request
update_option( 'wcs_subscriptions_with_totals_updated', $checked_subscriptions, false );
}
}
add_action( 'init', 'wcs_recalculate_totals' );
function general_admin_notice(){
if ( ! isset( $_GET['wcs-recalculate-totals'] ) ) {
return;
}
$checked_subscriptions = get_option( 'wcs_subscriptions_with_totals_updated', array() );
$subscriptions_to_check = get_posts( array(
'fields' => 'ids',
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'shop_subscription',
'posts_per_page' => 50,
'post__not_in' => $checked_subscriptions,
'post_status' => array(
'wc-active',
'wc-on-hold',
),
) );
if ( $checked_subscriptions != '' && empty( $subscriptions_to_check ) ) {
echo '<div class="notice notice-success is-dismissible">
<p><b>All subscriptions have been recalculated! :)</b></p>
<p>If you want to recalculate all your subscriptions again, you will need to use <mark style="background: #e5e5e5;">"reset-updated-subs-option=true"</mark> parameter</p>
</div>';
}
$logger = new WC_Logger();
$logger->add( 'wcs-recalculate-totals', '******** All subscriptions have been recalculated! ********' );
}
add_action( 'admin_notices', 'general_admin_notice' );