PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.33
OttoKit: All-in-One Automation Platform v1.1.33
1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / woocommerce-subscriptions / actions / create-subscription-order-parent.php
suretriggers / src / Integrations / woocommerce-subscriptions / actions Last commit date
add-subscription-coupon.php 10 months ago add-update-subscription-custom-fields.php 10 months ago change-subscription-status.php 10 months ago create-subscription-order-cost.php 7 months ago create-subscription-order-parent.php 6 months ago create-subscription-order-product.php 10 months ago extend-user-subscription.php 10 months ago find-subscription-by-id.php 5 days ago find-subscription-by-user-id.php 10 months ago remove-product-user-subscription.php 10 months ago update-subscription-end-date.php 10 months ago update-subscription-next-payment-date.php 10 months ago update-subscription-order-downloads.php 6 months ago
create-subscription-order-parent.php
371 lines
1 <?php
2 /**
3 * CreateSubscriptionOrderParent.
4 * php version 5.6
5 *
6 * @category CreateSubscriptionOrderParent
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\WoocommerceSubscriptions\Actions;
15
16 use SureTriggers\Integrations\AutomateAction;
17 use SureTriggers\Integrations\WooCommerce\WooCommerce;
18 use SureTriggers\Traits\SingletonLoader;
19 use Exception;
20 use WC_Subscriptions_Product;
21 use WC_Order;
22
23 /**
24 * CreateSubscriptionOrderParent
25 *
26 * @category CreateSubscriptionOrderParent
27 * @package SureTriggers
28 * @author BSF <username@example.com>
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
30 * @link https://www.brainstormforce.com/
31 * @since 1.0.0
32 */
33 class CreateSubscriptionOrderParent extends AutomateAction {
34
35 /**
36 * Integration type.
37 *
38 * @var string
39 */
40 public $integration = 'WoocommerceSubscriptions';
41
42 /**
43 * Action name.
44 *
45 * @var string
46 */
47 public $action = 'wc_create_subscription_order_parent';
48
49 use SingletonLoader;
50
51 /**
52 * Register a action.
53 *
54 * @param array $actions actions.
55 * @return array
56 */
57 public function register( $actions ) {
58 $actions[ $this->integration ][ $this->action ] = [
59 'label' => __( 'Create a subscription order with parent order id', 'suretriggers' ),
60 'action' => 'wc_create_subscription_order_parent',
61 'function' => [ $this, 'action_listener' ],
62 ];
63 return $actions;
64 }
65
66 /**
67 * Action listener.
68 *
69 * @param mixed $user_id user_id.
70 * @param int $automation_id automation_id.
71 * @param array $fields fields.
72 * @param array $selected_options selectedOptions.
73 * @throws Exception Exception.
74 *
75 * @return object|array|null|void
76 */
77 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
78 if ( ! function_exists( 'wc_create_order' ) ) {
79 return [
80 'status' => 'error',
81 'message' => 'WooCommerce function `wc_create_order` is missing.',
82 ];
83 }
84
85 if ( ! function_exists( 'wcs_create_subscription' ) ) {
86 return [
87 'status' => 'error',
88 'message' => 'WooCommerce Subscriptions function `wcs_create_subscription` is missing.',
89 ];
90 }
91
92 if ( ! class_exists( 'WC_Subscriptions' ) ) {
93 return [
94 'status' => 'error',
95 'message' => 'WooCommerce Subscriptions plugin is not active.',
96 ];
97 }
98
99 if ( ! class_exists( '\WC_Order' ) ) {
100 return [
101 'status' => 'error',
102 'message' => __( '\WC_Order class not found.', 'suretriggers' ),
103
104 ];
105 }
106
107 if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
108 return [
109 'status' => 'error',
110 'message' => __( 'WC_Subscriptions_Product class not found.', 'suretriggers' ),
111
112 ];
113 }
114
115 $parent_order_id = isset( $selected_options['parent_order_id'] ) ? intval( $selected_options['parent_order_id'] ) : 0;
116
117 if ( $parent_order_id <= 0 ) {
118 return [
119 'status' => 'error',
120 'message' => __( 'Parent order ID is required.', 'suretriggers' ),
121 ];
122 }
123
124 $parent_order = wc_get_order( $parent_order_id );
125 if ( ! $parent_order || ! $parent_order instanceof \WC_Order ) {
126 return [
127 'status' => 'error',
128 'message' => __( 'Invalid parent order ID provided.', 'suretriggers' ),
129 ];
130 }
131
132 $user_id = $parent_order->get_user_id();
133 $order_items = $parent_order->get_items();
134
135 if ( empty( $order_items ) ) {
136 return [
137 'status' => 'error',
138 'message' => __( 'Parent order has no products to create subscription.', 'suretriggers' ),
139 ];
140 }
141
142 $bundle_containers = [];
143 $subscription_product = null;
144 $product_id = 0;
145
146 foreach ( $order_items as $item ) {
147 if ( ! method_exists( $item, 'get_product' ) || ! method_exists( $item, 'get_product_id' ) ) {
148 continue;
149 }
150
151 $bundled_by = $item->get_meta( '_bundled_by' );
152 if ( ! empty( $bundled_by ) ) {
153 continue;
154 }
155
156 $product = $item->get_product();
157 $product_id = $item->get_product_id();
158
159 if ( $product ) {
160 $bundled_items_meta = $item->get_meta( '_bundled_items' );
161 if ( ! empty( $bundled_items_meta ) ) {
162 $bundle_containers[] = $item;
163 }
164
165 if ( ! $subscription_product ) {
166 $subscription_product = $product;
167 }
168 }
169 }
170
171 if ( ! $subscription_product ) {
172 return [
173 'status' => 'error',
174 'message' => __( 'No product found in parent order.', 'suretriggers' ),
175 ];
176 }
177
178 if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
179 return [
180 'status' => 'error',
181 'message' => __( 'WC_Subscriptions_Product class not found.', 'suretriggers' ),
182 ];
183 }
184
185 $billing_period = WC_Subscriptions_Product::get_period( $product_id );
186 $billing_interval = WC_Subscriptions_Product::get_interval( $product_id );
187
188 if ( empty( $billing_period ) ) {
189 $billing_period = 'year';
190 }
191
192 if ( empty( $billing_interval ) || $billing_interval <= 0 ) {
193 $billing_interval = 1;
194 }
195
196 $sub_args = [
197 'order_id' => $parent_order->get_id(),
198 'customer_id' => $user_id,
199 'billing_period' => $billing_period,
200 'billing_interval' => $billing_interval,
201 ];
202
203 $sub = wcs_create_subscription( $sub_args );
204
205 if ( is_wp_error( $sub ) ) {
206 $error_message = $sub->get_error_message();
207 return [
208 'status' => 'error',
209 'message' => 'Failed to create a subscription: ' . $error_message,
210 ];
211 }
212
213 if ( ! $sub ) {
214 return [
215 'status' => 'error',
216 'message' => 'Failed to create a subscription: No subscription object returned.',
217 ];
218 }
219
220 foreach ( $order_items as $item ) {
221 if ( ! method_exists( $item, 'get_product' ) || ! method_exists( $item, 'get_quantity' ) ) {
222 continue;
223 }
224
225 $bundled_by = $item->get_meta( '_bundled_by' );
226 if ( ! empty( $bundled_by ) ) {
227 continue;
228 }
229
230 $product = $item->get_product();
231 if ( $product ) {
232 $bundled_items_meta = $item->get_meta( '_bundled_items' );
233 if ( ! empty( $bundled_items_meta ) ) {
234 $this->add_bundle_product_to_subscription( $sub, $product, $item, $parent_order );
235 } else {
236 $sub->add_product( $product, $item->get_quantity() );
237 }
238 }
239 }
240
241 $billing_address = [
242 'first_name' => $parent_order->get_billing_first_name(),
243 'last_name' => $parent_order->get_billing_last_name(),
244 'company' => $parent_order->get_billing_company(),
245 'country' => $parent_order->get_billing_country(),
246 'address_1' => $parent_order->get_billing_address_1(),
247 'address_2' => $parent_order->get_billing_address_2(),
248 'city' => $parent_order->get_billing_city(),
249 'state' => $parent_order->get_billing_state(),
250 'postcode' => $parent_order->get_billing_postcode(),
251 'phone' => $parent_order->get_billing_phone(),
252 'email' => $parent_order->get_billing_email(),
253 ];
254
255 $shipping_address = [
256 'first_name' => $parent_order->get_shipping_first_name() ? $parent_order->get_shipping_first_name() : $parent_order->get_billing_first_name(),
257 'last_name' => $parent_order->get_shipping_last_name() ? $parent_order->get_shipping_last_name() : $parent_order->get_billing_last_name(),
258 'company' => $parent_order->get_shipping_company() ? $parent_order->get_shipping_company() : $parent_order->get_billing_company(),
259 'country' => $parent_order->get_shipping_country() ? $parent_order->get_shipping_country() : $parent_order->get_billing_country(),
260 'address_1' => $parent_order->get_shipping_address_1() ? $parent_order->get_shipping_address_1() : $parent_order->get_billing_address_1(),
261 'address_2' => $parent_order->get_shipping_address_2() ? $parent_order->get_shipping_address_2() : $parent_order->get_billing_address_2(),
262 'city' => $parent_order->get_shipping_city() ? $parent_order->get_shipping_city() : $parent_order->get_billing_city(),
263 'state' => $parent_order->get_shipping_state() ? $parent_order->get_shipping_state() : $parent_order->get_billing_state(),
264 'postcode' => $parent_order->get_shipping_postcode() ? $parent_order->get_shipping_postcode() : $parent_order->get_billing_postcode(),
265 ];
266
267 $sub->set_address( $billing_address, 'billing' );
268 $sub->set_address( $shipping_address, 'shipping' );
269
270 $start_date = gmdate( 'Y-m-d H:i:s' );
271
272 $trial_end_days = isset( $selected_options['trial_end_days'] ) ? $selected_options['trial_end_days'] : '';
273
274 if ( '' != $trial_end_days ) {
275 $now = strtotime( 'now' );
276 $trial_end_timestamp = strtotime( "+$trial_end_days days", $now );
277
278 if ( false !== $trial_end_timestamp ) {
279 $trial_end_date = gmdate( 'Y-m-d H:i:s', $trial_end_timestamp );
280 $dates['trial_end'] = $trial_end_date;
281
282 $trial_end_timestamp = strtotime( $trial_end_date );
283 if ( false !== $trial_end_timestamp ) {
284 $next_payment_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day', $trial_end_timestamp ) );
285 $dates['next_payment'] = WC_Subscriptions_Product::get_expiration_date( $product_id, $next_payment_date );
286 }
287 }
288
289 $start_date = $sub->get_date_created();
290 $end_date = WC_Subscriptions_Product::get_expiration_date( $product_id, $start_date );
291
292 $dates['end'] = $end_date;
293 } else {
294 $dates = [
295 'trial_end' => WC_Subscriptions_Product::get_trial_expiration_date( $product_id, $start_date ),
296 'next_payment' => WC_Subscriptions_Product::get_first_renewal_payment_date( $product_id, $start_date ),
297 'end' => WC_Subscriptions_Product::get_expiration_date( $product_id, $start_date ),
298 ];
299 }
300
301 $sub->update_dates( $dates );
302 $status = isset( $selected_options['status'] ) ? $selected_options['status'] : 'active';
303 $sub->update_status( $status );
304
305 if ( ! empty( $sub ) ) {
306 $context['subscription'] = [
307 'id' => $sub->get_id(),
308 'status' => $sub->get_status(),
309 'start_date' => $sub->get_date_created(),
310 'next_payment_date' => $sub->get_date( 'next_payment' ),
311 'trial_end_date' => $sub->get_date( 'trial_end' ),
312 'end_date' => $sub->get_date( 'end' ),
313 ];
314
315 $context['parent_order'] = [
316 'id' => $parent_order->get_id(),
317 'status' => $parent_order->get_status(),
318 'total' => $parent_order->get_total(),
319 ];
320
321 $order_details = WooCommerce::get_order_context( $parent_order->get_id() );
322 if ( is_array( $order_details ) ) {
323 return array_merge( $context, $order_details );
324 }
325
326 return $context;
327 }
328 }
329
330 /**
331 * Add bundle product to subscription using WooCommerce Product Bundles integration
332 *
333 * @param \WC_Order $subscription The subscription object.
334 * @param \WC_Product $bundle_product The bundle product.
335 * @param \WC_Order_Item $parent_item The parent order item.
336 * @param \WC_Order $parent_order The parent order.
337 * @return void
338 */
339 private function add_bundle_product_to_subscription( $subscription, $bundle_product, $parent_item, $parent_order ) {
340
341 if ( class_exists( '\WC_PB_Order' ) && function_exists( 'WC_PB' ) ) {
342 $pb_order_instance = \WC_PB_Order::instance();
343 if ( is_object( $pb_order_instance ) && method_exists( $pb_order_instance, 'add_bundle_to_order' ) ) {
344
345 // Use WooCommerce Product Bundles built-in configuration generator.
346 $configuration = WC_PB()->cart->get_posted_bundle_configuration( $bundle_product );
347
348 $args = [
349 'configuration' => $configuration,
350 'silent' => true,
351 ];
352
353 $result = $pb_order_instance->add_bundle_to_order( $bundle_product, $subscription, $parent_item->get_quantity(), $args );
354
355 if ( ! is_wp_error( $result ) ) {
356 return;
357 }
358 }
359 }
360
361 // Fallback: Add as simple product if bundle integration fails.
362 if ( method_exists( $subscription, 'add_product' ) ) {
363 $subscription->add_product( $bundle_product, $parent_item->get_quantity() );
364 }
365 }
366
367
368 }
369
370 CreateSubscriptionOrderParent::get_instance();
371