PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.4.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.4.2
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / Order.php

Order.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.4.2, at includes/Illuminate/Order.php

211 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Illuminate;
4
5 /**
6 * Class Order
7 *
8 * @package SpringDevs\Subscription\Illuminate
9 */
10 class Order {
11
12 /**
13 * Initialize the class.
14 */
15 public function __construct() {
16 add_action( 'woocommerce_admin_order_item_headers', array( $this, 'register_custom_column' ) );
17 add_action( 'woocommerce_admin_order_item_values', array( $this, 'add_column_value' ), 10, 2 );
18 add_action( 'woocommerce_before_order_itemmeta', array( $this, 'add_order_item_data' ), 10, 3 );
19 add_action( 'woocommerce_order_status_changed', array( $this, 'order_status_changed' ) );
20 add_action( 'woocommerce_before_delete_order', array( $this, 'delete_the_subscription' ) );
21 add_action( 'subscrpt_subscription_activated', array( $this, 'generate_dates_for_subscription' ) );
22 }
23
24 /**
25 * Generate start, next and trial dates.
26 *
27 * @param int $subscription_id Subscription Id.
28 *
29 * @return void
30 */
31 public function generate_dates_for_subscription( $subscription_id ) {
32 $order_item_id = get_post_meta( $subscription_id, '_subscrpt_order_item_id', true );
33 $subscription_history = Helper::get_subscription_from_order_item_id( $order_item_id );
34
35 $order_item_meta = wc_get_order_item_meta( $order_item_id, '_subscrpt_meta' );
36 $type = Helper::get_typos( 1, $order_item_meta['type'] );
37 $trial = get_post_meta( $subscription_id, '_subscrpt_trial', true );
38 $recurr_timing = ( $order_item_meta['time'] ?? 1 ) . ' ' . $type;
39 if ( 'new' === $subscription_history->type ) {
40 $start_date = time();
41 $next_date = sdevs_wp_strtotime( $recurr_timing, $start_date );
42 if ( $trial && ! empty( $trial ) ) {
43 $trial_started = get_post_meta( $subscription_id, '_subscrpt_trial_started', true );
44 $trial_ended = get_post_meta( $subscription_id, '_subscrpt_trial_ended', true );
45 if ( empty( $trial_started ) && empty( $trial_ended ) ) {
46 $start_date = sdevs_wp_strtotime( $trial );
47 update_post_meta( $subscription_id, '_subscrpt_trial_started', time() );
48 update_post_meta( $subscription_id, '_subscrpt_trial_ended', $start_date );
49 update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'on' );
50 $next_date = $start_date;
51 }
52 }
53 update_post_meta( $subscription_id, '_subscrpt_start_date', $start_date );
54 } elseif ( 'renew' === $subscription_history->type ) {
55 if ( $trial ) {
56 delete_post_meta( $subscription_id, '_subscrpt_trial' );
57 delete_post_meta( $subscription_id, '_subscrpt_trial_mode' );
58 delete_post_meta( $subscription_id, '_subscrpt_trial_started' );
59 delete_post_meta( $subscription_id, '_subscrpt_trial_ended' );
60 }
61 $next_date = sdevs_wp_strtotime( $recurr_timing, time() );
62 } elseif ( 'early-renew' === $subscription_history->type ) {
63 $next_date = sdevs_wp_strtotime( $recurr_timing, get_post_meta( $subscription_id, '_subscrpt_next_date', true ) );
64
65 if ( $trial ) {
66 $trial_mode = get_post_meta( $subscription_id, '_subscrpt_trial_mode', true );
67 if ( 'on' === $trial_mode ) {
68 update_post_meta( $subscription_id, '_subscrpt_trial_mode', 'extended' );
69 $next_date = sdevs_wp_strtotime( $recurr_timing, get_post_meta( $subscription_id, '_subscrpt_trial_ended', true ) );
70 }
71 }
72 }
73 update_post_meta( $subscription_id, '_subscrpt_next_date', $next_date );
74 }
75
76 /**
77 * Add custom column on order item.
78 *
79 * @return void
80 */
81 public function register_custom_column() {
82 ?>
83 <th class="item_recurring sortable" data-sort="float"><?php esc_html_e( 'Recurring', 'sdevs_subscrpt' ); ?></th>
84 <?php
85 }
86
87 /**
88 * Display data for custom column.
89 *
90 * @param \WC_Product $product Product Object.
91 * @param \WC_Order_Item $item Order Item.
92 *
93 * @return void
94 */
95 public function add_column_value( $product, $item ) {
96 if ( ! method_exists( $item, 'get_id' ) || ! method_exists( $item, 'get_subtotal' ) ) {
97 return;
98 }
99
100 $subtotal = '-';
101 $item_id = $item->get_id();
102 $subscription_id = Helper::get_subscription_from_order_item_id( $item->get_id() );
103
104 if ( ! $subscription_id ) {
105 echo "<td class='item_recurring' width='15%'>-</td>";
106 return;
107 }
108 $subscription_id = $subscription_id->subscription_id;
109
110 $price = get_post_meta( $subscription_id, '_subscrpt_price', true );
111 $subtotal = Helper::format_price_with_order_item( $price, $item_id );
112 ?>
113 <td class="item_recurring" width="15%">
114 <div class="view">
115 <?php echo wp_kses_post( $subtotal ); ?>
116 </div>
117 </td>
118 <?php
119 }
120
121 public function add_order_item_data( $item_id, $item, $product ) {
122 if ( ! $product ) {
123 return;
124 }
125
126 $item_meta = wc_get_order_item_meta( $item_id, '_subscrpt_meta', true );
127
128 if ( ! $item_meta || ! is_array( $item_meta ) ) {
129 return false;
130 }
131
132 $trial = $item_meta['trial'];
133 $has_trial = isset( $item_meta['trial'] ) && strlen( $item_meta['trial'] ) > 2;
134
135 if ( $has_trial ) {
136 echo '<br/><small> + Got ' . $trial . ' free trial!</small>';
137 }
138 }
139
140 /**
141 * Take some actions based on order status changed.
142 *
143 * @param int $order_id Order Id.
144 */
145 public function order_status_changed( $order_id ) {
146 $order = wc_get_order( $order_id );
147 $post_status = 'active';
148
149 switch ( $order->get_status() ) {
150 case 'on-hold':
151 case 'pending':
152 $post_status = 'pending';
153 break;
154
155 case 'refunded':
156 case 'failed':
157 case 'cancelled':
158 $post_status = 'cancelled';
159 break;
160
161 default:
162 $post_status = 'active';
163 break;
164 }
165 $post_status = apply_filters( 'subscript_order_status_to_post_status', $post_status, $order );
166
167 global $wpdb;
168 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
169 // @phpcs:ignore
170 $histories = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE order_id=%d', array( $table_name, $order_id ) ) );
171
172 foreach ( $histories as $history ) {
173 if ( 'new' === $history->type || 'renew' === $history->type ) {
174 wp_update_post(
175 array(
176 'ID' => $history->subscription_id,
177 'post_status' => $post_status,
178 )
179 );
180
181 Action::write_comment( $post_status, $history->subscription_id );
182 } else {
183 do_action( 'subscrpt_order_status_changed', $order, $history );
184 }
185 }
186 }
187
188 /**
189 * Delete the subscription.
190 *
191 * @param int $order_id Order ID.
192 *
193 * @return void
194 */
195 public function delete_the_subscription( $order_id ) {
196 global $wpdb;
197 $table_name = $wpdb->prefix . 'subscrpt_order_relation';
198
199 $histories = Helper::get_subscriptions_from_order( $order_id );
200 foreach ( (array) $histories as $history ) {
201 $subscription_order_id = get_post_meta( $history->subscription_id, '_subscrpt_order_id', true );
202 if ( (int) $subscription_order_id === $order_id ) {
203 wp_delete_post( $history->subscription_id, true );
204 }
205 }
206
207 // phpcs:ignore
208 $wpdb->delete( $table_name, array( 'order_id' => $order_id ), array( '%d' ) );
209 }
210 }
211