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-cost.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 4 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-cost.php
288 lines
1 <?php
2 /**
3 * CreateSubscriptionOrderCost.
4 * php version 5.6
5 *
6 * @category CreateSubscriptionOrderCost
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 * CreateSubscriptionOrderCost
25 *
26 * @category CreateSubscriptionOrderCost
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 CreateSubscriptionOrderCost 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_cost';
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 a product', 'suretriggers' ),
60 'action' => 'wc_create_subscription_order_cost',
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' ) || ! function_exists( 'wcs_create_subscription' ) || ! class_exists( 'WC_Subscriptions' ) ) {
79 return [
80 'status' => 'error',
81 'message' => '`wc_create_order` or `wcs_create_subscription` function is missing.',
82 ];
83 }
84
85 if ( ! class_exists( '\WC_Order' ) ) {
86 return [
87 'status' => 'error',
88 'message' => __( '\WC_Order class not found.', 'suretriggers' ),
89
90 ];
91 }
92
93 if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
94 return [
95 'status' => 'error',
96 'message' => __( 'WC_Subscriptions_Product class not found.', 'suretriggers' ),
97
98 ];
99 }
100
101 $user_id = ap_get_user_id_from_email( $selected_options['billing_email'] );
102
103 $quantity = $selected_options['quantity'] ? $selected_options['quantity'] : 1;
104
105 if ( 'yes' == $selected_options['create_parent_order'] ) {
106 // Create Order.
107 $order = wc_create_order(
108 [
109 'status' => 'wc-pending',
110 'customer_id' => $user_id,
111 ]
112 );
113
114 if ( ! $order instanceof \WC_Order ) {
115 return;
116 }
117
118 if ( is_object( $order ) ) {
119 $order = $order;
120 }
121 if ( isset( $selected_options['product_id'] ) ) {
122 $product = wc_get_product( intval( $selected_options['product_id'] ) );
123 if ( $product instanceof \WC_Product ) {
124 $order->add_product( $product, intval( $quantity ) );
125 }
126 }
127
128 // Setting order cost to entered order cost.
129 $order->set_total( $selected_options['parent_order_cost'] );
130 // Add billing and shipping addresses.
131 $billing_address = [
132 'first_name' => $selected_options['billing_first_name'],
133 'last_name' => $selected_options['billing_last_name'],
134 'company' => $selected_options['billing_company'],
135 'country' => $selected_options['billing_country'],
136 'address_1' => $selected_options['billing_address_1'],
137 'address_2' => $selected_options['billing_address_2'],
138 'city' => $selected_options['billing_city'],
139 'state' => $selected_options['billing_state'],
140 'postcode' => $selected_options['billing_zip_code'],
141 'phone' => $selected_options['billing_phone'],
142 'email' => $selected_options['billing_email'],
143 ];
144
145 $shipping_address = [
146 'first_name' => $selected_options['shipping_first_name'] ? $selected_options['shipping_first_name'] : $selected_options['billing_first_name'],
147 'last_name' => $selected_options['shipping_last_name'] ? $selected_options['shipping_last_name'] : $selected_options['billing_last_name'],
148 'company' => $selected_options['shipping_company'] ? $selected_options['shipping_company'] : $selected_options['billing_company'],
149 'country' => $selected_options['shipping_country'] ? $selected_options['shipping_country'] : $selected_options['billing_country'],
150 'address_1' => $selected_options['shipping_address_1'] ? $selected_options['shipping_address_1'] : $selected_options['billing_address_1'],
151 'address_2' => $selected_options['shipping_address_2'] ? $selected_options['shipping_address_2'] : $selected_options['billing_address_2'],
152 'city' => $selected_options['shipping_city'] ? $selected_options['shipping_city'] : $selected_options['billing_city'],
153 'state' => $selected_options['shipping_state'] ? $selected_options['shipping_state'] : $selected_options['billing_state'],
154 'postcode' => $selected_options['shipping_zip_code'] ? $selected_options['shipping_zip_code'] : $selected_options['billing_zip_code'],
155 'phone' => $selected_options['shipping_phone'] ? $selected_options['shipping_phone'] : $selected_options['billing_phone'],
156 'email' => $selected_options['shipping_email'] ? $selected_options['shipping_email'] : $selected_options['billing_email'],
157 ];
158
159 $order->set_address( $billing_address, 'billing' );
160 $order->set_address( $selected_options['shipping_billing_address'] ? $billing_address : $shipping_address, 'shipping' );
161
162 // Setting status of order to selected status.
163 $order->update_status( $selected_options['parent_order_status'] );
164 $order->save();
165 do_action( 'woocommerce_update_order', $order->get_id() );
166
167 }
168 if ( isset( $selected_options['product_id'] ) ) {
169 $product = wc_get_product( intval( $selected_options['product_id'] ) );
170 $billing_period = WC_Subscriptions_Product::get_period( intval( $selected_options['product_id'] ) );
171 $billing_interval = WC_Subscriptions_Product::get_interval( intval( $selected_options['product_id'] ) );
172
173 // Handle bundle products - check if it contains subscription products.
174 if ( empty( $billing_period ) && $product && method_exists( $product, 'get_bundled_items' ) ) {
175 $bundled_items = $product->get_bundled_items();
176 foreach ( $bundled_items as $bundled_item ) {
177 $bundled_product_id = $bundled_item->get_product_id();
178 $bundled_period = WC_Subscriptions_Product::get_period( $bundled_product_id );
179 if ( ! empty( $bundled_period ) ) {
180 $billing_period = $bundled_period;
181 $billing_interval = WC_Subscriptions_Product::get_interval( $bundled_product_id );
182 break; // Use first subscription product's period.
183 }
184 }
185 }
186
187 // Fallback for non-subscription products.
188 if ( empty( $billing_period ) ) {
189 $billing_period = 'month';
190 }
191
192 if ( empty( $billing_interval ) || $billing_interval <= 0 ) {
193 $billing_interval = 1;
194 }
195
196 $sub_args = [
197 'customer_id' => $user_id,
198 'billing_period' => $billing_period,
199 'billing_interval' => $billing_interval,
200 ];
201 if ( 'yes' == $selected_options['create_parent_order'] ) {
202 if ( ! empty( $order ) ) {
203 $sub_args['order_id'] = $order->get_id();
204 }
205 }
206 $sub = wcs_create_subscription( $sub_args );
207
208 if ( is_wp_error( $sub ) ) {
209 if ( ! empty( $order ) ) {
210 wp_delete_post( $order->get_id(), true );
211 }
212 return [
213 'status' => 'error',
214 'message' => 'Failed to create a subscription.',
215 ];
216 }
217
218 $sub->add_product( wc_get_product( intval( $selected_options['product_id'] ) ), intval( $quantity ) );
219
220 if ( ! empty( $selected_options['coupon_code'] ) ) {
221 $sub->apply_coupon( $selected_options['coupon_code'] );
222 }
223
224 $start_date = gmdate( 'Y-m-d H:i:s' );
225
226 $trial_end_days = $selected_options['trial_end_days'];
227
228 if ( '' != $trial_end_days ) {
229 $now = strtotime( 'now' );
230 $trial_end_timestamp = strtotime( "+$trial_end_days days", $now );
231
232 if ( false !== $trial_end_timestamp ) {
233 $trial_end_date = gmdate( 'Y-m-d H:i:s', $trial_end_timestamp );
234 $dates['trial_end'] = $trial_end_date;
235
236 $trial_end_timestamp = strtotime( $trial_end_date );
237 if ( false !== $trial_end_timestamp ) {
238 $next_payment_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day', $trial_end_timestamp ) );
239 $dates['next_payment'] = WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $next_payment_date );
240 }
241 }
242
243 $start_date = $sub->get_date_created();
244 $end_date = WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $start_date );
245
246 $dates['end'] = $end_date;
247 } else {
248 $dates = [
249 'trial_end' => WC_Subscriptions_Product::get_trial_expiration_date( intval( $selected_options['product_id'] ), $start_date ),
250 'next_payment' => WC_Subscriptions_Product::get_first_renewal_payment_date( intval( $selected_options['product_id'] ), $start_date ),
251 'end' => WC_Subscriptions_Product::get_expiration_date( intval( $selected_options['product_id'] ), $start_date ),
252 ];
253 }
254
255 $sub->update_dates( $dates );
256 $sub->update_status( $selected_options['status'] );
257 $sub->calculate_totals();
258 }
259
260 if ( ! empty( $sub ) ) {
261 $context['subscription'] = [
262 'id' => $sub->get_id(),
263 'status' => $sub->get_status(),
264 'start_date' => $sub->get_date_created(),
265 'next_payment_date' => $sub->get_date( 'next_payment' ),
266 'trial_end_date' => $sub->get_date( 'trial_end' ),
267 'end_date' => $sub->get_date( 'end' ),
268 ];
269 if ( 'yes' == $selected_options['create_parent_order'] ) {
270 if ( ! empty( $order ) ) {
271 $order_details = WooCommerce::get_order_context( $order->get_id() );
272 if ( is_array( $order_details ) ) {
273 return array_merge( $context, $order_details );
274 }
275 }
276 } else {
277 return $context;
278 }
279 } else {
280 if ( ! empty( $order ) ) {
281 return WooCommerce::get_order_context( $order->get_id() );
282 }
283 }
284 }
285 }
286
287 CreateSubscriptionOrderCost::get_instance();
288